[7156] | 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> |
---|
[7298] | 30 | <publishable>Yes</publishable> |
---|
[7156] | 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 | |
---|
[7298] | 39 | # dictionary to map yes/no chars to a string |
---|
| 40 | YesNoDict = {'y': 'Yes', |
---|
| 41 | 'n': 'No'} |
---|
[7156] | 42 | |
---|
[7298] | 43 | |
---|
[7156] | 44 | def create_file(lfile, fnames, arg_author='', |
---|
| 45 | arg_publishable='', arg_accountable='', |
---|
| 46 | arg_source='', arg_ipowner='', arg_ipinfo=''): |
---|
| 47 | '''Create licence file 'lfile' that controls files in 'fnames'.''' |
---|
| 48 | |
---|
[7178] | 49 | # get username from the environment |
---|
| 50 | username = os.getenv('USERNAME') |
---|
| 51 | |
---|
[7156] | 52 | # see if there is already a licence file |
---|
| 53 | if os.path.exists(lfile): |
---|
| 54 | print("Sorry, licence file '%s' already exists" % lfile) |
---|
| 55 | return False |
---|
| 56 | |
---|
| 57 | # create initial XML template |
---|
| 58 | root = etree.Element('ga_license_file') |
---|
| 59 | root.text = '\n ' |
---|
| 60 | metadata = etree.SubElement(root, 'metadata') |
---|
| 61 | metadata.text = '\n ' |
---|
| 62 | metadata.tail = '\n ' |
---|
| 63 | author = etree.SubElement(metadata, 'author') |
---|
| 64 | author.tail = '\n ' |
---|
[7178] | 65 | if arg_author: |
---|
[7156] | 66 | author.text = arg_author |
---|
[7178] | 67 | else: |
---|
| 68 | author.text = username |
---|
[7156] | 69 | |
---|
| 70 | # add each controlled file to XML tree |
---|
| 71 | prev_f_tree = None |
---|
[7178] | 72 | already_done = [] # list of handled files |
---|
[7156] | 73 | for f in fnames: |
---|
[7178] | 74 | # check we haven't already handled this file |
---|
| 75 | if f in already_done: |
---|
| 76 | print("Have already seen file '%s': ignored" % f) |
---|
| 77 | continue |
---|
| 78 | already_done.append(f) |
---|
| 79 | |
---|
[7156] | 80 | # get new XML sub-tree for this file |
---|
| 81 | f_tree = etree.fromstring(Datafile_Template) |
---|
| 82 | |
---|
| 83 | # update filename and checksum text |
---|
| 84 | f_tree.find('filename').text = f |
---|
| 85 | f_tree.find('checksum').text = str(compute_checksum(f)) |
---|
| 86 | |
---|
| 87 | # update other tags user has overridden |
---|
| 88 | if arg_publishable: |
---|
| 89 | f_tree.find('publishable').text = arg_publishable |
---|
| 90 | if arg_accountable: |
---|
| 91 | f_tree.find('accountable').text = arg_accountable |
---|
[7178] | 92 | else: |
---|
| 93 | f_tree.find('accountable').text = username |
---|
[7156] | 94 | if arg_source: |
---|
| 95 | f_tree.find('source').text = arg_source |
---|
| 96 | if arg_ipowner: |
---|
| 97 | f_tree.find('IP_owner').text = arg_ipowner |
---|
| 98 | if arg_ipinfo: |
---|
| 99 | f_tree.find('IP_info').text = arg_ipinfo |
---|
| 100 | |
---|
| 101 | # fix element layout |
---|
| 102 | f_tree.tail = '\n' |
---|
| 103 | if prev_f_tree: |
---|
| 104 | prev_f_tree.tail = '\n ' |
---|
| 105 | prev_f_tree = f_tree |
---|
| 106 | |
---|
| 107 | # plug new sub-tree into top-level XML |
---|
| 108 | root.append(f_tree) |
---|
| 109 | |
---|
| 110 | # write new licence data to file |
---|
| 111 | root = etree.ElementTree(root) |
---|
| 112 | root.write(lfile, DefaultEncoding) |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | def usage(msg=None): |
---|
| 116 | if msg: |
---|
| 117 | print(msg) |
---|
| 118 | |
---|
| 119 | print('usage: create_lic_file.py <options> <lic_file> [<filename> ...]') |
---|
| 120 | print('where <options> is zero or more of:') |
---|
| 121 | print(' --author <name>') |
---|
| 122 | print(' -w <name> ' |
---|
| 123 | '- name of the author') |
---|
| 124 | print(' --publishable [Yes|No]') |
---|
| 125 | print(' -p [Yes|No] ' |
---|
[7211] | 126 | '- is document publishable (Yes is default)') |
---|
[7156] | 127 | print(' --accountable <name>') |
---|
| 128 | print(' -a <name> ' |
---|
| 129 | '- name of person accountable for file') |
---|
| 130 | print(' --source <string>') |
---|
| 131 | print(' -s <string> ' |
---|
| 132 | '- source of controlled file') |
---|
| 133 | print(' --owner <name>') |
---|
| 134 | print(' -o <name> ' |
---|
| 135 | '- IP owner name') |
---|
| 136 | print(' --info <string>') |
---|
| 137 | print(' -i <string> ' |
---|
| 138 | '- IP extra information') |
---|
| 139 | print(' <lic_file> is the name of the licence file to create.') |
---|
| 140 | print(' <filename> is one or more files to control.') |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | def main(argv): |
---|
| 144 | # parse command line args |
---|
| 145 | try: |
---|
| 146 | opts, args = getopt.gnu_getopt(argv, 'hw:p:a:s:o:i:', |
---|
| 147 | ['help', 'author=', 'publishable=', |
---|
| 148 | 'accountable=', 'source=', 'owner=', |
---|
| 149 | 'info=']) |
---|
| 150 | except: |
---|
| 151 | usage() |
---|
| 152 | return 10 |
---|
| 153 | |
---|
| 154 | arg_author = None |
---|
| 155 | arg_publishable = None |
---|
| 156 | arg_accountable = None |
---|
| 157 | arg_source = None |
---|
| 158 | arg_ipowner = None |
---|
| 159 | arg_ipinfo = None |
---|
| 160 | |
---|
| 161 | for (opt, opt_arg) in opts: |
---|
| 162 | if opt in ['-h', '--help']: |
---|
| 163 | usage() |
---|
| 164 | return 10 |
---|
| 165 | elif opt in ['-w', '--author']: |
---|
| 166 | arg_author = opt_arg |
---|
| 167 | elif opt in ['-p', '--publishable']: |
---|
[7298] | 168 | try: |
---|
| 169 | arg_publishable = YesNoDict[opt_arg.lower()[0]] |
---|
| 170 | except KeyError: |
---|
| 171 | print("Bad %s arg: '%s' " |
---|
| 172 | "(expected a string starting 'y', 'n', 'Y' or 'N')" |
---|
| 173 | % (opt, opt_arg)) |
---|
[7156] | 174 | elif opt in ['-a', '--accountable']: |
---|
| 175 | arg_accountable = opt_arg |
---|
| 176 | elif opt in ['-s', '--source']: |
---|
| 177 | arg_source = opt_arg |
---|
| 178 | elif opt in ['-o', '--owner']: |
---|
| 179 | arg_ipowner = opt_arg |
---|
| 180 | elif opt in ['-i', '--info']: |
---|
| 181 | arg_ipinfo = opt_arg |
---|
| 182 | |
---|
| 183 | if len(args) < 2: |
---|
| 184 | usage() |
---|
| 185 | return 10 |
---|
| 186 | lic_file = args[0] |
---|
| 187 | filenames = args[1:] |
---|
| 188 | |
---|
| 189 | create_file(lic_file, filenames, |
---|
[7178] | 190 | arg_author=arg_author, arg_publishable=arg_publishable, |
---|
| 191 | arg_accountable=arg_accountable, |
---|
| 192 | arg_source=arg_source, arg_ipowner=arg_ipowner, |
---|
| 193 | arg_ipinfo=arg_ipinfo) |
---|
[7156] | 194 | |
---|
| 195 | sys.exit(main(sys.argv[1:])) |
---|