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

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

Minor bug fixes to various browse code bits and bobs ...

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