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

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

Exist database now configurable

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