diff options
Diffstat (limited to 'tools/agptek_rocker/update_update.py')
-rw-r--r-- | tools/agptek_rocker/update_update.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tools/agptek_rocker/update_update.py b/tools/agptek_rocker/update_update.py new file mode 100644 index 0000000000..4fb7056d19 --- /dev/null +++ b/tools/agptek_rocker/update_update.py | |||
@@ -0,0 +1,81 @@ | |||
1 | #!/usr/bin/env python | ||
2 | import os | ||
3 | import sys | ||
4 | import hashlib | ||
5 | import argparse | ||
6 | from collections import OrderedDict | ||
7 | |||
8 | EQ = '=' | ||
9 | BLK_START = '{' | ||
10 | BLK_END = '}' | ||
11 | |||
12 | #name: {key: value, key: value} | ||
13 | def parse(filename): | ||
14 | blocks = OrderedDict() | ||
15 | blk = None | ||
16 | key = None | ||
17 | value = None | ||
18 | |||
19 | with open(filename) as f: | ||
20 | # read all lines | ||
21 | for line in f: | ||
22 | # if line has '=' sign treat it | ||
23 | # as split symbol | ||
24 | if EQ in line: | ||
25 | key, value = line.split(EQ, 1) | ||
26 | key = key.strip() | ||
27 | value = value.strip() | ||
28 | |||
29 | if value == BLK_START: | ||
30 | # value on the right of '=' is '{' | ||
31 | # so this opens new block | ||
32 | blk = key | ||
33 | blocks[key] = OrderedDict() | ||
34 | |||
35 | elif value == BLK_END: | ||
36 | # value on the right of '=' is '}' | ||
37 | # this terminates block | ||
38 | blk = None | ||
39 | |||
40 | else: | ||
41 | # key = value inside block | ||
42 | blocks[blk][key] = value | ||
43 | |||
44 | # return parsed structure as dictionary | ||
45 | return blocks | ||
46 | |||
47 | # write back internal representation into file | ||
48 | def dump(tree, filename=None): | ||
49 | with open(filename, 'w') as f: | ||
50 | for blk in tree.keys(): | ||
51 | f.write('\n%s={\n' % blk) | ||
52 | for key,value in tree[blk].items(): | ||
53 | f.write('\t%s=%s\n' % (key,value)) | ||
54 | f.write('}\n') | ||
55 | |||
56 | if __name__=='__main__': | ||
57 | description = 'Update information in update.txt control file.' | ||
58 | usage = 'update_update.py filepath' | ||
59 | |||
60 | argp = argparse.ArgumentParser(usage=usage, description=description) | ||
61 | argp.add_argument('filepath', help='update.txt filepath to update contents of.') | ||
62 | |||
63 | if len(sys.argv) == 1: | ||
64 | argp.print_help() | ||
65 | sys.exit(1) | ||
66 | |||
67 | args = argp.parse_args() | ||
68 | |||
69 | # build config file representation | ||
70 | tree = parse(args.filepath) | ||
71 | |||
72 | dir = os.path.dirname(args.filepath) | ||
73 | |||
74 | # update all md5 sums | ||
75 | for blk in tree.keys(): | ||
76 | filename = os.path.join(dir, os.path.basename(tree[blk]['file_path'])) | ||
77 | with open(filename) as f: | ||
78 | tree[blk]['md5'] = hashlib.md5(f.read()).hexdigest() | ||
79 | |||
80 | # write back result | ||
81 | dump(tree, args.filepath) | ||