summaryrefslogtreecommitdiff
path: root/cli.py
diff options
context:
space:
mode:
authorjvoisin2011-06-14 22:00:10 +0200
committerjvoisin2011-06-14 22:00:10 +0200
commit1583a58289318e612e099d47bdb53880ee877a21 (patch)
tree28bfad62396f370c0a3224f45a7be39e31c7a85c /cli.py
parentc308cf7daaa4fa46377e2df0f2e9a397981e19b2 (diff)
Use getopt (python2.3) instead of argparse (2.7)
Getopt is deprecated in 2.7, but debian is in 2.6
Diffstat (limited to 'cli.py')
-rw-r--r--cli.py81
1 files changed, 39 insertions, 42 deletions
diff --git a/cli.py b/cli.py
index fedb40c..14a848d 100644
--- a/cli.py
+++ b/cli.py
@@ -1,84 +1,81 @@
1#!/usr/bin/python 1#!/usr/bin/python
2""" 2'''
3 Metadata anonymisation toolkit 3 Metadata anonymisation toolkit
4""" 4'''
5 5
6import sys 6import sys
7import mat 7import mat
8import argparse 8import optparse
9 9
10__version__ = "0.1" 10__version__ = '0.1'
11 11
12def parsing(): 12def parse():
13 ''' 13 parser = optparse.OptionParser(usage='%prog [options] filename')
14 Parse the arguments, 14 common = optparse.OptionGroup(parser, 'Metadata Anonymisation Toolkit - CLI')
15 and returns a dict 15 common.add_option('--display', '-d', action='store_true', default=False,
16 ''' 16 help='List all the meta of a file without removing them')
17 parser = argparse.ArgumentParser(version=__version__, 17 common.add_option('--check', '-c', action='store_true', default=False,
18 description="Metadata Anonymisation Toolkit - CLI %s" % __version__) 18 help='Check if a file is free of harmfull metadatas')
19 19 common.add_option('--version', action='callback', callback=displayVersion,
20 #list and check clean are mutually exclusives 20 help='Display version and exit')
21 group = parser.add_mutually_exclusive_group()
22
23 #list meta
24 group.add_argument('--print-meta', '-p', action="store_true", default=False,
25 dest='just_list', help='List all the meta of a file,\
26 without removing them')
27 21
28 #check if the file is clean 22 parser.add_option_group(common)
29 group.add_argument('--check-clean', '-c', action="store_true",
30 default=False, dest='just_check',
31 help='Check if a file is clean of harmfull metadatas')
32 23
33 #list of files to process 24 values, arguments = parser.parse_args()
34 parser.add_argument('filelist', action="store", type=str, nargs="+", 25 if not arguments:
35 metavar='file', help='File(s) to process') 26 parser.print_help()
27 sys.exit(1)
28 return values, arguments
36 29
37 return parser.parse_args() 30def displayVersion():
31 print('Metadata Anonymisation Toolkit - CLI %s') % __version__
32 print('Hachoir library version %s') % hachoir_core.__version__
33 sys.exit(0)
38 34
39def list_meta(class_file, filename): 35def list_meta(class_file, filename):
40 ''' 36 '''
41 Print all the meta of "filename" on stdout 37 Print all the meta of 'filename' on stdout
42 ''' 38 '''
43 print("[+] File %s :" % filename) 39 print('[+] File %s :' % filename)
44 for key, item in class_file.get_meta().iteritems(): 40 for key, item in class_file.get_meta().iteritems():
45 print("\t%s : %s" % (key, item) ) 41 print('\t%s : %s' % (key, item) )
46 42
47def is_clean(class_file, filename): 43def is_clean(class_file, filename):
48 ''' 44 '''
49 Say if "filename" is clean or not 45 Say if 'filename' is clean or not
50 ''' 46 '''
51 if class_file.is_clean(): 47 if class_file.is_clean():
52 print("[+] %s is clean" % filename) 48 print('[+] %s is clean' % filename)
53 else: 49 else:
54 print("[+] %s is not clean" % filename) 50 print('[+] %s is not clean' % filename)
55 51
56def clean_meta(class_file, filename): 52def clean_meta(class_file, filename):
57 ''' 53 '''
58 Clean the file "filename" 54 Clean the file 'filename'
59 ''' 55 '''
60 print("[+] Cleaning %s" % filename) 56 print('[+] Cleaning %s' % filename)
61 if class_file.is_clean(): 57 if class_file.is_clean():
62 print("%s is already clean" % filename) 58 print('%s is already clean' % filename)
63 else: 59 else:
64 class_file.remove_all() 60 class_file.remove_all()
65 print("%s cleaned !" % filename) 61 print('%s cleaned !' % filename)
66 62
67def main(): 63def main():
68 args = parsing() 64 args, filenames = parse()
65 #args = parsing()
69 66
70 #func receive the function correponding to the options given as parameters 67 #func receive the function correponding to the options given as parameters
71 if args.just_list is True: #only print metadatas 68 if args.display is True: #only print metadatas
72 func = list_meta 69 func = list_meta
73 elif args.just_check is True: #only check if the file is clean 70 elif args.check is True: #only check if the file is clean
74 func = is_clean 71 func = is_clean
75 else: #clean the file 72 else: #clean the file
76 func = clean_meta 73 func = clean_meta
77 74
78 for filename in args.filelist: 75 for filename in filenames:
79 class_file = mat.create_class_file(filename) 76 class_file = mat.create_class_file(filename)
80 func(class_file, filename) 77 func(class_file, filename)
81 print("\n") 78 print('\n')
82 79
83if __name__ == '__main__': 80if __name__ == '__main__':
84 main() 81 main()