1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | A program to update existing *.lic file checksum values in a given |
---|
5 | directory tree. |
---|
6 | |
---|
7 | Mask of files to process may be specified (defaults to '*.lic'). |
---|
8 | |
---|
9 | Always recurses down into sub-directories. |
---|
10 | ''' |
---|
11 | |
---|
12 | |
---|
13 | import sys |
---|
14 | import os |
---|
15 | import getopt |
---|
16 | import fnmatch |
---|
17 | import xml.etree.cElementTree as etree |
---|
18 | |
---|
19 | try: |
---|
20 | from anuga.utilities.system_tools import compute_checksum |
---|
21 | except ImportError: |
---|
22 | print("Can't import 'anuga.utilities.system_tools'?") |
---|
23 | print('Please ensure PYTHONPATH environment variable is correct.') |
---|
24 | print("It's currently: %s" % os.getenv('PYTHONPATH')) |
---|
25 | sys.exit(10) |
---|
26 | |
---|
27 | |
---|
28 | # default XML encoding |
---|
29 | DefaultEncoding = 'iso-8859-1' |
---|
30 | |
---|
31 | # verbose flag |
---|
32 | Verbose = False |
---|
33 | |
---|
34 | |
---|
35 | def process_file(dirname, fname): |
---|
36 | '''Update checksum in a licence file.''' |
---|
37 | |
---|
38 | # read in the licence file (XML) |
---|
39 | lic_tree = etree.ElementTree() |
---|
40 | input_filename = os.path.join(dirname, fname) |
---|
41 | lic_tree.parse(input_filename) |
---|
42 | |
---|
43 | if Verbose: |
---|
44 | print('Updating %s' % input_filename) |
---|
45 | |
---|
46 | # process every <datafile> tagset - update checksum |
---|
47 | datafiles = lic_tree.getiterator('datafile') |
---|
48 | for f in datafiles: |
---|
49 | # get 'filename' element text |
---|
50 | filename = f.find('filename') |
---|
51 | if filename is None: |
---|
52 | msg = ('Badly formed licence XML in file %s:\n%s' |
---|
53 | % (input_filename, etree.dump(f))) |
---|
54 | raise RuntimeError, msg |
---|
55 | |
---|
56 | # get 'checksum' element text |
---|
57 | checksum = f.find('checksum') |
---|
58 | if checksum is None: |
---|
59 | msg = ('Badly formed licence XML in file %s:\n%s' |
---|
60 | % (input_filename, etree.dump(f))) |
---|
61 | raise RuntimeError, msg |
---|
62 | |
---|
63 | # update the 'checksum' element from the file |
---|
64 | controlled_filename = os.path.join(dirname, filename.text) |
---|
65 | if (not os.path.exists(controlled_filename) or |
---|
66 | not os.path.isfile(controlled_filename)): |
---|
67 | print('?' * 80) |
---|
68 | print("Licence file %s\nrefers to %s\nwhich doesn't exist!?" |
---|
69 | % (input_filename, controlled_filename)) |
---|
70 | print('?' * 80 + '\n') |
---|
71 | continue |
---|
72 | new_checksum = compute_checksum(controlled_filename) |
---|
73 | checksum.text = str(new_checksum) |
---|
74 | |
---|
75 | # write updated XML data back to file |
---|
76 | new_fname = os.path.join(dirname, fname+'._NEW_') |
---|
77 | lic_tree.write(new_fname, DefaultEncoding) |
---|
78 | os.rename(new_fname, input_filename) |
---|
79 | |
---|
80 | |
---|
81 | def process_one_dir(mask, dirname, names): |
---|
82 | '''Update all licence files in directory that match 'mask'.''' |
---|
83 | |
---|
84 | for fname in names: |
---|
85 | if fnmatch.fnmatch(fname, mask): |
---|
86 | process_file(dirname, fname) |
---|
87 | |
---|
88 | |
---|
89 | def usage(msg=None): |
---|
90 | '''Print helpful usage one-liner.''' |
---|
91 | |
---|
92 | if msg: |
---|
93 | print(msg) |
---|
94 | |
---|
95 | print('usage: update_lic_checksum [-m <lic_mask>] <directory>') |
---|
96 | |
---|
97 | |
---|
98 | def main(argv): |
---|
99 | '''Main function - parse args and start the process.''' |
---|
100 | |
---|
101 | global Verbose |
---|
102 | |
---|
103 | # parse command line args |
---|
104 | try: |
---|
105 | opts, args = getopt.gnu_getopt(argv, 'hm:v', |
---|
106 | ['help', 'mask=', 'verbose']) |
---|
107 | except: |
---|
108 | usage() |
---|
109 | return 10 |
---|
110 | |
---|
111 | file_mask = '*.lic' |
---|
112 | for (opt, opt_arg) in opts: |
---|
113 | if opt in ['-m', '--mask']: |
---|
114 | file_mask = opt_arg |
---|
115 | elif opt in ['-h', '--help']: |
---|
116 | usage() |
---|
117 | return 10 |
---|
118 | elif opt in ['-v', '--verbose']: |
---|
119 | Verbose = True |
---|
120 | else: |
---|
121 | usage() |
---|
122 | return 10 |
---|
123 | |
---|
124 | if len(args) != 1: |
---|
125 | usage() |
---|
126 | return 10 |
---|
127 | dir_path = args[0] |
---|
128 | |
---|
129 | # walk all directories under path, look for licence files |
---|
130 | os.path.walk(dir_path, process_one_dir, file_mask) |
---|
131 | |
---|
132 | |
---|
133 | sys.exit(main(sys.argv[1:])) |
---|