summaryrefslogtreecommitdiff
path: root/MAT/mat.py
diff options
context:
space:
mode:
Diffstat (limited to 'MAT/mat.py')
-rw-r--r--MAT/mat.py186
1 files changed, 0 insertions, 186 deletions
diff --git a/MAT/mat.py b/MAT/mat.py
deleted file mode 100644
index 5b1fbda..0000000
--- a/MAT/mat.py
+++ /dev/null
@@ -1,186 +0,0 @@
1#!/usr/bin/env python
2
3''' Metadata anonymisation toolkit library
4'''
5
6import logging
7import mimetypes
8import os
9import subprocess
10import xml.sax
11
12import hachoir_core.cmd_line
13import hachoir_parser
14
15import MAT.exceptions
16
17__version__ = '0.5.2'
18__author__ = 'jvoisin'
19
20#Silence
21LOGGING_LEVEL = logging.CRITICAL
22hachoir_core.config.quiet = True
23fname = ''
24
25#Verbose
26#LOGGING_LEVEL = logging.DEBUG
27#hachoir_core.config.quiet = False
28#logname = 'report.log'
29
30logging.basicConfig(filename=fname, level=LOGGING_LEVEL)
31
32import strippers # this is loaded here because we need LOGGING_LEVEL
33
34
35def 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
46def 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
57def 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
77class 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
112def 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 MAT.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 MAT.exceptions.UnableToRemoveFile
135
136 return True
137
138
139def 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)