#!/usr/bin/env python
'''
A program to create a *.lic file from scratch.
'''
import sys
import os
import getopt
import glob
import fnmatch
import xml.etree.cElementTree as etree
try:
from anuga.utilities.system_tools import compute_checksum
except ImportError:
print("Can't import 'anuga.utilities.system_tools'?")
print('Please ensure PYTHONPATH environment variable is correct.')
print("It's currently: %s" % os.getenv('PYTHONPATH'))
sys.exit(10)
# default XML encoding
DefaultEncoding = 'iso-8859-1'
Datafile_Template = ''' -1YesANUGA developerGenerated by ANUGA development teamGeoscience AustraliaFor use with the ANUGA test suite
'''
# dictionary to map yes/no chars to a string
YesNoDict = {'y': 'Yes',
'n': 'No'}
def create_file(lfile, fnames, arg_author='',
arg_publishable='', arg_accountable='',
arg_source='', arg_ipowner='', arg_ipinfo=''):
'''Create licence file 'lfile' that controls files in 'fnames'.'''
# get username from the environment
username = os.getenv('USERNAME')
# see if there is already a licence file
if os.path.exists(lfile):
print("Sorry, licence file '%s' already exists" % lfile)
return False
# create initial XML template
root = etree.Element('ga_license_file')
root.text = '\n '
metadata = etree.SubElement(root, 'metadata')
metadata.text = '\n '
metadata.tail = '\n '
author = etree.SubElement(metadata, 'author')
author.tail = '\n '
if arg_author:
author.text = arg_author
else:
author.text = username
# add each controlled file to XML tree
prev_f_tree = None
already_done = [] # list of handled files
for f in fnames:
# check we haven't already handled this file
if f in already_done:
print("Have already seen file '%s': ignored" % f)
continue
already_done.append(f)
# get new XML sub-tree for this file
f_tree = etree.fromstring(Datafile_Template)
# update filename and checksum text
f_tree.find('filename').text = f
f_tree.find('checksum').text = str(compute_checksum(f))
# update other tags user has overridden
if arg_publishable:
f_tree.find('publishable').text = arg_publishable
if arg_accountable:
f_tree.find('accountable').text = arg_accountable
else:
f_tree.find('accountable').text = username
if arg_source:
f_tree.find('source').text = arg_source
if arg_ipowner:
f_tree.find('IP_owner').text = arg_ipowner
if arg_ipinfo:
f_tree.find('IP_info').text = arg_ipinfo
# fix element layout
f_tree.tail = '\n'
if prev_f_tree:
prev_f_tree.tail = '\n '
prev_f_tree = f_tree
# plug new sub-tree into top-level XML
root.append(f_tree)
# write new licence data to file
root = etree.ElementTree(root)
root.write(lfile, DefaultEncoding)
def usage(msg=None):
if msg:
print(msg)
print('usage: create_lic_file.py [ ...]')
print('where is zero or more of:')
print(' --author ')
print(' -w '
'- name of the author')
print(' --publishable [Yes|No]')
print(' -p [Yes|No] '
'- is document publishable (Yes is default)')
print(' --accountable ')
print(' -a '
'- name of person accountable for file')
print(' --source ')
print(' -s '
'- source of controlled file')
print(' --owner ')
print(' -o '
'- IP owner name')
print(' --info ')
print(' -i '
'- IP extra information')
print(' is the name of the licence file to create.')
print(' is one or more files to control.')
def main(argv):
# parse command line args
try:
opts, args = getopt.gnu_getopt(argv, 'hw:p:a:s:o:i:',
['help', 'author=', 'publishable=',
'accountable=', 'source=', 'owner=',
'info='])
except:
usage()
return 10
arg_author = None
arg_publishable = None
arg_accountable = None
arg_source = None
arg_ipowner = None
arg_ipinfo = None
for (opt, opt_arg) in opts:
if opt in ['-h', '--help']:
usage()
return 10
elif opt in ['-w', '--author']:
arg_author = opt_arg
elif opt in ['-p', '--publishable']:
try:
arg_publishable = YesNoDict[opt_arg.lower()[0]]
except KeyError:
print("Bad %s arg: '%s' "
"(expected a string starting 'y', 'n', 'Y' or 'N')"
% (opt, opt_arg))
elif opt in ['-a', '--accountable']:
arg_accountable = opt_arg
elif opt in ['-s', '--source']:
arg_source = opt_arg
elif opt in ['-o', '--owner']:
arg_ipowner = opt_arg
elif opt in ['-i', '--info']:
arg_ipinfo = opt_arg
if len(args) < 2:
usage()
return 10
lic_file = args[0]
filenames = args[1:]
create_file(lic_file, filenames,
arg_author=arg_author, arg_publishable=arg_publishable,
arg_accountable=arg_accountable,
arg_source=arg_source, arg_ipowner=arg_ipowner,
arg_ipinfo=arg_ipinfo)
sys.exit(main(sys.argv[1:]))