Commit | Line | Data |
---|---|---|
9926ec7f PP |
1 | import argparse |
2 | import shutil | |
3 | import sys | |
4 | import os | |
5 | ||
6 | ||
2b6ee9ec | 7 | class MetadataTestAssistant: |
cc0ca792 PP |
8 | BASIC_PROLOGUE = \ |
9 | '''/* CTF 1.8 */ | |
10 | ||
11 | typealias integer { size = 8; align = 8; signed = false; base = 10; } := uint8_t; | |
12 | typealias integer { size = 32; align = 8; signed = false; base = hex; } := uint32_t; | |
13 | ||
14 | trace { | |
15 | major = 0; | |
16 | minor = 0; | |
17 | uuid = "2a6422d0-6cee-11e0-8c08-cb07d7b3a564"; | |
18 | byte_order = le; | |
19 | packet.header := struct { | |
20 | uint32_t magic; | |
21 | uint8_t uuid[16]; | |
22 | }; | |
23 | }; | |
24 | ||
25 | ''' | |
26 | ||
9926ec7f PP |
27 | def __init__(self): |
28 | self._actions = { | |
29 | 'prepare': self._prepare, | |
30 | 'clean': self._clean, | |
31 | } | |
32 | ||
33 | @staticmethod | |
34 | def _perror(str): | |
35 | print('Error: {}'.format(str), file=sys.stderr) | |
36 | sys.exit(1) | |
37 | ||
38 | def _parse_args(self): | |
39 | ap = argparse.ArgumentParser() | |
40 | ||
41 | ap.add_argument('-s', '--size', action='store', metavar='SIZE', | |
42 | type=int, required=True, help='size') | |
43 | ap.add_argument('action', metavar='ACTION', action='store', | |
44 | help='action') | |
45 | ||
46 | # parse args | |
47 | args = ap.parse_args() | |
48 | ||
49 | # validate size | |
50 | if args.size < 1: | |
2b6ee9ec | 51 | MetadataTestAssistant._perror('wrong size: {}'.format(args.size)) |
9926ec7f PP |
52 | |
53 | return args | |
54 | ||
55 | def _get_what(self): | |
56 | return self.what.format(size=self.size) | |
57 | ||
58 | def _prepare(self): | |
59 | # make sure everything is clean first | |
60 | self._clean() | |
61 | ||
62 | print('Preparing test for {}'.format(self._get_what())) | |
63 | ||
64 | # make test directory | |
65 | os.mkdir(self._trace_dir_path) | |
66 | ||
67 | # open and write metadata file | |
68 | with open(self._metadata_path, 'w') as f: | |
69 | self.write_metadata(f) | |
70 | ||
71 | def _clean(self): | |
72 | print('Cleaning up test for {}'.format(self._get_what())) | |
73 | ||
74 | try: | |
75 | shutil.rmtree(self._trace_dir_path, ignore_errors=True) | |
76 | except Exception as e: | |
77 | # ignore | |
78 | pass | |
79 | ||
80 | def _do_action(self): | |
81 | if self._action not in self._actions: | |
2ffbd362 | 82 | msg = 'invalid action: "{}"'.format(self._action) |
2b6ee9ec | 83 | MetadataTestAssistant._perror(msg) |
9926ec7f PP |
84 | |
85 | self._actions[self._action]() | |
86 | ||
87 | @property | |
88 | def size(self): | |
89 | return self._size | |
90 | ||
91 | def main(self): | |
92 | args = self._parse_args() | |
93 | self._size = args.size | |
94 | self._trace_dir_path = 'trace-{}'.format(args.size) | |
95 | self._metadata_path = os.path.join(self._trace_dir_path, 'metadata') | |
96 | self._action = args.action | |
97 | ||
98 | self._do_action() |