source: TI07-MOLES/trunk/StubB/XSLT/browse/tmp/xsltRunner.py @ 760

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/tmp/xsltRunner.py@760
Revision 760, 2.5 KB checked in by lawrence, 14 years ago (diff)

This changeset includes a temporary directory for showing problems
with the xslt in the background for browse (and includes the xalan
processor).

Line 
1#!/usr/bin/env python
2"""
3General XSLT runner
4
5Parameters:
6        1 - XSLT script to run, including path from current location - mandatory
7        2 - file name modifier, put in output file name like "_DIF" in original DIF script - mandatory
8        3 - input directory - mandatory
9        4 - output directory - optional: default = <input dir>/output, if it doesn't
10                already exist. If it does then warn user and exit.
11
12Notes:  Overwrites any files in output directory
13                spaces in parameters failed test and other special characters untested
14"""
15# define exceptions
16class xsltError(Exception): pass
17class BadArgs(xsltError): pass
18class NoScriptError(xsltError): pass
19class NoInputDir(xsltError): pass
20class NoOutputDir(xsltError): pass
21
22import os, sys, string
23
24n_files_produced = 0
25
26if len(sys.argv) < 3 or len(sys.argv) > 5 :     # invalid number of parameters
27        raise BadArgs('Invalid argument list. Usage is: xsltRunner xsltFile inputPath [outputPath]')
28
29scriptFile = ''
30scriptFile = scriptFile.join(sys.argv[1:2])
31# Check script is there
32if not os.access(scriptFile, os.R_OK):
33        raise NoScriptError("Script " + scriptFile + " does not exist")
34
35fileNameModifier = ''
36fileNameModifier = fileNameModifier.join(sys.argv[2:3])
37print "fileNameModifier = " + fileNameModifier
38# Check inputDir is there
39inputDir = ''
40inputDir = inputDir.join(sys.argv[3:4])
41print inputDir
42if not os.access(os.path.normcase(inputDir), os.R_OK):
43        raise NoInputDir("Input path " + str(sys.argv[3:4]) + " does not exist")
44else:
45        inputFiles = os.listdir(inputDir)
46
47# Check outputDir is there
48outputDir = ''
49if len(sys.argv) > 4 :  # Got specified output directory
50        outputDir = outputDir.join(sys.argv[4:5])
51else:
52        outputDir = inputDir + os.sep + "output"
53print "outputDir = " + outputDir
54if not os.access(outputDir, os.W_OK):
55        os.makedirs(os.path.normcase(outputDir), os.W_OK)
56        if not os.access(outputDir, os.W_OK):
57                raise NoOutputDir("Output path " + outputDir + " does not exist")
58
59# Process
60for inputFile in inputFiles:
61        if inputFile.find('.xml') != -1:
62                inputFile=os.path.normcase(inputDir + os.sep + inputFile)
63                outfile = ''
64                outfile=inputFile.rsplit(os.sep, 1)[1] + fileNameModifier + '.xml'
65                outfile=os.path.normcase(outputDir + os.sep + outfile.split('.xml')[0] + fileNameModifier + '.xml')
66                commandline = 'java org.apache.xalan.xslt.Process -IN %s -XSL %s -OUT %s' %(inputFile, os.path.normcase(scriptFile), outfile)
67                os.system(commandline)
68                n_files_produced += 1
69                print "%s:Produced: %s" %(n_files_produced, outfile)
Note: See TracBrowser for help on using the repository browser.