diff options
| author | jvoisin | 2011-08-16 18:11:24 +0200 |
|---|---|---|
| committer | jvoisin | 2011-08-16 18:11:24 +0200 |
| commit | 4bd3e47da02fde08acfada1795cc55170abdb00a (patch) | |
| tree | f8c7aa5fd5e1b07a28b350c5ded8125ef2467c51 /lib/archive.py | |
| parent | baf8e080125614326ba9c96ca8f2404fd12b050e (diff) | |
setup.py now works !
Diffstat (limited to 'lib/archive.py')
| -rw-r--r-- | lib/archive.py | 289 |
1 files changed, 0 insertions, 289 deletions
diff --git a/lib/archive.py b/lib/archive.py deleted file mode 100644 index 77db71c..0000000 --- a/lib/archive.py +++ /dev/null | |||
| @@ -1,289 +0,0 @@ | |||
| 1 | ''' | ||
| 2 | Take care of archives formats | ||
| 3 | ''' | ||
| 4 | |||
| 5 | import zipfile | ||
| 6 | import shutil | ||
| 7 | import os | ||
| 8 | import logging | ||
| 9 | import tempfile | ||
| 10 | |||
| 11 | import parser | ||
| 12 | import mat | ||
| 13 | from tarfile import tarfile | ||
| 14 | |||
| 15 | |||
| 16 | class GenericArchiveStripper(parser.GenericParser): | ||
| 17 | ''' | ||
| 18 | Represent a generic archive | ||
| 19 | ''' | ||
| 20 | def __init__(self, filename, parser, mime, backup, add2archive): | ||
| 21 | super(GenericArchiveStripper, self).__init__(filename, parser, mime, | ||
| 22 | backup, add2archive) | ||
| 23 | self.compression = '' | ||
| 24 | self.add2archive = add2archive | ||
| 25 | self.tempdir = tempfile.mkdtemp() | ||
| 26 | |||
| 27 | def __del__(self): | ||
| 28 | ''' | ||
| 29 | Remove the files inside the temp dir, | ||
| 30 | then remove the temp dir | ||
| 31 | ''' | ||
| 32 | for root, dirs, files in os.walk(self.tempdir): | ||
| 33 | for item in files: | ||
| 34 | path_file = os.path.join(root, item) | ||
| 35 | mat.secure_remove(path_file) | ||
| 36 | shutil.rmtree(self.tempdir) | ||
| 37 | |||
| 38 | def remove_all(self): | ||
| 39 | ''' | ||
| 40 | Call _remove_all() with in argument : "normal" | ||
| 41 | ''' | ||
| 42 | self._remove_all('normal') | ||
| 43 | |||
| 44 | def remove_all_ugly(self): | ||
| 45 | ''' | ||
| 46 | call remove_all() with in argument : "ugly" | ||
| 47 | ''' | ||
| 48 | self._remove_all('ugly') | ||
| 49 | |||
| 50 | def _remove_all(self, method): | ||
| 51 | ''' | ||
| 52 | Remove all meta, normal way if method is "normal", | ||
| 53 | else, use the ugly way (with possible data loss) | ||
| 54 | ''' | ||
| 55 | raise NotImplementedError | ||
| 56 | |||
| 57 | |||
| 58 | class ZipStripper(GenericArchiveStripper): | ||
| 59 | ''' | ||
| 60 | Represent a zip file | ||
| 61 | ''' | ||
| 62 | def is_file_clean(self, fileinfo): | ||
| 63 | ''' | ||
| 64 | Check if a ZipInfo object is clean of metadatas added | ||
| 65 | by zip itself, independently of the corresponding file metadatas | ||
| 66 | ''' | ||
| 67 | if fileinfo.comment is not '': | ||
| 68 | return False | ||
| 69 | elif fileinfo.date_time is not 0: | ||
| 70 | return False | ||
| 71 | elif fileinfo.create_system is not 0: | ||
| 72 | return False | ||
| 73 | elif fileinfo.create_version is not 0: | ||
| 74 | return False | ||
| 75 | else: | ||
| 76 | return True | ||
| 77 | |||
| 78 | def is_clean(self): | ||
| 79 | ''' | ||
| 80 | Check if the given file is clean from harmful metadata | ||
| 81 | ''' | ||
| 82 | zipin = zipfile.ZipFile(self.filename, 'r') | ||
| 83 | if zipin.comment != '': | ||
| 84 | logging.debug('%s has a comment' % self.filename) | ||
| 85 | return False | ||
| 86 | for item in zipin.infolist(): | ||
| 87 | #I have not found a way to remove the crap added by zipfile :/ | ||
| 88 | #if not self.is_file_clean(item): | ||
| 89 | # logging.debug('%s from %s has compromizing zipinfo' % | ||
| 90 | # (item.filename, self.filename)) | ||
| 91 | # return False | ||
| 92 | zipin.extract(item, self.tempdir) | ||
| 93 | name = os.path.join(self.tempdir, item.filename) | ||
| 94 | if os.path.isfile(name): | ||
| 95 | try: | ||
| 96 | cfile = mat.create_class_file(name, False, | ||
| 97 | self.add2archive) | ||
| 98 | if not cfile.is_clean(): | ||
| 99 | return False | ||
| 100 | except: | ||
| 101 | #best solution I have found | ||
| 102 | logging.info('%s\'s fileformat is not supported, or is a \ | ||
| 103 | harmless format' % item.filename) | ||
| 104 | _, ext = os.path.splitext(name) | ||
| 105 | bname = os.path.basename(item.filename) | ||
| 106 | if ext not in parser.NOMETA: | ||
| 107 | if bname != 'mimetype' and bname != '.rels': | ||
| 108 | return False | ||
| 109 | zipin.close() | ||
| 110 | return True | ||
| 111 | |||
| 112 | def get_meta(self): | ||
| 113 | ''' | ||
| 114 | Return all the metadata of a ZipFile (don't return metadatas | ||
| 115 | of contained files : should it ?) | ||
| 116 | ''' | ||
| 117 | zipin = zipfile.ZipFile(self.filename, 'r') | ||
| 118 | metadata = {} | ||
| 119 | for field in zipin.infolist(): | ||
| 120 | zipmeta = {} | ||
| 121 | zipmeta['comment'] = field.comment | ||
| 122 | zipmeta['modified'] = field.date_time | ||
| 123 | zipmeta['system'] = field.create_system | ||
| 124 | zipmeta['zip_version'] = field.create_version | ||
| 125 | metadata[field.filename] = zipmeta | ||
| 126 | metadata["%s comment" % self.filename] = zipin.comment | ||
| 127 | zipin.close() | ||
| 128 | return metadata | ||
| 129 | |||
| 130 | def _remove_all(self, method): | ||
| 131 | ''' | ||
| 132 | So far, the zipfile module does not allow to write a ZipInfo | ||
| 133 | object into a zipfile (and it's a shame !) : so data added | ||
| 134 | by zipfile itself could not be removed. It's a big concern. | ||
| 135 | Is shiping a patched version of zipfile.py a good idea ? | ||
| 136 | ''' | ||
| 137 | zipin = zipfile.ZipFile(self.filename, 'r') | ||
| 138 | zipout = zipfile.ZipFile(self.output, 'w', allowZip64=True) | ||
| 139 | for item in zipin.infolist(): | ||
| 140 | zipin.extract(item, self.tempdir) | ||
| 141 | name = os.path.join(self.tempdir, item.filename) | ||
| 142 | if os.path.isfile(name): | ||
| 143 | try: | ||
| 144 | cfile = mat.create_class_file(name, False, | ||
| 145 | self.add2archive) | ||
| 146 | if method is 'normal': | ||
| 147 | cfile.remove_all() | ||
| 148 | else: | ||
| 149 | cfile.remove_all_ugly() | ||
| 150 | logging.debug('Processing %s from %s' % (item.filename, | ||
| 151 | self.filename)) | ||
| 152 | zipout.write(name, item.filename) | ||
| 153 | except: | ||
| 154 | logging.info('%s\'s format is not supported or harmless' % | ||
| 155 | item.filename) | ||
| 156 | _, ext = os.path.splitext(name) | ||
| 157 | if self.add2archive or ext in parser.NOMETA: | ||
| 158 | zipout.write(name, item.filename) | ||
| 159 | zipout.comment = '' | ||
| 160 | zipin.close() | ||
| 161 | zipout.close() | ||
| 162 | logging.info('%s treated' % self.filename) | ||
| 163 | self.do_backup() | ||
| 164 | |||
| 165 | |||
| 166 | class TarStripper(GenericArchiveStripper): | ||
| 167 | ''' | ||
| 168 | Represent a tarfile archive | ||
| 169 | ''' | ||
| 170 | def _remove(self, current_file): | ||
| 171 | ''' | ||
| 172 | remove the meta added by tar itself to the file | ||
| 173 | ''' | ||
| 174 | current_file.mtime = 0 | ||
| 175 | current_file.uid = 0 | ||
| 176 | current_file.gid = 0 | ||
| 177 | current_file.uname = '' | ||
| 178 | current_file.gname = '' | ||
| 179 | return current_file | ||
| 180 | |||
| 181 | def _remove_all(self, method): | ||
| 182 | tarin = tarfile.open(self.filename, 'r' + self.compression) | ||
| 183 | tarout = tarfile.open(self.output, 'w' + self.compression) | ||
| 184 | for item in tarin.getmembers(): | ||
| 185 | tarin.extract(item, self.tempdir) | ||
| 186 | name = os.path.join(self.tempdir, item.name) | ||
| 187 | if item.type is '0': # is item a regular file ? | ||
| 188 | #no backup file | ||
| 189 | try: | ||
| 190 | cfile = mat.create_class_file(name, False, | ||
| 191 | self.add2archive) | ||
| 192 | if method is 'normal': | ||
| 193 | cfile.remove_all() | ||
| 194 | else: | ||
| 195 | cfile.remove_all_ugly() | ||
| 196 | tarout.add(name, item.name, filter=self._remove) | ||
| 197 | except: | ||
| 198 | logging.info('%s\' format is not supported or harmless' % | ||
| 199 | item.name) | ||
| 200 | _, ext = os.path.splitext(name) | ||
| 201 | if self.add2archive or ext in parser.NOMETA: | ||
| 202 | tarout.add(name, item.name, filter=self._remove) | ||
| 203 | tarin.close() | ||
| 204 | tarout.close() | ||
| 205 | self.do_backup() | ||
| 206 | |||
| 207 | def is_file_clean(self, current_file): | ||
| 208 | ''' | ||
| 209 | Check metadatas added by tar | ||
| 210 | ''' | ||
| 211 | if current_file.mtime is not 0: | ||
| 212 | return False | ||
| 213 | elif current_file.uid is not 0: | ||
| 214 | return False | ||
| 215 | elif current_file.gid is not 0: | ||
| 216 | return False | ||
| 217 | elif current_file.uname is not '': | ||
| 218 | return False | ||
| 219 | elif current_file.gname is not '': | ||
| 220 | return False | ||
| 221 | else: | ||
| 222 | return True | ||
| 223 | |||
| 224 | def is_clean(self): | ||
| 225 | ''' | ||
| 226 | Check if the file is clean from harmful metadatas | ||
| 227 | ''' | ||
| 228 | tarin = tarfile.open(self.filename, 'r' + self.compression) | ||
| 229 | for item in tarin.getmembers(): | ||
| 230 | if not self.is_file_clean(item): | ||
| 231 | tarin.close() | ||
| 232 | return False | ||
| 233 | tarin.extract(item, self.tempdir) | ||
| 234 | name = os.path.join(self.tempdir, item.name) | ||
| 235 | if item.type is '0': # is item a regular file ? | ||
| 236 | try: | ||
| 237 | class_file = mat.create_class_file(name, | ||
| 238 | False, self.add2archive) # no backup file | ||
| 239 | if not class_file.is_clean(): | ||
| 240 | tarin.close() | ||
| 241 | return False | ||
| 242 | except: | ||
| 243 | logging.error('%s\'s foramt is not supported or harmless' % | ||
| 244 | item.filename) | ||
| 245 | _, ext = os.path.splitext(name) | ||
| 246 | if ext not in parser.NOMETA: | ||
| 247 | tarin.close() | ||
| 248 | return False | ||
| 249 | tarin.close() | ||
| 250 | return True | ||
| 251 | |||
| 252 | def get_meta(self): | ||
| 253 | ''' | ||
| 254 | Return a dict with all the meta of the file | ||
| 255 | ''' | ||
| 256 | tarin = tarfile.open(self.filename, 'r' + self.compression) | ||
| 257 | metadata = {} | ||
| 258 | for current_file in tarin.getmembers(): | ||
| 259 | if current_file.type is '0': | ||
| 260 | if not self.is_file_clean(current_file): # if there is meta | ||
| 261 | current_meta = {} | ||
| 262 | current_meta['mtime'] = current_file.mtime | ||
| 263 | current_meta['uid'] = current_file.uid | ||
| 264 | current_meta['gid'] = current_file.gid | ||
| 265 | current_meta['uname'] = current_file.uname | ||
| 266 | current_meta['gname'] = current_file.gname | ||
| 267 | metadata[current_file.name] = current_meta | ||
| 268 | tarin.close() | ||
| 269 | return metadata | ||
| 270 | |||
| 271 | |||
| 272 | class GzipStripper(TarStripper): | ||
| 273 | ''' | ||
| 274 | Represent a tar.gz archive | ||
| 275 | ''' | ||
| 276 | def __init__(self, filename, parser, mime, backup, add2archive): | ||
| 277 | super(GzipStripper, self).__init__(filename, parser, mime, backup, | ||
| 278 | add2archive) | ||
| 279 | self.compression = ':gz' | ||
| 280 | |||
| 281 | |||
| 282 | class Bzip2Stripper(TarStripper): | ||
| 283 | ''' | ||
| 284 | Represents a tar.bz2 archive | ||
| 285 | ''' | ||
| 286 | def __init__(self, filename, parser, mime, backup, add2archive): | ||
| 287 | super(Bzip2Stripper, self).__init__(filename, parser, mime, backup, | ||
| 288 | add2archive) | ||
| 289 | self.compression = ':bz2' | ||
