From 79acf1fb077da207c8058e1b2544f9d3d843b533 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 19 Jan 2014 11:50:20 +0000 Subject: Improves archive.py code consistency --- MAT/archive.py | 77 +++++++++++++++++++++++++++++++--------------------------- 1 file 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 import mat import parser +# Zip files do not support dates older than 01/01/1980 ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) ZIP_EPOCH_SECONDS = (datetime.datetime(1980, 1, 1, 0, 0, 0) - datetime.datetime(1970, 1, 1, 0, 0, 0)).total_seconds() @@ -78,24 +79,23 @@ class ZipStripper(GenericArchiveStripper): return False for item in zipin.infolist(): zipin.extract(item, self.tempdir) - name = os.path.join(self.tempdir, item.filename) + path = os.path.join(self.tempdir, item.filename) if not self.__is_zipfile_clean(item) and not list_unsupported: logging.debug('%s from %s has compromising zipinfo' % (item.filename, self.filename)) return False - if os.path.isfile(name): - cfile = mat.create_class_file(name, False, add2archive=self.add2archive) - if cfile: + if os.path.isfile(path): + cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + if cfile is not None: if not cfile.is_clean(): - logging.debug('%s from %s has compromising zipinfo' % - (item.filename, self.filename)) + logging.debug('%s from %s has metadata' % (item.filename, self.filename)) if not list_unsupported: return False ret_list.append(item.filename) else: logging.info('%s\'s fileformat is not supported or harmless.' % item.filename) - basename, ext = os.path.splitext(name) + basename, ext = os.path.splitext(path) if os.path.basename(item.filename) not in ('mimetype', '.rels'): if ext not in parser.NOMETA: if not list_unsupported: @@ -117,10 +117,10 @@ class ZipStripper(GenericArchiveStripper): if zipinfo_meta != {}: # zipinfo metadata metadata[item.filename + "'s zipinfo"] = str(zipinfo_meta) zipin.extract(item, self.tempdir) - name = os.path.join(self.tempdir, item.filename) - if os.path.isfile(name): - cfile = mat.create_class_file(name, False, add2archive=self.add2archive) - if cfile: + path = os.path.join(self.tempdir, item.filename) + if os.path.isfile(path): + cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + if cfile is not None: cfile_meta = cfile.get_meta() if cfile_meta != {}: metadata[item.filename] = str(cfile_meta) @@ -153,26 +153,27 @@ class ZipStripper(GenericArchiveStripper): zipout = zipfile.ZipFile(self.output, 'w', allowZip64=True) for item in zipin.infolist(): zipin.extract(item, self.tempdir) - name = os.path.join(self.tempdir, item.filename) + path = os.path.join(self.tempdir, item.filename) beginning = any((True for f in beginning_blacklist if item.filename.startswith(f))) ending = any((True for f in ending_blacklist if item.filename.endswith(f))) - if os.path.isfile(name) and not beginning and not ending: - cfile = mat.create_class_file(name, False, add2archive=self.add2archive) + if os.path.isfile(path) and not beginning and not ending: + cfile = mat.create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: - old_stat = os.stat(name).st_mode - os.chmod(name, old_stat|stat.S_IWUSR) + # Handle read-only files inside archive + old_stat = os.stat(path).st_mode + os.chmod(path, old_stat|stat.S_IWUSR) cfile.remove_all() - os.chmod(name, old_stat) + os.chmod(path, old_stat) logging.debug('Processing %s from %s' % (item.filename, self.filename)) elif item.filename not in whitelist: logging.info('%s\'s format is not supported or harmless' % item.filename) - basename, ext = os.path.splitext(name) + basename, ext = os.path.splitext(path) if not (self.add2archive or ext in parser.NOMETA): continue - os.utime(name, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS)) - zipout.write(name, item.filename) + os.utime(path, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS)) + zipout.write(path, item.filename) zipin.close() zipout.close() @@ -200,13 +201,14 @@ class TarStripper(GenericArchiveStripper): for item in tarin.getmembers(): tarin.extract(item, self.tempdir) if item.isfile(): - complete_name = os.path.join(self.tempdir, item.name) - cfile = mat.create_class_file(complete_name, False, add2archive=self.add2archive) - if cfile: - old_stat = os.stat(complete_name).st_mode - os.chmod(complete_name, old_stat|stat.S_IWUSR) + path = os.path.join(self.tempdir, item.name) + cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + if cfile is not None: + # Handle read-only files inside archive + old_stat = os.stat(path).st_mode + os.chmod(path, old_stat|stat.S_IWUSR) cfile.remove_all() - os.chmod(complete_name, old_stat) + os.chmod(path, old_stat) elif self.add2archive or os.path.splitext(item.name)[1] in parser.NOMETA: logging.debug('%s\' format is either not supported or harmless' % item.name) elif item.name in whitelist: @@ -215,7 +217,7 @@ class TarStripper(GenericArchiveStripper): else: # Don't add the file to the archive logging.debug('%s will not be added' % item.name) continue - tarout.add(complete_name, item.name, filter=self._remove) + tarout.add(path, item.name, filter=self._remove) tarin.close() tarout.close() self.do_backup() @@ -244,22 +246,25 @@ class TarStripper(GenericArchiveStripper): tarin = tarfile.open(self.filename, 'r' + self.compression) for item in tarin.getmembers(): if not self.is_file_clean(item) and not list_unsupported: + logging.debug('%s from %s has compromising tarinfo' % + (item.name, self.filename)) return False tarin.extract(item, self.tempdir) - complete_name = os.path.join(self.tempdir, item.name) + path = os.path.join(self.tempdir, item.name) if item.isfile(): - class_file = mat.create_class_file(complete_name, - False, add2archive=self.add2archive) - if class_file: - if not class_file.is_clean(): + cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + if cfile is not None: + if not cfile.is_clean(): + logging.debug('%s from %s has metadata' % + (item.name, self.filename)) if not list_unsupported: return False # Nested archives are treated like unsupported files - elif isinstance(class_file, GenericArchiveStripper): + elif isinstance(cfile, GenericArchiveStripper): ret_list.append(item.name) else: logging.error('%s\'s format is not supported or harmless' % item.name) - if os.path.splitext(complete_name)[1] not in parser.NOMETA: + if os.path.splitext(path)[1] not in parser.NOMETA: if not list_unsupported: return False ret_list.append(item.name) @@ -277,8 +282,8 @@ class TarStripper(GenericArchiveStripper): current_meta = {} if item.isfile(): tarin.extract(item, self.tempdir) - name = os.path.join(self.tempdir, item.name) - class_file = mat.create_class_file(name, False, add2archive=self.add2archive) + path = os.path.join(self.tempdir, item.name) + class_file = mat.create_class_file(path, False, add2archive=self.add2archive) if class_file is not None: meta = class_file.get_meta() if meta: -- cgit v1.3