diff options
| author | jvoisin | 2011-07-26 02:45:10 +0200 |
|---|---|---|
| committer | jvoisin | 2011-07-26 02:45:10 +0200 |
| commit | 243098b3432e8a424dd8217662d36228545af8a1 (patch) | |
| tree | 6e10c1a0710eac7c08bb1c5ceee0aa91e14cc51c | |
| parent | ea21231d6d332b5e27e85c0ce0c103cfb56433f8 (diff) | |
Complete the archive.py module : full zip, tar, tar.bz2 and tar.gz support.
| -rw-r--r-- | lib/archive.py | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/lib/archive.py b/lib/archive.py index 6e3efc0..8a8f7f4 100644 --- a/lib/archive.py +++ b/lib/archive.py | |||
| @@ -33,6 +33,9 @@ class GenericArchiveStripper(parser.Generic_parser): | |||
| 33 | self._remove_all('ugly') | 33 | self._remove_all('ugly') |
| 34 | 34 | ||
| 35 | class ZipStripper(GenericArchiveStripper): | 35 | class ZipStripper(GenericArchiveStripper): |
| 36 | ''' | ||
| 37 | Represent a zip file | ||
| 38 | ''' | ||
| 36 | def is_file_clean(self, file): | 39 | def is_file_clean(self, file): |
| 37 | if file.comment is not '': | 40 | if file.comment is not '': |
| 38 | return False | 41 | return False |
| @@ -48,16 +51,22 @@ class ZipStripper(GenericArchiveStripper): | |||
| 48 | def is_clean(self): | 51 | def is_clean(self): |
| 49 | zipin = zipfile.ZipFile(self.filename, 'r') | 52 | zipin = zipfile.ZipFile(self.filename, 'r') |
| 50 | if zipin.comment != '': | 53 | if zipin.comment != '': |
| 54 | logging.debug('%s has a comment' % self.filename) | ||
| 51 | return False | 55 | return False |
| 52 | for item in zipin.infolist(): | 56 | for item in zipin.infolist(): |
| 53 | if not self.is_file_clean(item): | 57 | #I have not found a way to remove the crap added by zipfile :/ |
| 54 | return False | 58 | #if not self.is_file_clean(item): |
| 59 | # logging.debug('%s from %s has compromizing zipinfo' % | ||
| 60 | # (item.filename, self.filename)) | ||
| 61 | # return False | ||
| 55 | zipin.extract(item, self.tempdir) | 62 | zipin.extract(item, self.tempdir) |
| 56 | name = os.path.join(self.tempdir, item.filename) | 63 | name = os.path.join(self.tempdir, item.filename) |
| 57 | if os.path.isfile(name): | 64 | if os.path.isfile(name): |
| 58 | try: | 65 | try: |
| 59 | cfile = mat.create_class_file(name, False, | 66 | cfile = mat.create_class_file(name, False, |
| 60 | self.add2archive) | 67 | self.add2archive) |
| 68 | if not cfile.is_clean(): | ||
| 69 | return False | ||
| 61 | except: | 70 | except: |
| 62 | #best solution I have found | 71 | #best solution I have found |
| 63 | logging.error('%s is not supported' % item.filename) | 72 | logging.error('%s is not supported' % item.filename) |
| @@ -66,7 +75,7 @@ class ZipStripper(GenericArchiveStripper): | |||
| 66 | return False | 75 | return False |
| 67 | mat.secure_remove(name) | 76 | mat.secure_remove(name) |
| 68 | zipin.close() | 77 | zipin.close() |
| 69 | return False | 78 | return True |
| 70 | 79 | ||
| 71 | def get_meta(self): | 80 | def get_meta(self): |
| 72 | zipin = zipfile.ZipFile(self.filename, 'r') | 81 | zipin = zipfile.ZipFile(self.filename, 'r') |
| @@ -84,6 +93,12 @@ class ZipStripper(GenericArchiveStripper): | |||
| 84 | 93 | ||
| 85 | 94 | ||
| 86 | def _remove_all(self, method): | 95 | def _remove_all(self, method): |
| 96 | ''' | ||
| 97 | So far, the zipfile module does not allow to write a ZipInfo | ||
| 98 | object into a zipfile (and it's a shame !) : so data added | ||
| 99 | by zipfile itself could not be removed. It's a big concern. | ||
| 100 | Is shiping a patched version of zipfile.py a good idea ? | ||
| 101 | ''' | ||
| 87 | zipin = zipfile.ZipFile(self.filename, 'r') | 102 | zipin = zipfile.ZipFile(self.filename, 'r') |
| 88 | zipout = zipfile.ZipFile(self.output, 'w', | 103 | zipout = zipfile.ZipFile(self.output, 'w', |
| 89 | allowZip64=True) | 104 | allowZip64=True) |
| @@ -108,9 +123,9 @@ class ZipStripper(GenericArchiveStripper): | |||
| 108 | zipout.write(name, item.filename) | 123 | zipout.write(name, item.filename) |
| 109 | mat.secure_remove(name) | 124 | mat.secure_remove(name) |
| 110 | zipout.comment = '' | 125 | zipout.comment = '' |
| 111 | logging.info('%s treated' % self.filename) | ||
| 112 | zipin.close() | 126 | zipin.close() |
| 113 | zipout.close() | 127 | zipout.close() |
| 128 | logging.info('%s treated' % self.filename) | ||
| 114 | self.do_backup() | 129 | self.do_backup() |
| 115 | 130 | ||
| 116 | 131 | ||
| @@ -178,10 +193,18 @@ class TarStripper(GenericArchiveStripper): | |||
| 178 | name = os.path.join(self.tempdir, item.name) | 193 | name = os.path.join(self.tempdir, item.name) |
| 179 | if item.type is '0': #is item a regular file ? | 194 | if item.type is '0': #is item a regular file ? |
| 180 | #no backup file | 195 | #no backup file |
| 181 | class_file = mat.create_class_file(name, False,self.add2archive) | 196 | try: |
| 197 | class_file = mat.create_class_file(name, | ||
| 198 | False, self.add2archive) | ||
| 199 | if not class_file.is_clean(): | ||
| 200 | return False | ||
| 201 | except: | ||
| 202 | #best solution I have found | ||
| 203 | logging.error('%s is not supported' % item.filename) | ||
| 204 | _, ext = os.path.splitext(name) | ||
| 205 | if ext not in parser.NOMETA: | ||
| 206 | return False | ||
| 182 | mat.secure_remove(name) | 207 | mat.secure_remove(name) |
| 183 | if not class_file.is_clean():#if the extracted file is not clean | ||
| 184 | return False | ||
| 185 | tarin.close() | 208 | tarin.close() |
| 186 | return True | 209 | return True |
| 187 | 210 | ||
