source: ndgCommon/trunk/ndg/common/src/tools/granulator.py @ 5188

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/tools/granulator.py@5188
Revision 5188, 3.4 KB checked in by cbyrom, 10 years ago (diff)

Delete original granulator codebase and associated code and create new
granulator script in the ndgcommon tools package, where it more
logically sits. Add new README with up to date details and update
tests.

Line 
1#!/usr/bin/env python
2"""
3Wrapper script to run granulator against all granulite files in the current dir
4"""
5import os, sys, logging, getopt
6# configure logging
7logging.basicConfig(level=logging.INFO,
8                    format='%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s')
9from ndg.common.src.lib.granulite import granulite
10
11
12def usage():
13    '''
14    Display input params for the script
15    '''
16    print "Usage: python granulator.py [OPTION] <granulite_filename>, <granulite_filename>,..."
17    print " - where:\n  <granulite_filename>,.. is a list of granulite file with granule configuration data - NB, this is"
18    print "    optional - if not specified, the script will process all files with a '.granulite' suffix in the "
19    print "    current working directory."
20    print "Options:"
21    print " -x - delete mode - remove the granule data specified in the granulite from eXist"
22    print " -a - aggregate coverage mode - if set, only coverage data that extends the existing"
23    print "    atom coverage data will be added"
24    print " -r - replace mode - if the granule data already exists in eXist, automatically"
25    print "    overwrite it with the current data - NB, if not set, and existing data is found,"
26    print "    an exception is thrown"
27    sys.exit(2)
28
29
30run_counter = 0
31error_counter = 0
32
33lineSeparator = "-----------------------------"
34
35logging.info(lineSeparator)
36logging.info("RUNNING: granulator.py")
37logging.info(lineSeparator)
38
39opts, args = getopt.getopt(sys.argv[1:], '-rxah')
40
41isReplace = False
42isDelete = False
43aggregateCoverage = False 
44for o, a in opts:
45    if o == "-r":
46        print " - Replace mode ON"
47        isReplace = True
48    elif o == "-x":
49        print " - Delete mode ON"
50        isDelete = True
51    elif o == "-a":
52        print " - Aggregate coverage mode ON"
53        aggregateCoverage = True
54    elif o == "-h":
55        usage()
56    else:
57        usage()
58
59current_dir = os.getcwd() + "/"# this is the base dir that the script is ran from
60logging.info("Running granulite ingester script for all granulite files in the current run directory (%s)" %current_dir)
61
62# iterate over all files in the current directory and process any granulite ones
63granulite_suffix = '.granulite'
64if not args[0]:
65    logging.info("No input file specified - will run script on all granulite files in current directory")
66    filenames = os.listdir(current_dir)
67else:
68    filenames = args
69   
70for filename in filenames:
71        if filename.endswith(granulite_suffix):
72            logging.info("Found granulite file: %s" %filename)
73           
74            # now invoke the ingest script
75            logging.info("Running the granulite ingest script with the file...")
76            try:
77                run_counter += 1
78                g = granulite(filename, replaceAtom = isReplace, 
79                              deleteMode = isDelete, 
80                              aggregateCoverage = aggregateCoverage)
81                g.processGranulite()
82            except:
83                logging.error("Exception thrown - detail: ")
84                logging.error(sys.exc_info())
85                logging.info("Continue processing next granulite file...")
86                error_counter +=1
87               
88logging.info("granulator.py complete - processed %s config files" %run_counter)
89if error_counter > 0:           
90    logging.error("WARNING: %s errors were encountered during the run - check logfiles for more details" %error_counter)
Note: See TracBrowser for help on using the repository browser.