summaryrefslogtreecommitdiffstats
path: root/tools/agptek_rocker/update_update.py
blob: 4fb7056d1961f9ae4f54fb751386558522b53b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import os
import sys
import hashlib
import argparse
from collections import OrderedDict

EQ = '='
BLK_START = '{'
BLK_END = '}'

#name: {key: value, key: value}
def parse(filename):
    blocks = OrderedDict()
    blk = None
    key = None
    value = None

    with open(filename) as f:
        # read all lines
        for line in f:
            # if line has '=' sign treat it
            # as split symbol
            if EQ in line:
                key, value = line.split(EQ, 1)
                key = key.strip()
                value = value.strip()

                if value == BLK_START:
                    # value on the right of '=' is '{'
                    # so this opens new block
                    blk = key
                    blocks[key] = OrderedDict()

                elif value == BLK_END:
                    # value on the right of '=' is '}'
                    # this terminates block
                    blk = None

                else:
                    # key = value inside block
                    blocks[blk][key] = value

    # return parsed structure as dictionary
    return blocks

# write back internal representation into file
def dump(tree, filename=None):
    with open(filename, 'w') as f:
        for blk in tree.keys():
            f.write('\n%s={\n' % blk)
            for key,value in tree[blk].items():
                f.write('\t%s=%s\n' % (key,value))
            f.write('}\n')

if __name__=='__main__':
    description = 'Update information in update.txt control file.'
    usage = 'update_update.py filepath'

    argp = argparse.ArgumentParser(usage=usage, description=description)
    argp.add_argument('filepath', help='update.txt filepath to update contents of.')

    if len(sys.argv) == 1:
        argp.print_help()
        sys.exit(1)

    args = argp.parse_args()

    # build config file representation
    tree = parse(args.filepath)

    dir = os.path.dirname(args.filepath)

    # update all md5 sums
    for blk in tree.keys():
        filename = os.path.join(dir, os.path.basename(tree[blk]['file_path']))
        with open(filename) as f:
            tree[blk]['md5'] = hashlib.md5(f.read()).hexdigest()

    # write back result
    dump(tree, args.filepath)