diff options
| author | jvoisin | 2011-08-16 18:43:58 +0200 |
|---|---|---|
| committer | jvoisin | 2011-08-16 18:43:58 +0200 |
| commit | 9f8dab3209e42a7b69a8a7a1ae100d707a4fc558 (patch) | |
| tree | 3170b8474d858eacc25d0df58f3e8535c8432bac /cli.py | |
| parent | 7cdd87a2eeea35aa4fa5fccb7e0ee31a88d8611a (diff) | |
Change test according to previous commit
Diffstat (limited to 'cli.py')
| -rwxr-xr-x | cli.py | 152 |
1 files changed, 0 insertions, 152 deletions
| @@ -1,152 +0,0 @@ | |||
| 1 | #!/usr/bin/python | ||
| 2 | ''' | ||
| 3 | Metadata anonymisation toolkit - CLI edition | ||
| 4 | ''' | ||
| 5 | |||
| 6 | import sys | ||
| 7 | import xml.sax | ||
| 8 | import optparse | ||
| 9 | |||
| 10 | import hachoir_core | ||
| 11 | |||
| 12 | from mat import mat | ||
| 13 | |||
| 14 | __version__ = '0.1' | ||
| 15 | |||
| 16 | |||
| 17 | def parse(): | ||
| 18 | ''' | ||
| 19 | Get, and parse options passed to the program | ||
| 20 | ''' | ||
| 21 | parser = optparse.OptionParser(usage='%prog [options] files') | ||
| 22 | parser.add_option('--add2archive', '-a', action='store_true', | ||
| 23 | default=False, help='Add to outputed archive non-supported filetypes') | ||
| 24 | parser.add_option('--backup', '-b', action='store_true', default=False, | ||
| 25 | help='Keep a backup copy') | ||
| 26 | parser.add_option('--check', '-c', action='store_true', default=False, | ||
| 27 | help='Check if a file is free of harmfull metadatas') | ||
| 28 | parser.add_option('--display', '-d', action='store_true', default=False, | ||
| 29 | help='List all the meta of a file without removing them') | ||
| 30 | parser.add_option('--force', '-f', action='store_true', default=False, | ||
| 31 | help='Don\'t check if files are clean before cleaning') | ||
| 32 | parser.add_option('--list', '-l', action='store_true', default=False, | ||
| 33 | help='List all supported fileformat') | ||
| 34 | parser.add_option('--ugly', '-u', action='store_true', default=False, | ||
| 35 | help='Remove harmful meta, but loss can occure') | ||
| 36 | parser.add_option('--version', '-v', action='callback', | ||
| 37 | callback=display_version, help='Display version and exit') | ||
| 38 | |||
| 39 | values, arguments = parser.parse_args() | ||
| 40 | if not arguments and values.list is False: | ||
| 41 | parser.print_help() | ||
| 42 | sys.exit(0) | ||
| 43 | return values, arguments | ||
| 44 | |||
| 45 | |||
| 46 | def display_version(*_): | ||
| 47 | ''' | ||
| 48 | Display the program's version, and exit | ||
| 49 | ''' | ||
| 50 | print('Metadata Anonymisation Toolkit version %s') % mat.__version__ | ||
| 51 | print('CLI version %s') % __version__ | ||
| 52 | print('Hachoir version %s') % hachoir_core.__version__ | ||
| 53 | sys.exit(0) | ||
| 54 | |||
| 55 | |||
| 56 | def list_meta(class_file, filename, force): | ||
| 57 | ''' | ||
| 58 | Print all the meta of 'filename' on stdout | ||
| 59 | ''' | ||
| 60 | print('[+] File %s :' % filename) | ||
| 61 | if force is False and class_file.is_clean(): | ||
| 62 | print('No harmful meta found') | ||
| 63 | else: | ||
| 64 | meta = class_file.get_meta() | ||
| 65 | if meta is None: | ||
| 66 | print('No harmful meta found') | ||
| 67 | else: | ||
| 68 | for key, value in class_file.get_meta().iteritems(): | ||
| 69 | print(key + ' : ' + str(value)) | ||
| 70 | |||
| 71 | |||
| 72 | def is_clean(class_file, filename, force): | ||
| 73 | ''' | ||
| 74 | Say if 'filename' is clean or not | ||
| 75 | ''' | ||
| 76 | if class_file.is_clean(): | ||
| 77 | print('[+] %s is clean' % filename) | ||
| 78 | else: | ||
| 79 | print('[+] %s is not clean' % filename) | ||
| 80 | |||
| 81 | |||
| 82 | def clean_meta(class_file, filename, force): | ||
| 83 | ''' | ||
| 84 | Clean the file 'filename' | ||
| 85 | ''' | ||
| 86 | print('[+] Cleaning %s' % filename) | ||
| 87 | if force is False and class_file.is_clean(): | ||
| 88 | print('%s is already clean' % filename) | ||
| 89 | else: | ||
| 90 | class_file.remove_all() | ||
| 91 | print('%s cleaned !' % filename) | ||
| 92 | |||
| 93 | |||
| 94 | def clean_meta_ugly(class_file, filename, force): | ||
| 95 | ''' | ||
| 96 | Clean the file 'filename', ugly way | ||
| 97 | ''' | ||
| 98 | print('[+] Cleaning %s' % filename) | ||
| 99 | if force is False and class_file.is_clean(): | ||
| 100 | print('%s is already clean' % filename) | ||
| 101 | else: | ||
| 102 | class_file.remove_all_ugly() | ||
| 103 | print('%s cleaned' % filename) | ||
| 104 | |||
| 105 | |||
| 106 | def list_supported(): | ||
| 107 | ''' | ||
| 108 | Print all supported fileformat, and exit | ||
| 109 | ''' | ||
| 110 | handler = mat.XMLParser() | ||
| 111 | parser = xml.sax.make_parser() | ||
| 112 | parser.setContentHandler(handler) | ||
| 113 | with open('FORMATS', 'r') as xmlfile: | ||
| 114 | parser.parse(xmlfile) | ||
| 115 | |||
| 116 | for item in handler.list: | ||
| 117 | print('%s (%s)' % (item['name'], item['extension'])) | ||
| 118 | print('\tsupport : ' + item['support']) | ||
| 119 | print('\tmetadata : ' + item['metadata']) | ||
| 120 | print('\tmethod : ' + item['method']) | ||
| 121 | if item['support'] == 'partial': | ||
| 122 | print('\tremaining : ' + item['remaining']) | ||
| 123 | print('\n') | ||
| 124 | sys.exit(0) | ||
| 125 | |||
| 126 | |||
| 127 | def main(): | ||
| 128 | ''' | ||
| 129 | main function : get args, and launch the appropriate function | ||
| 130 | ''' | ||
| 131 | args, filenames = parse() | ||
| 132 | |||
| 133 | #func receive the function correponding to the options given as parameters | ||
| 134 | if args.display is True: # only print metadatas | ||
| 135 | func = list_meta | ||
| 136 | elif args.check is True: # only check if the file is clean | ||
| 137 | func = is_clean | ||
| 138 | elif args.ugly is True: # destructive anonymisation method | ||
| 139 | func = clean_meta_ugly | ||
| 140 | elif args.list is True: # print the list of all supported format | ||
| 141 | list_supported() | ||
| 142 | else: # clean the file | ||
| 143 | func = clean_meta | ||
| 144 | |||
| 145 | for filename in filenames: | ||
| 146 | class_file = mat.create_class_file(filename, args.backup, | ||
| 147 | args.add2archive) | ||
| 148 | if class_file is not None: | ||
| 149 | func(class_file, filename, args.force) | ||
| 150 | |||
| 151 | if __name__ == '__main__': | ||
| 152 | main() | ||
