Index: /branches/numpy_misc/tools/update_lic_checksum/README
===================================================================
--- /branches/numpy_misc/tools/update_lic_checksum/README (revision 7156)
+++ /branches/numpy_misc/tools/update_lic_checksum/README (revision 7156)
@@ -0,0 +1,11 @@
+This directory contains programs used to create and maintain *.lic files.
+
+create_lic_file.py
+------------------
+
+Used to create a *.lic file for one or more controlled files.
+
+update_lic_checksum.py
+----------------------
+
+Updates existing *.lic files.
Index: /branches/numpy_misc/tools/update_lic_checksum/create_lic_file.py
===================================================================
--- /branches/numpy_misc/tools/update_lic_checksum/create_lic_file.py (revision 7156)
+++ /branches/numpy_misc/tools/update_lic_checksum/create_lic_file.py (revision 7156)
@@ -0,0 +1,170 @@
+#!/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 = '''
+
+ -1
+ N
+ ANUGA developer
+
+ Geoscience Australia
+ For use with the ANUGA test suite
+
+
+'''
+
+
+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'.'''
+
+ # 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
+
+ # add each controlled file to XML tree
+ prev_f_tree = None
+ for f in fnames:
+ # 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
+ 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')
+ 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']:
+ arg_publishable = opt_arg.title()
+ 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_publishable, arg_accountable,
+ arg_source, arg_ipowner, arg_ipinfo)
+
+sys.exit(main(sys.argv[1:]))
Index: /branches/numpy_misc/tools/update_lic_checksum/update_lic_checksum.py
===================================================================
--- /branches/numpy_misc/tools/update_lic_checksum/update_lic_checksum.py (revision 7156)
+++ /branches/numpy_misc/tools/update_lic_checksum/update_lic_checksum.py (revision 7156)
@@ -0,0 +1,133 @@
+#!/usr/bin/env python
+
+'''
+A program to update existing *.lic file checksum values in a given
+directory tree.
+
+Mask of files to process may be specified (defaults to '*.lic').
+
+Always recurses down into sub-directories.
+'''
+
+
+import sys
+import os
+import getopt
+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'
+
+# verbose flag
+Verbose = False
+
+
+def process_file(dirname, fname):
+ '''Update checksum in a licence file.'''
+
+ # read in the licence file (XML)
+ lic_tree = etree.ElementTree()
+ input_filename = os.path.join(dirname, fname)
+ lic_tree.parse(input_filename)
+
+ if Verbose:
+ print('Updating %s' % input_filename)
+
+ # process every tagset - update checksum
+ datafiles = lic_tree.getiterator('datafile')
+ for f in datafiles:
+ # get 'filename' element text
+ filename = f.find('filename')
+ if filename is None:
+ msg = ('Badly formed licence XML in file %s:\n%s'
+ % (input_filename, etree.dump(f)))
+ raise RuntimeError, msg
+
+ # get 'checksum' element text
+ checksum = f.find('checksum')
+ if checksum is None:
+ msg = ('Badly formed licence XML in file %s:\n%s'
+ % (input_filename, etree.dump(f)))
+ raise RuntimeError, msg
+
+ # update the 'checksum' element from the file
+ controlled_filename = os.path.join(dirname, filename.text)
+ if (not os.path.exists(controlled_filename) or
+ not os.path.isfile(controlled_filename)):
+ print('?' * 80)
+ print("Licence file %s\nrefers to %s\nwhich doesn't exist!?"
+ % (input_filename, controlled_filename))
+ print('?' * 80 + '\n')
+ continue
+ new_checksum = compute_checksum(controlled_filename)
+ checksum.text = str(new_checksum)
+
+ # write updated XML data back to file
+ new_fname = os.path.join(dirname, fname+'._NEW_')
+ lic_tree.write(new_fname, DefaultEncoding)
+ os.rename(new_fname, input_filename)
+
+
+def process_one_dir(mask, dirname, names):
+ '''Update all licence files in directory that match 'mask'.'''
+
+ for fname in names:
+ if fnmatch.fnmatch(fname, mask):
+ process_file(dirname, fname)
+
+
+def usage(msg=None):
+ '''Print helpful usage one-liner.'''
+
+ if msg:
+ print(msg)
+
+ print('usage: update_lic_checksum [-m ] ')
+
+
+def main(argv):
+ '''Main function - parse args and start the process.'''
+
+ global Verbose
+
+ # parse command line args
+ try:
+ opts, args = getopt.gnu_getopt(argv, 'hm:v',
+ ['help', 'mask=', 'verbose'])
+ except:
+ usage()
+ return 10
+
+ file_mask = '*.lic'
+ for (opt, opt_arg) in opts:
+ if opt in ['-m', '--mask']:
+ file_mask = opt_arg
+ elif opt in ['-h', '--help']:
+ usage()
+ return 10
+ elif opt in ['-v', '--verbose']:
+ Verbose = True
+ else:
+ usage()
+ return 10
+
+ if len(args) != 1:
+ usage()
+ return 10
+ dir_path = args[0]
+
+ # walk all directories under path, look for licence files
+ os.path.walk(dir_path, process_one_dir, file_mask)
+
+
+sys.exit(main(sys.argv[1:]))