diff options
| author | jvoisin | 2012-01-21 23:56:52 +0100 |
|---|---|---|
| committer | jvoisin | 2012-01-21 23:56:52 +0100 |
| commit | e96e5b8ac44095668198e3e6797a3f77d9fb4634 (patch) | |
| tree | d4d6d63b290d2439442906c4822ee48c97b5576b | |
| parent | ca744afeffde81b8e71ad7b6763ba0573ffad752 (diff) | |
Preliminary handling of ctime/mtime reset
| -rw-r--r-- | mat/archive.py | 9 | ||||
| -rw-r--r-- | mat/audio.py | 2 | ||||
| -rw-r--r-- | mat/exiftool.py | 1 | ||||
| -rw-r--r-- | mat/misc.py | 1 | ||||
| -rw-r--r-- | mat/office.py | 4 | ||||
| -rw-r--r-- | mat/parser.py | 31 |
6 files changed, 46 insertions, 2 deletions
diff --git a/mat/archive.py b/mat/archive.py index 9993102..84ef10a 100644 --- a/mat/archive.py +++ b/mat/archive.py | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | import zipfile | 5 | import zipfile |
| 6 | import shutil | 6 | import shutil |
| 7 | import os | 7 | import os |
| 8 | import time | ||
| 8 | import logging | 9 | import logging |
| 9 | import tempfile | 10 | import tempfile |
| 10 | 11 | ||
| @@ -12,6 +13,10 @@ import parser | |||
| 12 | import mat | 13 | import mat |
| 13 | from tarfile import tarfile | 14 | from tarfile import tarfile |
| 14 | 15 | ||
| 16 | #Zip fileformat does not handle dates | ||
| 17 | #prior to 1980 | ||
| 18 | ZIP_TIME = 315529200 #1jan1980 | ||
| 19 | |||
| 15 | 20 | ||
| 16 | class GenericArchiveStripper(parser.GenericParser): | 21 | class GenericArchiveStripper(parser.GenericParser): |
| 17 | ''' | 22 | ''' |
| @@ -161,6 +166,9 @@ harmless format' % item.filename) | |||
| 161 | zipout.close() | 166 | zipout.close() |
| 162 | logging.info('%s treated' % self.filename) | 167 | logging.info('%s treated' % self.filename) |
| 163 | self.do_backup() | 168 | self.do_backup() |
| 169 | #time = 1980 | ||
| 170 | # TODO | ||
| 171 | #self.set_time(time) | ||
| 164 | return True | 172 | return True |
| 165 | 173 | ||
| 166 | 174 | ||
| @@ -204,6 +212,7 @@ class TarStripper(GenericArchiveStripper): | |||
| 204 | tarin.close() | 212 | tarin.close() |
| 205 | tarout.close() | 213 | tarout.close() |
| 206 | self.do_backup() | 214 | self.do_backup() |
| 215 | self.set_time(parser.EPOCH) | ||
| 207 | return True | 216 | return True |
| 208 | 217 | ||
| 209 | def is_file_clean(self, current_file): | 218 | def is_file_clean(self, current_file): |
diff --git a/mat/audio.py b/mat/audio.py index ed849ee..4682db1 100644 --- a/mat/audio.py +++ b/mat/audio.py | |||
| @@ -35,6 +35,7 @@ class OggStripper(parser.GenericParser): | |||
| 35 | mfile = OggVorbis(self.filename) | 35 | mfile = OggVorbis(self.filename) |
| 36 | mfile.delete() | 36 | mfile.delete() |
| 37 | mfile.save() | 37 | mfile.save() |
| 38 | self.set_time(parser.EPOCH) | ||
| 38 | return True | 39 | return True |
| 39 | 40 | ||
| 40 | def is_clean(self): | 41 | def is_clean(self): |
| @@ -74,6 +75,7 @@ class FlacStripper(parser.GenericParser): | |||
| 74 | mfile.delete() | 75 | mfile.delete() |
| 75 | mfile.clear_pictures() | 76 | mfile.clear_pictures() |
| 76 | mfile.save() | 77 | mfile.save() |
| 78 | self.set_time(parser.EPOCH) | ||
| 77 | return True | 79 | return True |
| 78 | 80 | ||
| 79 | def is_clean(self): | 81 | def is_clean(self): |
diff --git a/mat/exiftool.py b/mat/exiftool.py index 758a094..47e7dfc 100644 --- a/mat/exiftool.py +++ b/mat/exiftool.py | |||
| @@ -43,6 +43,7 @@ class ExiftoolStripper(parser.GenericParser): | |||
| 43 | [ 'exiftool', '-m', '-All=', '-overwrite_original', self.filename ], | 43 | [ 'exiftool', '-m', '-All=', '-overwrite_original', self.filename ], |
| 44 | stdout=open('/dev/null')) | 44 | stdout=open('/dev/null')) |
| 45 | process.wait() | 45 | process.wait() |
| 46 | self.set_time(parser.EPOCH) | ||
| 46 | return True | 47 | return True |
| 47 | except: | 48 | except: |
| 48 | return False | 49 | return False |
diff --git a/mat/misc.py b/mat/misc.py index d084861..80ccc79 100644 --- a/mat/misc.py +++ b/mat/misc.py | |||
| @@ -60,4 +60,5 @@ class TorrentStripper(parser.GenericParser): | |||
| 60 | with open(self.output, 'w') as f: # encode the decoded torrent | 60 | with open(self.output, 'w') as f: # encode the decoded torrent |
| 61 | f.write(bencode.bencode(decoded)) # and write it in self.output | 61 | f.write(bencode.bencode(decoded)) # and write it in self.output |
| 62 | self.do_backup() | 62 | self.do_backup() |
| 63 | self.set_time(parser.EPOCH) | ||
| 63 | return True | 64 | return True |
diff --git a/mat/office.py b/mat/office.py index e1d738e..5ff9a83 100644 --- a/mat/office.py +++ b/mat/office.py | |||
| @@ -100,6 +100,7 @@ class OpenDocumentStripper(archive.GenericArchiveStripper): | |||
| 100 | zipin.close() | 100 | zipin.close() |
| 101 | zipout.close() | 101 | zipout.close() |
| 102 | self.do_backup() | 102 | self.do_backup() |
| 103 | self.set_time(archive.ZIP_TIME) | ||
| 103 | return True | 104 | return True |
| 104 | 105 | ||
| 105 | def is_clean(self): | 106 | def is_clean(self): |
| @@ -213,6 +214,8 @@ class PdfStripper(parser.GenericParser): | |||
| 213 | if processed is False: | 214 | if processed is False: |
| 214 | logging.error('Please install either pdfrw, or exiftool to\ | 215 | logging.error('Please install either pdfrw, or exiftool to\ |
| 215 | fully handle PDF files') | 216 | fully handle PDF files') |
| 217 | |||
| 218 | self.set_time(parser.EPOCH) | ||
| 216 | return processed | 219 | return processed |
| 217 | 220 | ||
| 218 | def get_meta(self): | 221 | def get_meta(self): |
| @@ -274,6 +277,7 @@ class OpenXmlStripper(archive.GenericArchiveStripper): | |||
| 274 | zipin.close() | 277 | zipin.close() |
| 275 | zipout.close() | 278 | zipout.close() |
| 276 | self.do_backup() | 279 | self.do_backup() |
| 280 | self.set_time(archive.ZIP_TIME) | ||
| 277 | return True | 281 | return True |
| 278 | 282 | ||
| 279 | def is_clean(self): | 283 | def is_clean(self): |
diff --git a/mat/parser.py b/mat/parser.py index 6dc5d0b..0c6f0ee 100644 --- a/mat/parser.py +++ b/mat/parser.py | |||
| @@ -6,15 +6,18 @@ import hachoir_core | |||
| 6 | import hachoir_editor | 6 | import hachoir_editor |
| 7 | 7 | ||
| 8 | import os | 8 | import os |
| 9 | import time | ||
| 10 | import sys | ||
| 9 | 11 | ||
| 10 | import mat | 12 | import mat |
| 11 | 13 | ||
| 12 | NOMETA = ('.bmp', '.rdf', '.txt', '.xml', '.rels') | 14 | NOMETA = ('.bmp', '.rdf', '.txt', '.xml', '.rels') |
| 15 | EPOCH = 0 | ||
| 13 | #bmp : image | 16 | #bmp : image |
| 14 | #rdf : text | 17 | #rdf : text |
| 15 | #txt : plain text | 18 | #txt : plain text |
| 16 | #xml : formated text | 19 | #xml : formated text |
| 17 | #rels : openxml foramted text | 20 | #rels : openxml formated text |
| 18 | 21 | ||
| 19 | 22 | ||
| 20 | FIELD = object() | 23 | FIELD = object() |
| @@ -55,7 +58,14 @@ class GenericParser(object): | |||
| 55 | if remove is FIELD: | 58 | if remove is FIELD: |
| 56 | if not self._is_clean(field): | 59 | if not self._is_clean(field): |
| 57 | return False | 60 | return False |
| 58 | return True | 61 | |
| 62 | def is_time_clean(self): | ||
| 63 | ''' | ||
| 64 | Check if the atime and the mtime | ||
| 65 | of self.filename is Epoch | ||
| 66 | ''' | ||
| 67 | stat = os.stat(self.filename) | ||
| 68 | return stat.st_atime == 0 and stat.st_mtime == 0 | ||
| 59 | 69 | ||
| 60 | def remove_all(self): | 70 | def remove_all(self): |
| 61 | ''' | 71 | ''' |
| @@ -64,6 +74,7 @@ class GenericParser(object): | |||
| 64 | state = self._remove_all(self.editor) | 74 | state = self._remove_all(self.editor) |
| 65 | hachoir_core.field.writeIntoFile(self.editor, self.output) | 75 | hachoir_core.field.writeIntoFile(self.editor, self.output) |
| 66 | self.do_backup() | 76 | self.do_backup() |
| 77 | self.set_time(EPOCH) | ||
| 67 | return state | 78 | return state |
| 68 | 79 | ||
| 69 | def _remove_all(self, fieldset): | 80 | def _remove_all(self, fieldset): |
| @@ -128,3 +139,19 @@ class GenericParser(object): | |||
| 128 | if self.backup is False: | 139 | if self.backup is False: |
| 129 | mat.secure_remove(self.filename) | 140 | mat.secure_remove(self.filename) |
| 130 | os.rename(self.output, self.filename) | 141 | os.rename(self.output, self.filename) |
| 142 | |||
| 143 | def set_time(self, time): | ||
| 144 | ''' | ||
| 145 | Set the ctime of the file to $time | ||
| 146 | ''' | ||
| 147 | filename = '' | ||
| 148 | if self.backup is True: | ||
| 149 | filename = self.output | ||
| 150 | else: | ||
| 151 | filename = self.filename | ||
| 152 | |||
| 153 | try: | ||
| 154 | os.utime(filename, (time, time)) | ||
| 155 | except: | ||
| 156 | print "Unable to set %s's date to %s" % (filename, time) | ||
| 157 | sys.exit(1) | ||
