source: branches/numpy_misc/tools/update_lic_checksum/create_lic_file.py @ 7156

Last change on this file since 7156 was 7156, checked in by rwilson, 15 years ago

Created program to update licence files, one to create them.

File size: 5.2 KB
Line 
1#!/usr/bin/env python
2
3'''
4A program to create a *.lic file from scratch.
5'''
6
7
8import sys
9import os
10import getopt
11import glob
12import fnmatch
13import xml.etree.cElementTree as etree
14
15try:
16    from anuga.utilities.system_tools import compute_checksum
17except 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
25DefaultEncoding = 'iso-8859-1'
26
27Datafile_Template = '''  <datafile>
28    <filename></filename>
29    <checksum>-1</checksum>
30    <publishable>N</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
40def 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    # see if there is already a licence file
46    if os.path.exists(lfile):
47        print("Sorry, licence file '%s' already exists" % lfile)
48        return False
49
50    # create initial XML template
51    root = etree.Element('ga_license_file')
52    root.text = '\n  '
53    metadata = etree.SubElement(root, 'metadata')
54    metadata.text = '\n    '
55    metadata.tail = '\n  '
56    author = etree.SubElement(metadata, 'author')
57    author.tail = '\n  '
58    if arg_author != '':
59        author.text = arg_author
60
61    # add each controlled file to XML tree
62    prev_f_tree = None
63    for f in fnames:
64        # get new XML sub-tree for this file
65        f_tree = etree.fromstring(Datafile_Template)
66
67        # update  filename and checksum text
68        f_tree.find('filename').text = f
69        f_tree.find('checksum').text = str(compute_checksum(f))
70
71        # update other tags user has overridden
72        if arg_publishable:
73            f_tree.find('publishable').text = arg_publishable
74        if arg_accountable:
75            f_tree.find('accountable').text = arg_accountable
76        if arg_source:
77            f_tree.find('source').text = arg_source
78        if arg_ipowner:
79            f_tree.find('IP_owner').text = arg_ipowner
80        if arg_ipinfo:
81            f_tree.find('IP_info').text = arg_ipinfo
82
83        # fix element layout
84        f_tree.tail = '\n'
85        if prev_f_tree:
86            prev_f_tree.tail = '\n  '
87        prev_f_tree = f_tree
88
89        # plug new sub-tree into top-level XML
90        root.append(f_tree)
91
92    # write new licence data to file
93    root = etree.ElementTree(root)
94    root.write(lfile, DefaultEncoding)
95
96
97def usage(msg=None):
98    if msg:
99        print(msg)
100
101    print('usage: create_lic_file.py <options> <lic_file> [<filename> ...]')
102    print('where <options>  is zero or more of:')
103    print('                  --author <name>')
104    print('                  -w <name>             '
105          '- name of the author')
106    print('                  --publishable [Yes|No]')
107    print('                  -p [Yes|No]           '
108          '- is document publishable')
109    print('                  --accountable <name>')
110    print('                  -a <name>             '
111          '- name of person accountable for file')
112    print('                  --source <string>')
113    print('                  -s <string>           '
114          '- source of controlled file')
115    print('                  --owner <name>')
116    print('                  -o <name>             '
117          '- IP owner name')
118    print('                  --info <string>')
119    print('                  -i <string>           '
120          '- IP extra information')
121    print('      <lic_file> is the name of the licence file to create.')
122    print('      <filename> is one or more files to control.')
123
124
125def main(argv):
126    # parse command line args
127    try:
128        opts, args = getopt.gnu_getopt(argv, 'hw:p:a:s:o:i:',
129                                       ['help', 'author=', 'publishable=',
130                                        'accountable=', 'source=', 'owner=',
131                                        'info='])
132    except:
133        usage()
134        return 10
135
136    arg_author = None
137    arg_publishable = None
138    arg_accountable = None
139    arg_source = None
140    arg_ipowner = None
141    arg_ipinfo = None
142
143    for (opt, opt_arg) in opts:
144        if opt in ['-h', '--help']:
145            usage()
146            return 10
147        elif opt in ['-w', '--author']:
148            arg_author = opt_arg
149        elif opt in ['-p', '--publishable']:
150            arg_publishable = opt_arg.title()
151        elif opt in ['-a', '--accountable']:
152            arg_accountable = opt_arg
153        elif opt in ['-s', '--source']:
154            arg_source = opt_arg
155        elif opt in ['-o', '--owner']:
156            arg_ipowner = opt_arg
157        elif opt in ['-i', '--info']:
158            arg_ipinfo = opt_arg
159
160    if len(args) < 2:
161        usage()
162        return 10
163    lic_file = args[0]
164    filenames = args[1:]
165
166    create_file(lic_file, filenames,
167                arg_author, arg_publishable, arg_accountable,
168                arg_source, arg_ipowner, arg_ipinfo)
169
170sys.exit(main(sys.argv[1:]))
Note: See TracBrowser for help on using the repository browser.