From 4dcdf721d8ab5b7c8925ac9ad55f4dedd2c8e7df Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sat, 29 Oct 2011 20:57:12 +0200 Subject: Take care about the return value of external utils --- TODO | 5 ----- mat-cli | 7 ++++--- mat-gui | 8 ++++---- mat/archive.py | 6 ++++-- mat/audio.py | 2 ++ mat/exiftool.py | 22 +++++++++++++--------- mat/misc.py | 1 + mat/office.py | 53 ++++++++++++++++++++++++++++------------------------- mat/parser.py | 16 ++++++++++------ 9 files changed, 66 insertions(+), 54 deletions(-) diff --git a/TODO b/TODO index 6899851..07504c2 100644 --- a/TODO +++ b/TODO @@ -3,12 +3,7 @@ GENERAL: the ultimate goal is to remove all "harmful meta" stuff - Fix the ugly function get_sharedir - - What is wrong with the testsuite ? - Handle exotic characters in filenames - - use the return code of external apps used by mat - to check that everything went right - - proper handling of pdf with exiftool - - enhance the exiftool's presence test GUI: - Drag and Drop diff --git a/mat-cli b/mat-cli index 7819a41..a83891a 100755 --- a/mat-cli +++ b/mat-cli @@ -93,9 +93,10 @@ def clean_meta(class_file, filename, force): if force is False and class_file.is_clean(): print('%s is already clean' % filename) else: - class_file.remove_all() - print('%s cleaned !' % filename) - + if class_file.remove_all(): + print('%s cleaned !' % filename) + else: + print('Unable to clean %s', filename) def clean_meta_ugly(class_file, filename, force): ''' diff --git a/mat-gui b/mat-gui index 0b39d8c..9a9f95b 100755 --- a/mat-gui +++ b/mat-gui @@ -485,10 +485,10 @@ non-anonymised) file to output archive')) self.statusbar.push(0, _('Cleaning %s...') % self.liststore[line][1]) if self.liststore[line][3] != _('Clean (strict)'): if self.force or not self.liststore[line][0].file.is_clean(): - self.liststore[line][0].file.remove_all() + if self.liststore[line][0].file.remove_all(): + self.liststore[line][3] = _('Clean (lossless)') if self.backup: # the backup copy state self.liststore[line][4] = self.liststore[line][0].file.output - self.liststore[line][3] = _('Clean (lossless)') yield True self.statusbar.push(0, _('Ready')) yield False @@ -502,10 +502,10 @@ non-anonymised) file to output archive')) self.statusbar.push(0, _('Cleaning %s...') % self.liststore[line][1]) if self.liststore[line][3] != _('Clean (strict)'): if self.force or not self.liststore[line][0].file.is_clean(): - self.liststore[line][0].file.remove_all_ugly() + if self.liststore[line][0].file.remove_all_ugly(): + self.liststore[line][3] = _('Clean (strict)') if self.backup: # the backup copy state self.liststore[line][4] = self.liststore[line][0].file.output - self.liststore[line][3] = _('Clean (strict)') yield True self.statusbar.push(0, _('Ready')) yield False diff --git a/mat/archive.py b/mat/archive.py index 77db71c..65527d2 100644 --- a/mat/archive.py +++ b/mat/archive.py @@ -39,13 +39,13 @@ class GenericArchiveStripper(parser.GenericParser): ''' Call _remove_all() with in argument : "normal" ''' - self._remove_all('normal') + return self._remove_all('normal') def remove_all_ugly(self): ''' call remove_all() with in argument : "ugly" ''' - self._remove_all('ugly') + return self._remove_all('ugly') def _remove_all(self, method): ''' @@ -161,6 +161,7 @@ harmless format' % item.filename) zipout.close() logging.info('%s treated' % self.filename) self.do_backup() + return True class TarStripper(GenericArchiveStripper): @@ -203,6 +204,7 @@ class TarStripper(GenericArchiveStripper): tarin.close() tarout.close() self.do_backup() + return True def is_file_clean(self, current_file): ''' diff --git a/mat/audio.py b/mat/audio.py index 21a94be..ed849ee 100644 --- a/mat/audio.py +++ b/mat/audio.py @@ -35,6 +35,7 @@ class OggStripper(parser.GenericParser): mfile = OggVorbis(self.filename) mfile.delete() mfile.save() + return True def is_clean(self): ''' @@ -73,6 +74,7 @@ class FlacStripper(parser.GenericParser): mfile.delete() mfile.clear_pictures() mfile.save() + return True def is_clean(self): ''' diff --git a/mat/exiftool.py b/mat/exiftool.py index 5a4ecc9..1d2d116 100644 --- a/mat/exiftool.py +++ b/mat/exiftool.py @@ -30,15 +30,19 @@ class ExiftoolStripper(parser.GenericParser): ''' Remove all metadata with help of exiftool ''' - if self.backup: - process = subprocess.Popen(['exiftool', '-All=', - '-out', self.output, self.filename], - stdout=open('/dev/null')) - process.wait() - else: - process = subprocess.Popen(['exiftool', '-overwrite_original', - '-All=', self.filename], stdout=open('/dev/null')) - process.wait() + try: + if self.backup: + process = subprocess.Popen(['exiftool', '-All=', + '-out', self.output, self.filename], + stdout=open('/dev/null')) + process.wait() + else: + process = subprocess.Popen(['exiftool', '-overwrite_original', + '-All=', self.filename], stdout=open('/dev/null')) + process.wait() + return True + except: + return False def is_clean(self): ''' diff --git a/mat/misc.py b/mat/misc.py index f7b256f..d084861 100644 --- a/mat/misc.py +++ b/mat/misc.py @@ -60,3 +60,4 @@ class TorrentStripper(parser.GenericParser): with open(self.output, 'w') as f: # encode the decoded torrent f.write(bencode.bencode(decoded)) # and write it in self.output self.do_backup() + return True diff --git a/mat/office.py b/mat/office.py index 2d7e9e6..2782318 100644 --- a/mat/office.py +++ b/mat/office.py @@ -91,6 +91,7 @@ class OpenDocumentStripper(archive.GenericArchiveStripper): zipin.close() zipout.close() self.do_backup() + return True def is_clean(self): ''' @@ -128,8 +129,8 @@ class PdfStripper(parser.GenericParser): ''' for key in self.meta_list: if self.document.get_property(key) is not None and \ - self.document.get_property(key) != '': - return False + self.document.get_property(key) != '': + return False return True @@ -137,7 +138,7 @@ class PdfStripper(parser.GenericParser): ''' Remove supperficial ''' - self._remove_meta() + return self._remove_meta() def remove_all_ugly(self): @@ -159,7 +160,7 @@ class PdfStripper(parser.GenericParser): page.render(context) # render the page on context context.show_page() # draw context on surface surface.finish() - self._remove_meta() + return self._remove_meta() def _remove_meta(self): ''' @@ -167,39 +168,40 @@ class PdfStripper(parser.GenericParser): from a pdf file, using exiftool, of pdfrw if exiftool is not installed ''' - processed = False - try: # try with pdfrw - import pdfrw - #For now, poppler cannot write meta, so we must use pdfrw - logging.debug('Removing %s\'s superficial metadata' % self.filename) - trailer = pdfrw.PdfReader(self.output) - trailer.Info.Producer = trailer.Author = trailer.Info.Creator = None - writer = pdfrw.PdfWriter() - writer.trailer = trailer - writer.write(self.output) - self.do_backup() - processed = True - except: - pass + processed = False + try:# try with pdfrw + import pdfrw + #For now, poppler cannot write meta, so we must use pdfrw + logging.debug('Removing %s\'s superficial metadata' % self.filename) + trailer = pdfrw.PdfReader(self.output) + trailer.Info.Producer = trailer.Author = trailer.Info.Creator = None + writer = pdfrw.PdfWriter() + writer.trailer = trailer + writer.write(self.output) + self.do_backup() + processed = True + except: + pass try: # try with exiftool - subprocess.Popen('exiftool', stdout=open('/dev/null')) + subprocess.Popen('exiftool', stdout=open('/dev/null')) import exiftool if self.backup: process = subprocess.Popen(['exiftool', '-All=', - '-out', self.output, self.filename], - stdout=open('/dev/null')) + '-out', self.output, self.filename], stdout=open('/dev/null')) process.wait() else: process = subprocess.Popen(['exiftool', '-overwrite_original', '-All=', self.filename], stdout=open('/dev/null')) process.wait() - processed = True + processed = True except: - pass + pass - if processed is False: - logging.error('Please install either pdfrw, or exiftool') + if processed is False: + logging.error('Please install either pdfrw, or exiftool to\ + fully handle pdf files') + return processed def get_meta(self): ''' @@ -260,6 +262,7 @@ class OpenXmlStripper(archive.GenericArchiveStripper): zipin.close() zipout.close() self.do_backup() + return True def is_clean(self): ''' diff --git a/mat/parser.py b/mat/parser.py index c7c606c..ebab297 100644 --- a/mat/parser.py +++ b/mat/parser.py @@ -66,12 +66,16 @@ class GenericParser(object): self.do_backup() def _remove_all(self, fieldset): - for field in fieldset: - remove = self._should_remove(field) - if remove is True: - self._remove(fieldset, field.name) - if remove is FIELD: - self._remove_all(field) + try: + for field in fieldset: + remove = self._should_remove(field) + if remove is True: + self._remove(fieldset, field.name) + if remove is FIELD: + self._remove_all(field) + return True + except: + return False def remove_all_ugly(self): ''' -- cgit v1.3