diff options
| author | jvoisin | 2014-06-08 13:39:18 +0200 |
|---|---|---|
| committer | jvoisin | 2014-06-08 13:39:18 +0200 |
| commit | af36529554c39a2eefcc2c8723715e2d25b401b8 (patch) | |
| tree | f54b964520bab44d1dfac725086211eaf22d3763 /libmat/mat.py | |
| parent | ef5a32cfd3c0555ffe5ddf413eeaae61622ebb4b (diff) | |
Rename the MAT folder to libmat.
This commit fixes some issues for dump operating
systems who doesn't handle capitalization.
Diffstat (limited to 'libmat/mat.py')
| -rw-r--r-- | libmat/mat.py | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/libmat/mat.py b/libmat/mat.py new file mode 100644 index 0000000..8dfc2dc --- /dev/null +++ b/libmat/mat.py | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | |||
| 3 | ''' Metadata anonymisation toolkit library | ||
| 4 | ''' | ||
| 5 | |||
| 6 | import logging | ||
| 7 | import mimetypes | ||
| 8 | import os | ||
| 9 | import subprocess | ||
| 10 | import xml.sax | ||
| 11 | |||
| 12 | import hachoir_core.cmd_line | ||
| 13 | import hachoir_parser | ||
| 14 | |||
| 15 | import libmat.exceptions | ||
| 16 | |||
| 17 | __version__ = '0.5.2' | ||
| 18 | __author__ = 'jvoisin' | ||
| 19 | |||
| 20 | #Silence | ||
| 21 | LOGGING_LEVEL = logging.CRITICAL | ||
| 22 | hachoir_core.config.quiet = True | ||
| 23 | fname = '' | ||
| 24 | |||
| 25 | #Verbose | ||
| 26 | #LOGGING_LEVEL = logging.DEBUG | ||
| 27 | #hachoir_core.config.quiet = False | ||
| 28 | #logname = 'report.log' | ||
| 29 | |||
| 30 | logging.basicConfig(filename=fname, level=LOGGING_LEVEL) | ||
| 31 | |||
| 32 | import strippers # this is loaded here because we need LOGGING_LEVEL | ||
| 33 | |||
| 34 | |||
| 35 | def get_logo(): | ||
| 36 | ''' Return the path to the logo | ||
| 37 | ''' | ||
| 38 | if os.path.isfile('./data/mat.png'): | ||
| 39 | return './data/mat.png' | ||
| 40 | elif os.path.isfile('/usr/share/pixmaps/mat.png'): | ||
| 41 | return '/usr/share/pixmaps/mat.png' | ||
| 42 | elif os.path.isfile('/usr/local/share/pixmaps/mat.png'): | ||
| 43 | return '/usr/local/share/pixmaps/mat.png' | ||
| 44 | |||
| 45 | |||
| 46 | def get_datadir(): | ||
| 47 | ''' Return the path to the data directory | ||
| 48 | ''' | ||
| 49 | if os.path.isdir('./data/'): | ||
| 50 | return './data/' | ||
| 51 | elif os.path.isdir('/usr/local/share/mat/'): | ||
| 52 | return '/usr/local/share/mat/' | ||
| 53 | elif os.path.isdir('/usr/share/mat/'): | ||
| 54 | return '/usr/share/mat/' | ||
| 55 | |||
| 56 | |||
| 57 | def list_supported_formats(): | ||
| 58 | ''' Return a list of all locally supported fileformat. | ||
| 59 | It parses that FORMATS file, and removes locally | ||
| 60 | non-supported formats. | ||
| 61 | ''' | ||
| 62 | handler = XMLParser() | ||
| 63 | parser = xml.sax.make_parser() | ||
| 64 | parser.setContentHandler(handler) | ||
| 65 | path = os.path.join(get_datadir(), 'FORMATS') | ||
| 66 | with open(path, 'r') as xmlfile: | ||
| 67 | parser.parse(xmlfile) | ||
| 68 | |||
| 69 | localy_supported = [] | ||
| 70 | for item in handler.list: | ||
| 71 | if item['mimetype'].split(',')[0] in strippers.STRIPPERS: | ||
| 72 | localy_supported.append(item) | ||
| 73 | |||
| 74 | return localy_supported | ||
| 75 | |||
| 76 | |||
| 77 | class XMLParser(xml.sax.handler.ContentHandler): | ||
| 78 | ''' Parse the supported format xml, and return a corresponding | ||
| 79 | list of dict | ||
| 80 | ''' | ||
| 81 | def __init__(self): | ||
| 82 | self.dict = {} | ||
| 83 | self.list = [] | ||
| 84 | self.content, self.key = '', '' | ||
| 85 | self.between = False | ||
| 86 | |||
| 87 | def startElement(self, name, attrs): | ||
| 88 | ''' Called when entering into xml tag | ||
| 89 | ''' | ||
| 90 | self.between = True | ||
| 91 | self.key = name | ||
| 92 | self.content = '' | ||
| 93 | |||
| 94 | def endElement(self, name): | ||
| 95 | ''' Called when exiting a xml tag | ||
| 96 | ''' | ||
| 97 | if name == 'format': # leaving a fileformat section | ||
| 98 | self.list.append(self.dict.copy()) | ||
| 99 | self.dict.clear() | ||
| 100 | else: | ||
| 101 | content = self.content.replace('\s', ' ') | ||
| 102 | self.dict[self.key] = content | ||
| 103 | self.between = False | ||
| 104 | |||
| 105 | def characters(self, characters): | ||
| 106 | ''' Concatenate the content between opening and closing tags | ||
| 107 | ''' | ||
| 108 | if self.between: | ||
| 109 | self.content += characters | ||
| 110 | |||
| 111 | |||
| 112 | def secure_remove(filename): | ||
| 113 | ''' Securely remove the file | ||
| 114 | ''' | ||
| 115 | # I want the file removed, even if it's ro | ||
| 116 | try: | ||
| 117 | os.chmod(filename, 220) | ||
| 118 | except OSError: | ||
| 119 | logging.error('Unable to add write rights to %s' % filename) | ||
| 120 | raise libmat.exceptions.UnableToWriteFile | ||
| 121 | |||
| 122 | try: | ||
| 123 | if not subprocess.call(['shred', '--remove', filename]): | ||
| 124 | return True | ||
| 125 | else: | ||
| 126 | raise OSError | ||
| 127 | except OSError: | ||
| 128 | logging.error('Unable to securely remove %s' % filename) | ||
| 129 | |||
| 130 | try: | ||
| 131 | os.remove(filename) | ||
| 132 | except OSError: | ||
| 133 | logging.error('Unable to remove %s' % filename) | ||
| 134 | raise libmat.exceptions.UnableToRemoveFile | ||
| 135 | |||
| 136 | return True | ||
| 137 | |||
| 138 | |||
| 139 | def create_class_file(name, backup, **kwargs): | ||
| 140 | ''' Return a $FILETYPEStripper() class, | ||
| 141 | corresponding to the filetype of the given file | ||
| 142 | ''' | ||
| 143 | if not os.path.isfile(name): # check if the file exists | ||
| 144 | logging.error('%s is not a valid file' % name) | ||
| 145 | return None | ||
| 146 | |||
| 147 | if not os.access(name, os.R_OK): # check read permissions | ||
| 148 | logging.error('%s is is not readable' % name) | ||
| 149 | return None | ||
| 150 | |||
| 151 | if not os.path.getsize(name): | ||
| 152 | #check if the file is not empty (hachoir crash on empty files) | ||
| 153 | logging.error('%s is empty' % name) | ||
| 154 | return None | ||
| 155 | |||
| 156 | filename = '' | ||
| 157 | try: | ||
| 158 | filename = hachoir_core.cmd_line.unicodeFilename(name) | ||
| 159 | except TypeError: # get rid of "decoding Unicode is not supported" | ||
| 160 | filename = name | ||
| 161 | |||
| 162 | parser = hachoir_parser.createParser(filename) | ||
| 163 | if not parser: | ||
| 164 | logging.info('Unable to parse %s' % filename) | ||
| 165 | return None | ||
| 166 | |||
| 167 | mime = parser.mime_type | ||
| 168 | |||
| 169 | if mime == 'application/zip': # some formats are zipped stuff | ||
| 170 | if mimetypes.guess_type(name)[0]: | ||
| 171 | mime = mimetypes.guess_type(name)[0] | ||
| 172 | |||
| 173 | if mime.startswith('application/vnd.oasis.opendocument'): | ||
| 174 | mime = 'application/opendocument' # opendocument fileformat | ||
| 175 | elif mime.startswith('application/vnd.openxmlformats-officedocument'): | ||
| 176 | mime = 'application/officeopenxml' # office openxml | ||
| 177 | |||
| 178 | is_writable = os.access(name, os.W_OK) | ||
| 179 | |||
| 180 | try: | ||
| 181 | stripper_class = strippers.STRIPPERS[mime] | ||
| 182 | except KeyError: | ||
| 183 | logging.info('Don\'t have stripper for %s format' % mime) | ||
| 184 | return None | ||
| 185 | |||
| 186 | return stripper_class(filename, parser, mime, backup, is_writable, **kwargs) | ||
