summaryrefslogtreecommitdiff
path: root/MAT
diff options
context:
space:
mode:
authorjvoisin2014-01-19 11:50:20 +0000
committerjvoisin2014-01-19 11:50:20 +0000
commit79acf1fb077da207c8058e1b2544f9d3d843b533 (patch)
treeae19fe646c84ce5773552b2a1b5c115da4d9c708 /MAT
parent9a75f1f1a7ebd745027a976ad06eca0284acc74d (diff)
Improves archive.py code consistency
Diffstat (limited to 'MAT')
-rw-r--r--MAT/archive.py77
1 files changed, 41 insertions, 36 deletions
diff --git a/MAT/archive.py b/MAT/archive.py
index 4dfc0d5..dd8dc63 100644
--- a/MAT/archive.py
+++ b/MAT/archive.py
@@ -13,6 +13,7 @@ import zipfile
13import mat 13import mat
14import parser 14import parser
15 15
16# Zip files do not support dates older than 01/01/1980
16ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) 17ZIP_EPOCH = (1980, 1, 1, 0, 0, 0)
17ZIP_EPOCH_SECONDS = (datetime.datetime(1980, 1, 1, 0, 0, 0) 18ZIP_EPOCH_SECONDS = (datetime.datetime(1980, 1, 1, 0, 0, 0)
18 - datetime.datetime(1970, 1, 1, 0, 0, 0)).total_seconds() 19 - datetime.datetime(1970, 1, 1, 0, 0, 0)).total_seconds()
@@ -78,24 +79,23 @@ class ZipStripper(GenericArchiveStripper):
78 return False 79 return False
79 for item in zipin.infolist(): 80 for item in zipin.infolist():
80 zipin.extract(item, self.tempdir) 81 zipin.extract(item, self.tempdir)
81 name = os.path.join(self.tempdir, item.filename) 82 path = os.path.join(self.tempdir, item.filename)
82 if not self.__is_zipfile_clean(item) and not list_unsupported: 83 if not self.__is_zipfile_clean(item) and not list_unsupported:
83 logging.debug('%s from %s has compromising zipinfo' % 84 logging.debug('%s from %s has compromising zipinfo' %
84 (item.filename, self.filename)) 85 (item.filename, self.filename))
85 return False 86 return False
86 if os.path.isfile(name): 87 if os.path.isfile(path):
87 cfile = mat.create_class_file(name, False, add2archive=self.add2archive) 88 cfile = mat.create_class_file(path, False, add2archive=self.add2archive)
88 if cfile: 89 if cfile is not None:
89 if not cfile.is_clean(): 90 if not cfile.is_clean():
90 logging.debug('%s from %s has compromising zipinfo' % 91 logging.debug('%s from %s has metadata' % (item.filename, self.filename))
91 (item.filename, self.filename))
92 if not list_unsupported: 92 if not list_unsupported:
93 return False 93 return False
94 ret_list.append(item.filename) 94 ret_list.append(item.filename)
95 else: 95 else:
96 logging.info('%s\'s fileformat is not supported or harmless.' 96 logging.info('%s\'s fileformat is not supported or harmless.'
97 % item.filename) 97 % item.filename)
98 basename, ext = os.path.splitext(name) 98 basename, ext = os.path.splitext(path)
99 if os.path.basename(item.filename) not in ('mimetype', '.rels'): 99 if os.path.basename(item.filename) not in ('mimetype', '.rels'):
100 if ext not in parser.NOMETA: 100 if ext not in parser.NOMETA:
101 if not list_unsupported: 101 if not list_unsupported:
@@ -117,10 +117,10 @@ class ZipStripper(GenericArchiveStripper):
117 if zipinfo_meta != {}: # zipinfo metadata 117 if zipinfo_meta != {}: # zipinfo metadata
118 metadata[item.filename + "'s zipinfo"] = str(zipinfo_meta) 118 metadata[item.filename + "'s zipinfo"] = str(zipinfo_meta)
119 zipin.extract(item, self.tempdir) 119 zipin.extract(item, self.tempdir)
120 name = os.path.join(self.tempdir, item.filename) 120 path = os.path.join(self.tempdir, item.filename)
121 if os.path.isfile(name): 121 if os.path.isfile(path):
122 cfile = mat.create_class_file(name, False, add2archive=self.add2archive) 122 cfile = mat.create_class_file(path, False, add2archive=self.add2archive)
123 if cfile: 123 if cfile is not None:
124 cfile_meta = cfile.get_meta() 124 cfile_meta = cfile.get_meta()
125 if cfile_meta != {}: 125 if cfile_meta != {}:
126 metadata[item.filename] = str(cfile_meta) 126 metadata[item.filename] = str(cfile_meta)
@@ -153,26 +153,27 @@ class ZipStripper(GenericArchiveStripper):
153 zipout = zipfile.ZipFile(self.output, 'w', allowZip64=True) 153 zipout = zipfile.ZipFile(self.output, 'w', allowZip64=True)
154 for item in zipin.infolist(): 154 for item in zipin.infolist():
155 zipin.extract(item, self.tempdir) 155 zipin.extract(item, self.tempdir)
156 name = os.path.join(self.tempdir, item.filename) 156 path = os.path.join(self.tempdir, item.filename)
157 157
158 beginning = any((True for f in beginning_blacklist if item.filename.startswith(f))) 158 beginning = any((True for f in beginning_blacklist if item.filename.startswith(f)))
159 ending = any((True for f in ending_blacklist if item.filename.endswith(f))) 159 ending = any((True for f in ending_blacklist if item.filename.endswith(f)))
160 160
161 if os.path.isfile(name) and not beginning and not ending: 161 if os.path.isfile(path) and not beginning and not ending:
162 cfile = mat.create_class_file(name, False, add2archive=self.add2archive) 162 cfile = mat.create_class_file(path, False, add2archive=self.add2archive)
163 if cfile is not None: 163 if cfile is not None:
164 old_stat = os.stat(name).st_mode 164 # Handle read-only files inside archive
165 os.chmod(name, old_stat|stat.S_IWUSR) 165 old_stat = os.stat(path).st_mode
166 os.chmod(path, old_stat|stat.S_IWUSR)
166 cfile.remove_all() 167 cfile.remove_all()
167 os.chmod(name, old_stat) 168 os.chmod(path, old_stat)
168 logging.debug('Processing %s from %s' % (item.filename, self.filename)) 169 logging.debug('Processing %s from %s' % (item.filename, self.filename))
169 elif item.filename not in whitelist: 170 elif item.filename not in whitelist:
170 logging.info('%s\'s format is not supported or harmless' % item.filename) 171 logging.info('%s\'s format is not supported or harmless' % item.filename)
171 basename, ext = os.path.splitext(name) 172 basename, ext = os.path.splitext(path)
172 if not (self.add2archive or ext in parser.NOMETA): 173 if not (self.add2archive or ext in parser.NOMETA):
173 continue 174 continue
174 os.utime(name, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS)) 175 os.utime(path, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS))
175 zipout.write(name, item.filename) 176 zipout.write(path, item.filename)
176 zipin.close() 177 zipin.close()
177 zipout.close() 178 zipout.close()
178 179
@@ -200,13 +201,14 @@ class TarStripper(GenericArchiveStripper):
200 for item in tarin.getmembers(): 201 for item in tarin.getmembers():
201 tarin.extract(item, self.tempdir) 202 tarin.extract(item, self.tempdir)
202 if item.isfile(): 203 if item.isfile():
203 complete_name = os.path.join(self.tempdir, item.name) 204 path = os.path.join(self.tempdir, item.name)
204 cfile = mat.create_class_file(complete_name, False, add2archive=self.add2archive) 205 cfile = mat.create_class_file(path, False, add2archive=self.add2archive)
205 if cfile: 206 if cfile is not None:
206 old_stat = os.stat(complete_name).st_mode 207 # Handle read-only files inside archive
207 os.chmod(complete_name, old_stat|stat.S_IWUSR) 208 old_stat = os.stat(path).st_mode
209 os.chmod(path, old_stat|stat.S_IWUSR)
208 cfile.remove_all() 210 cfile.remove_all()
209 os.chmod(complete_name, old_stat) 211 os.chmod(path, old_stat)
210 elif self.add2archive or os.path.splitext(item.name)[1] in parser.NOMETA: 212 elif self.add2archive or os.path.splitext(item.name)[1] in parser.NOMETA:
211 logging.debug('%s\' format is either not supported or harmless' % item.name) 213 logging.debug('%s\' format is either not supported or harmless' % item.name)
212 elif item.name in whitelist: 214 elif item.name in whitelist:
@@ -215,7 +217,7 @@ class TarStripper(GenericArchiveStripper):
215 else: # Don't add the file to the archive 217 else: # Don't add the file to the archive
216 logging.debug('%s will not be added' % item.name) 218 logging.debug('%s will not be added' % item.name)
217 continue 219 continue
218 tarout.add(complete_name, item.name, filter=self._remove) 220 tarout.add(path, item.name, filter=self._remove)
219 tarin.close() 221 tarin.close()
220 tarout.close() 222 tarout.close()
221 self.do_backup() 223 self.do_backup()
@@ -244,22 +246,25 @@ class TarStripper(GenericArchiveStripper):
244 tarin = tarfile.open(self.filename, 'r' + self.compression) 246 tarin = tarfile.open(self.filename, 'r' + self.compression)
245 for item in tarin.getmembers(): 247 for item in tarin.getmembers():
246 if not self.is_file_clean(item) and not list_unsupported: 248 if not self.is_file_clean(item) and not list_unsupported:
249 logging.debug('%s from %s has compromising tarinfo' %
250 (item.name, self.filename))
247 return False 251 return False
248 tarin.extract(item, self.tempdir) 252 tarin.extract(item, self.tempdir)
249 complete_name = os.path.join(self.tempdir, item.name) 253 path = os.path.join(self.tempdir, item.name)
250 if item.isfile(): 254 if item.isfile():
251 class_file = mat.create_class_file(complete_name, 255 cfile = mat.create_class_file(path, False, add2archive=self.add2archive)
252 False, add2archive=self.add2archive) 256 if cfile is not None:
253 if class_file: 257 if not cfile.is_clean():
254 if not class_file.is_clean(): 258 logging.debug('%s from %s has metadata' %
259 (item.name, self.filename))
255 if not list_unsupported: 260 if not list_unsupported:
256 return False 261 return False
257 # Nested archives are treated like unsupported files 262 # Nested archives are treated like unsupported files
258 elif isinstance(class_file, GenericArchiveStripper): 263 elif isinstance(cfile, GenericArchiveStripper):
259 ret_list.append(item.name) 264 ret_list.append(item.name)
260 else: 265 else:
261 logging.error('%s\'s format is not supported or harmless' % item.name) 266 logging.error('%s\'s format is not supported or harmless' % item.name)
262 if os.path.splitext(complete_name)[1] not in parser.NOMETA: 267 if os.path.splitext(path)[1] not in parser.NOMETA:
263 if not list_unsupported: 268 if not list_unsupported:
264 return False 269 return False
265 ret_list.append(item.name) 270 ret_list.append(item.name)
@@ -277,8 +282,8 @@ class TarStripper(GenericArchiveStripper):
277 current_meta = {} 282 current_meta = {}
278 if item.isfile(): 283 if item.isfile():
279 tarin.extract(item, self.tempdir) 284 tarin.extract(item, self.tempdir)
280 name = os.path.join(self.tempdir, item.name) 285 path = os.path.join(self.tempdir, item.name)
281 class_file = mat.create_class_file(name, False, add2archive=self.add2archive) 286 class_file = mat.create_class_file(path, False, add2archive=self.add2archive)
282 if class_file is not None: 287 if class_file is not None:
283 meta = class_file.get_meta() 288 meta = class_file.get_meta()
284 if meta: 289 if meta: