1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | A program to create a *.lic file from scratch. |
---|
5 | ''' |
---|
6 | |
---|
7 | |
---|
8 | import sys |
---|
9 | import os |
---|
10 | import getopt |
---|
11 | import glob |
---|
12 | import fnmatch |
---|
13 | import xml.etree.cElementTree as etree |
---|
14 | |
---|
15 | try: |
---|
16 | from anuga.utilities.system_tools import compute_checksum |
---|
17 | except ImportError: |
---|
18 | print("Can't import 'anuga.utilities.system_tools'?") |
---|
19 | print('Please ensure PYTHONPATH environment variable is correct.') |
---|
20 | print("It's currently: %s" % os.getenv('PYTHONPATH')) |
---|
21 | sys.exit(10) |
---|
22 | |
---|
23 | |
---|
24 | # default XML encoding |
---|
25 | DefaultEncoding = 'iso-8859-1' |
---|
26 | |
---|
27 | Datafile_Template = ''' <datafile> |
---|
28 | <filename></filename> |
---|
29 | <checksum>-1</checksum> |
---|
30 | <publishable>Y</publishable> |
---|
31 | <accountable>ANUGA developer</accountable> |
---|
32 | <source>Generated by ANUGA development team</source> |
---|
33 | <IP_owner>Geoscience Australia</IP_owner> |
---|
34 | <IP_info>For use with the ANUGA test suite</IP_info> |
---|
35 | </datafile> |
---|
36 | |
---|
37 | ''' |
---|
38 | |
---|
39 | |
---|
40 | def create_file(lfile, fnames, arg_author='', |
---|
41 | arg_publishable='', arg_accountable='', |
---|
42 | arg_source='', arg_ipowner='', arg_ipinfo=''): |
---|
43 | '''Create licence file 'lfile' that controls files in 'fnames'.''' |
---|
44 | |
---|
45 | # get username from the environment |
---|
46 | username = os.getenv('USERNAME') |
---|
47 | |
---|
48 | # see if there is already a licence file |
---|
49 | if os.path.exists(lfile): |
---|
50 | print("Sorry, licence file '%s' already exists" % lfile) |
---|
51 | return False |
---|
52 | |
---|
53 | # create initial XML template |
---|
54 | root = etree.Element('ga_license_file') |
---|
55 | root.text = '\n ' |
---|
56 | metadata = etree.SubElement(root, 'metadata') |
---|
57 | metadata.text = '\n ' |
---|
58 | metadata.tail = '\n ' |
---|
59 | author = etree.SubElement(metadata, 'author') |
---|
60 | author.tail = '\n ' |
---|
61 | if arg_author: |
---|
62 | author.text = arg_author |
---|
63 | else: |
---|
64 | author.text = username |
---|
65 | |
---|
66 | # add each controlled file to XML tree |
---|
67 | prev_f_tree = None |
---|
68 | already_done = [] # list of handled files |
---|
69 | for f in fnames: |
---|
70 | # check we haven't already handled this file |
---|
71 | if f in already_done: |
---|
72 | print("Have already seen file '%s': ignored" % f) |
---|
73 | continue |
---|
74 | already_done.append(f) |
---|
75 | |
---|
76 | # get new XML sub-tree for this file |
---|
77 | f_tree = etree.fromstring(Datafile_Template) |
---|
78 | |
---|
79 | # update filename and checksum text |
---|
80 | f_tree.find('filename').text = f |
---|
81 | f_tree.find('checksum').text = str(compute_checksum(f)) |
---|
82 | |
---|
83 | # update other tags user has overridden |
---|
84 | if arg_publishable: |
---|
85 | f_tree.find('publishable').text = arg_publishable |
---|
86 | if arg_accountable: |
---|
87 | f_tree.find('accountable').text = arg_accountable |
---|
88 | else: |
---|
89 | f_tree.find('accountable').text = username |
---|
90 | if arg_source: |
---|
91 | f_tree.find('source').text = arg_source |
---|
92 | if arg_ipowner: |
---|
93 | f_tree.find('IP_owner').text = arg_ipowner |
---|
94 | if arg_ipinfo: |
---|
95 | f_tree.find('IP_info').text = arg_ipinfo |
---|
96 | |
---|
97 | # fix element layout |
---|
98 | f_tree.tail = '\n' |
---|
99 | if prev_f_tree: |
---|
100 | prev_f_tree.tail = '\n ' |
---|
101 | prev_f_tree = f_tree |
---|
102 | |
---|
103 | # plug new sub-tree into top-level XML |
---|
104 | root.append(f_tree) |
---|
105 | |
---|
106 | # write new licence data to file |
---|
107 | root = etree.ElementTree(root) |
---|
108 | root.write(lfile, DefaultEncoding) |
---|
109 | |
---|
110 | |
---|
111 | def usage(msg=None): |
---|
112 | if msg: |
---|
113 | print(msg) |
---|
114 | |
---|
115 | print('usage: create_lic_file.py <options> <lic_file> [<filename> ...]') |
---|
116 | print('where <options> is zero or more of:') |
---|
117 | print(' --author <name>') |
---|
118 | print(' -w <name> ' |
---|
119 | '- name of the author') |
---|
120 | print(' --publishable [Yes|No]') |
---|
121 | print(' -p [Yes|No] ' |
---|
122 | '- is document publishable (Yes is default)') |
---|
123 | print(' --accountable <name>') |
---|
124 | print(' -a <name> ' |
---|
125 | '- name of person accountable for file') |
---|
126 | print(' --source <string>') |
---|
127 | print(' -s <string> ' |
---|
128 | '- source of controlled file') |
---|
129 | print(' --owner <name>') |
---|
130 | print(' -o <name> ' |
---|
131 | '- IP owner name') |
---|
132 | print(' --info <string>') |
---|
133 | print(' -i <string> ' |
---|
134 | '- IP extra information') |
---|
135 | print(' <lic_file> is the name of the licence file to create.') |
---|
136 | print(' <filename> is one or more files to control.') |
---|
137 | |
---|
138 | |
---|
139 | def main(argv): |
---|
140 | # parse command line args |
---|
141 | try: |
---|
142 | opts, args = getopt.gnu_getopt(argv, 'hw:p:a:s:o:i:', |
---|
143 | ['help', 'author=', 'publishable=', |
---|
144 | 'accountable=', 'source=', 'owner=', |
---|
145 | 'info=']) |
---|
146 | except: |
---|
147 | usage() |
---|
148 | return 10 |
---|
149 | |
---|
150 | arg_author = None |
---|
151 | arg_publishable = None |
---|
152 | arg_accountable = None |
---|
153 | arg_source = None |
---|
154 | arg_ipowner = None |
---|
155 | arg_ipinfo = None |
---|
156 | |
---|
157 | for (opt, opt_arg) in opts: |
---|
158 | if opt in ['-h', '--help']: |
---|
159 | usage() |
---|
160 | return 10 |
---|
161 | elif opt in ['-w', '--author']: |
---|
162 | arg_author = opt_arg |
---|
163 | elif opt in ['-p', '--publishable']: |
---|
164 | arg_publishable = opt_arg.title() |
---|
165 | elif opt in ['-a', '--accountable']: |
---|
166 | arg_accountable = opt_arg |
---|
167 | elif opt in ['-s', '--source']: |
---|
168 | arg_source = opt_arg |
---|
169 | elif opt in ['-o', '--owner']: |
---|
170 | arg_ipowner = opt_arg |
---|
171 | elif opt in ['-i', '--info']: |
---|
172 | arg_ipinfo = opt_arg |
---|
173 | |
---|
174 | if len(args) < 2: |
---|
175 | usage() |
---|
176 | return 10 |
---|
177 | lic_file = args[0] |
---|
178 | filenames = args[1:] |
---|
179 | |
---|
180 | create_file(lic_file, filenames, |
---|
181 | arg_author=arg_author, arg_publishable=arg_publishable, |
---|
182 | arg_accountable=arg_accountable, |
---|
183 | arg_source=arg_source, arg_ipowner=arg_ipowner, |
---|
184 | arg_ipinfo=arg_ipinfo) |
---|
185 | |
---|
186 | sys.exit(main(sys.argv[1:])) |
---|