|
4 | 4 | # Attempts to merge related Malware Subjects |
5 | 5 | import sys |
6 | 6 | import os |
| 7 | +import argparse |
7 | 8 | import maec |
8 | 9 | from maec.utils.merge import merge_documents |
9 | 10 |
|
10 | 11 | USAGE_TEXT = """ |
11 | 12 | MAEC Package Merge Script v0.10 BETA |
12 | 13 | *Merges two or more MAEC Package XML documents |
13 | 14 | *Attempts to merge related (e.g., same MD5 hash) Malware Subjects |
14 | | -
|
15 | | -Usage: python merge_packages.py -o <output file name> -l <single whitespace separated list of MAEC Package files> OR -d <directory name> |
16 | 15 | """ |
17 | 16 |
|
18 | 17 | def main(): |
19 | | - infilenames = [] |
20 | | - list_mode = False |
21 | | - directoryname = '' |
22 | | - outfilename = '' |
23 | | - |
24 | | - #Get the command-line arguments |
25 | | - args = sys.argv[1:] |
26 | | - |
27 | | - if len(args) < 3: |
28 | | - print USAGE_TEXT |
29 | | - sys.exit(1) |
30 | | - |
31 | | - for i in range(0,len(args)): |
32 | | - if args[i] == '-o': |
33 | | - outfilename = args[i+1] |
34 | | - elif args[i] == '-l': |
35 | | - list_mode = True |
36 | | - elif args[i] == '-d': |
37 | | - directoryname = args[i+1] |
38 | | - |
39 | | - if outfilename == '': |
40 | | - print USAGE_TEXT |
41 | | - sys.exit(1) |
| 18 | + parser = argparse.ArgumentParser(description=USAGE_TEXT) |
| 19 | + mutex_group = parser.add_mutually_exclusive_group() |
| 20 | + required_group = parser.add_argument_group('required arguments') |
| 21 | + mutex_group.add_argument( |
| 22 | + '-l', '--list', nargs='+', |
| 23 | + help='single whitespace separated list of MAEC Package files' |
| 24 | + ) |
| 25 | + mutex_group.add_argument( |
| 26 | + '-d', '--directory', |
| 27 | + help='directory name' |
| 28 | + ) |
| 29 | + required_group.add_argument( |
| 30 | + '-o', '--output', required=True, |
| 31 | + help='output file name' |
| 32 | + ) |
| 33 | + args = parser.parse_args() |
42 | 34 |
|
43 | 35 | sys.stdout.write("Merging...") |
44 | 36 | # Get the list of input files and perform the merge operation |
45 | | - if list_mode: |
46 | | - files = args[3:] |
47 | | - merge_documents(files, outfilename) |
48 | | - elif directoryname != '': |
| 37 | + if args.list: |
| 38 | + merge_documents(args.list, args.output) |
| 39 | + elif args.directory: |
49 | 40 | file_list = [] |
50 | | - for filename in os.listdir(directoryname): |
| 41 | + for filename in os.listdir(args.directory): |
51 | 42 | if '.xml' not in filename: |
52 | 43 | pass |
53 | 44 | else: |
54 | | - file_list.append(os.path.join(directoryname, filename)) |
55 | | - merge_documents(file_list, outfilename) |
| 45 | + file_list.append(os.path.join(args.directory, filename)) |
| 46 | + merge_documents(file_list, args.output) |
56 | 47 | sys.stdout.write("Done.") |
57 | 48 |
|
58 | 49 | if __name__ == "__main__": |
|
0 commit comments