Debian Codegraph @

stetl/filters/archiveexpander.py

raw
1 # Expands an archive file into a collection of files.
2 #
3 # Author: Just van den Broecke 2021
4 #
5 import os.path
6 from stetl.component import Config
7 from stetl.filter import Filter
8 from stetl.util import Util
9 from stetl.packet import FORMAT
10
11 log = Util.get_log('archiveexpander')
12
13
14 class ArchiveExpander(Filter):
15 """
16 Abstract Base Class.
17 Expands an archive file into a collection of files.
18
19 consumes=FORMAT.string, produces=FORMAT.string
20 """
21
22 # Start attribute config meta
23
24 @Config(ptype=str, default='temp_dir', required=True)
25 def target_dir(self):
26 """
27 Target directory to write the extracted files to.
28 """
29 pass
30
31 @Config(ptype=bool, default=False, required=False)
32 def remove_input_file(self):
33 """
34 Delete input archive file when the chain has been completed?
35 """
36 pass
37
38 @Config(ptype=bool, default=True, required=False)
39 def clear_target_dir(self):
40 """
41 Delete the files from the target directory when the chain has been completed?
42 """
43 pass
44
45 # End attribute config meta
46
47 # Constructor
48 def __init__(self, configdict, section, consumes, produces):
49 Filter.__init__(self, configdict, section, consumes=consumes, produces=produces)
50 self.input_archive_file = None
51 if not os.path.exists(self.target_dir):
52 os.mkdir(self.target_dir)
53
54 def remove_file(self, file_path):
55 if os.path.isfile(file_path):
56 os.remove(file_path)
57
58 def wipe_dir(self, dir_path):
59 if os.path.isdir(dir_path):
60 for file_object in os.listdir(dir_path):
61 file_object_path = os.path.join(dir_path, file_object)
62 if os.path.isdir(file_object_path):
63 self.wipe_dir(file_object_path)
64 os.rmdir(file_object_path)
65 return
66
67 os.remove(file_object_path)
68
69 def expand_archive(self, packet):
70 log.error('Only classes derived from ArchiveExpander can be used!')
71
72 def invoke(self, packet):
73
74 if packet.data is None:
75 log.info("Input data is empty")
76 return packet
77
78 # Optionally clear target dir
79 self.wipe_dir(self.target_dir)
80
81 self.input_archive_file = packet.data
82
83 # Let derived class provide archive expansion (.zip, .tar etc)
84 self.expand_archive(self.input_archive_file)
85 if not os.listdir(self.target_dir):
86 log.warn('No expanded files in {}'.format(self.target_dir))
87 packet.data = None
88 return packet
89
90 # ASSERT: expanded files in target dir
91 file_count = len(os.listdir(self.target_dir))
92 log.info('Expanded {} into {} OK - filecount={}'.format(
93 self.input_archive_file, self.target_dir, file_count))
94
95 # Output the target dir path where expanded files are found
96 packet.data = self.target_dir
97
98 return packet
99
100 def after_chain_invoke(self, packet):
101 if self.remove_input_file:
102 self.remove_file(self.input_archive_file)
103
104 if self.clear_target_dir:
105 self.wipe_dir(self.target_dir)
106
107 return True
108
109
110 class ZipArchiveExpander(ArchiveExpander):
111 """
112 Extracts all files from a ZIP file into the configured target directory.
113
114 consumes=FORMAT.string, produces=FORMAT.string
115 """
116
117 def __init__(self, configdict, section):
118 ArchiveExpander.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string)
119
120 def expand_archive(self, file_path):
121
122 import zipfile
123 if not file_path.lower().endswith('zip'):
124 log.warn('No zipfile passed: {}'.format(file_path))
125 return
126
127 zipfile.ZipFile(file_path).extractall(path=self.target_dir)