|
4 | 4 | import sys |
5 | 5 | import os |
6 | 6 | import shutil |
| 7 | +import argparse |
7 | 8 | import maec |
8 | 9 | from maec.bundle.bundle import Bundle |
9 | 10 | from maec.package.package import Package |
@@ -47,41 +48,42 @@ def usage(): |
47 | 48 | """ |
48 | 49 |
|
49 | 50 | def main(): |
50 | | - infilename = None |
51 | | - outfilename = None |
52 | | - directoryname = '' |
53 | | - filepath = '' |
54 | | - |
55 | | - #Get the command-line arguments |
56 | | - args = sys.argv[1:] |
57 | | - |
58 | | - if len(args) < 2: |
59 | | - usage() |
60 | | - sys.exit(1) |
61 | | - |
62 | | - for i in range(0,len(args)): |
63 | | - if args[i] == '-i': |
64 | | - infilename = args[i+1] |
65 | | - elif args[i] == '-o': |
66 | | - outfilename = args[i+1] |
67 | | - elif args[i] == '-d': |
68 | | - directoryname = args[i+1] |
| 51 | + # Setup the argument parser |
| 52 | + parser = argparse.ArgumentParser( |
| 53 | + description='MAEC 4.0.1 --> MAEC 4.1 XML Converter Utility' |
| 54 | + ) |
| 55 | + mutex_group = parser.add_mutually_exclusive_group() |
| 56 | + required_name = parser.add_argument_group('required arguments') |
| 57 | + mutex_group.add_argument( |
| 58 | + '--input', '-i', |
| 59 | + help='input maec 4.0.1 xml file' |
| 60 | + ) |
| 61 | + mutex_group.add_argument( |
| 62 | + '--directory', '-d', |
| 63 | + help='directory containing maec 4.0.1 xml files to convert to 4.1 xml files' |
| 64 | + ) |
| 65 | + required_name.add_argument( |
| 66 | + '--output', '-o', required=True, |
| 67 | + help='output maec 4.1 xml file' |
| 68 | + ) |
| 69 | + |
| 70 | + args = parser.parse_args() |
69 | 71 |
|
70 | | - if directoryname != '': |
71 | | - for filename in os.listdir(directoryname): |
| 72 | + if args.directory: |
| 73 | + for filename in os.listdir(args.directory): |
72 | 74 | print filename |
73 | 75 | if '.xml' not in filename: |
74 | 76 | pass |
75 | 77 | elif '_report.maec-4.0.1' not in filename: |
76 | | - update_maec(os.path.join(directoryname, filename), filename.rstrip('.xml') + '_cuckoobox_maec.xml') |
| 78 | + update_maec(os.path.join(args.directory, filename), filename.rstrip('.xml') + '_cuckoobox_maec.xml') |
77 | 79 | else: |
78 | | - new_filepath = os.path.join(directoryname, filename.replace('_report.maec-4.0.1', '')) |
79 | | - shutil.move(os.path.join(directoryname, filename), new_filepath) |
| 80 | + new_filepath = os.path.join(args.directory, filename.replace('_report.maec-4.0.1', '')) |
| 81 | + shutil.move(os.path.join(args.directory, filename), new_filepath) |
80 | 82 | update_maec(new_filepath, new_filepath.rstrip('.xml') + '_cuckoobox_maec.xml') |
81 | 83 |
|
82 | 84 | # Basic parameter checking |
83 | | - elif infilename and outfilename: |
84 | | - update_maec(infilename, outfilename) |
| 85 | + elif args.input and args.output: |
| 86 | + update_maec(args.input, args.output) |
85 | 87 |
|
86 | 88 | if __name__ == "__main__": |
87 | 89 | main() |
|
0 commit comments