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

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

Putting browse code into a module to make it easier
to coexist with other cgi scripts etc ...

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='glue.badc.rl.ac.uk', jar='./returnstubb.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        edb='xmldb:exist://%s:8080/exist/xmlrpc'%db
38        #following a hack for testing:
39        if uri=='methyl.example.xml':
40            f=file('../exampleB/methyl.example.xml')
41            return f.read()
42        try:
43                repPath,localID=uri.split('/')
44        except:
45                return None
46        #we can hash all this up properly later ...
47        #this file should exist (for the moment) in the cgi directory ...
48        #I know that's not very tidy, but it doesn't matter for the moment ...
49        f=file('passwords.txt','r')
50        pw={}
51        for host in f.readlines():
52            h=host.strip().split(' ')
53            pw[h[0]]=h[1],h[2]
54        #try:
55        returnMolesJar=jarLoc(jar)
56        cmd='java -jar %s repositoryID %s localID %s repository %s user %s userpw %s '%(
57               returnMolesJar,repPath,localID, edb, pw[db][0], pw[db][1])
58        status,result=commands.getstatusoutput(cmd)
59        #except:
60        #    return None
61        if status: 
62                return None
63        else:
64                return result
65       
66def doXSLT (document,jar='./xalan-j_2_7_0/xalan.jar',xsltMethod=None):
67        ''' Handle a possibly external transformation to produce an html page '''
68        #use temporary files for the moment, ugly, but it will work.
69        # consider using pyana or some other way of doing this later ...
70        #try:
71        #return 'done'
72        fn=tempfile.mktemp('.ndg-xml')
73        f=file(fn,'wb')
74        f.write(document)
75        f.close()
76        on=tempfile.mktemp('.ndg-xml')
77        xalanjar=jarLoc(jar)
78        xslt=jarLoc('./xslt/stubb_2_browse.xsl')
79        cmd='java -cp %s org.apache.xalan.xslt.Process -IN %s -XSL %s -OUT %s'%(xalanjar,fn,xslt,on)
80        status,result=osCommand(cmd)
81        if status:
82                return result
83        else:
84                f=file(on,'r')
85                result=''
86                for i in f.readlines(): result+=i
87                f.close()
88                r=os.remove(fn)
89                r=os.remove(on)
90                return result
91       
92if __name__=="__main__":
93        #d= insecureGetDoc('badc.nerc.ac.uk/dataent13',jar='../returnmoles.jar')
94        d= insecureGetDoc('badc.nerc.ac.uk/dataent1',db='gepidae.esc.rl.ac.uk',
95            jar='../returnstubb.jar')
96        print d
97        #print d
98        #dd=doXSLT(d,jar='../xalan-j_2_7_0/xalan.jar')
99        #print dd
Note: See TracBrowser for help on using the repository browser.