source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/insecure.py @ 800

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/insecure.py@800
Revision 800, 2.3 KB checked in by lawrence, 14 years ago (diff)

First steps to using ElementTree to parse the stub-B so we can get
at the A urls, security metadata etc, from within python ...

Line 
1#
2#This module provides python wrappers to Kevin's java code
3# I'm aiming to avoid having any java dependencies in the main browseCGI code
4# as I figure these command line activities at some point ought to be removed.
5# They certainly should be regarded as potentially insecure, especially the
6# exist call ...
7# BNL, April 2006
8#
9
10import os
11import os.path
12import tempfile
13
14def jarLoc(jar):
15        #don't want the jar in the cgi directory ... not quite sure why, but
16        #it seems wrong ... having made that decision, we have to bugger
17        #around with relative paths depending on whether this is test code
18        #(outside the cgi environment) or in the cgi environment
19        cwd=os.getcwd()
20        return os.path.normpath(os.path.join(cwd,jar))
21
22def osCommand(cmd):
23        stdin,stdout,stderr=os.popen3(cmd)
24        error=stderr.readlines()
25        if error !=[]:
26                return 1,error
27        else:
28                s=''
29                for i in stdout.readlines():s+=i
30                return 0,s
31
32def insecureGetDoc(uri,jar='./returnmoles.jar'):
33        ''' Use Kevin's code to get the document from exist '''
34        #we assume the URI is of the form repository/localid
35        #we don't use os.path.split, because this is os indpendent.
36        try:
37                repPath,localID=uri.split('/')
38        except:
39                return None
40        returnMolesJar=jarLoc(jar)
41        cmd='java -jar %s %s %s xmldb:exist://glue.badc.rl.ac.uk:8080/exist/xmlrpc'%(
42        returnMolesJar,repPath,localID)
43        status,result=osCommand(cmd)
44        if status: 
45                return None
46        else:
47                return result
48       
49def doXSLT (document,jar='./xalan-j_2_7_0/xalan.jar',xsltMethod=None):
50        ''' Handle a possibly external transformation to produce an html page '''
51        #use temporary files for the moment, ugly, but it will work.
52        # consider using pyana or some other way of doing this later ...
53        #try:
54        #return 'done'
55        fn=tempfile.mktemp('.ndg-xml')
56        f=file(fn,'wb')
57        f.write(document)
58        f.close()
59        on=tempfile.mktemp('.ndg-xml')
60        xalanjar=jarLoc(jar)
61        xslt=jarLoc('./xslt/stubb_2_browse.xsl')
62        cmd='java -cp %s org.apache.xalan.xslt.Process -IN %s -XSL %s -OUT %s'%(xalanjar,fn,xslt,on)
63        status,result=osCommand(cmd)
64        if status:
65                return result
66        else:
67                f=file(on,'r')
68                result=''
69                for i in f.readlines(): result+=i
70                f.close()
71                r=os.remove(fn)
72                r=os.remove(on)
73                return result
74       
75if __name__=="__main__":
76        d= insecureGetDoc('badc.nerc.ac.uk/dataent13',jar='../returnmoles.jar')
77        #print d
78        dd=doXSLT(d,jar='../xalan-j_2_7_0/xalan.jar')
79        print dd
Note: See TracBrowser for help on using the repository browser.