#!C:\Python25\python.exe # **************************************** # +++ BioCASE # +++ pywrapper # # See http://www.biocase.org for details # # **************************************** from xml.dom.minidom import Document #from xml.dom.expatbuilder import minidom import os, cgi, sys, zipfile, os.path import xml.dom.minidom from xml.dom.minidom import Node import xml.sax import pprint #import cgitb #cgitb.enable() # ***** include the biocase.lib directory in the python sys path for module importing ***** execfile( os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.path.pardir, 'lib', 'biocase', 'adjustpath.py')) ) import biocase.configuration cfg = biocase.configuration.Cfg() import logging, biocase.initlogs log = logging.getLogger("pywrapper") from biocase.wrapper.pywrapper import pywrapper archiveRequest = 0 archiveAppend = 1 archiveFileName = "aabcd.zip" ticket = "" hits = 0 try: # get datasource alias from GET querystring if os.environ.has_key("QUERY_STRING"): QS = cgi.parse_qs( os.environ["QUERY_STRING"] ) if QS.has_key('archiverequest'): if QS['archiverequest'][0] == "on" or QS['archiverequest'][0] == "1": archiveRequest = 1 else: archiveRequest = 0 if QS.has_key('archiveappend'): if QS['archiveappend'][0] == "on" or QS['archiveappend'][0] == "1": archiveAppend = 1 else: archiveAppend = 0 if QS.has_key('archivefilename'): archiveFileName = QS['archivefilename'][0] if QS.has_key('ticket'): ticket = QS['ticket'][0] if QS.has_key('dsa'): dsa = QS['dsa'][0] elif QS.has_key('DSA'): dsa = QS['DSA'][0] elif QS.has_key('datasource'): dsa = QS['datasource'][0] else: raise "No Datasource alias given!" else: raise "No querystring given. Pass at least a datasource alias via dsa=XXX!" except Error: raise Error # #sys.stdout.write('Content-Type: text\n\n') #sys.stdout.write(str(os.environ.has_key("QUERY_STRING")) + "\n") #sys.stdout.write(str(archiveRequest) + "\n") #sys.stdout.write(str(archiveAppend) + "\n") #sys.stdout.write(str(archiveFileName) + "\n") #sys.stdout.write(str(recursiveAppend) + "\n") #sys.stdout.write(str(recordCount) + "\n") #sys.stdout.write(str(recordStart) + "\n") #sys.stdout.write(str(searchHits) + "\n") # #exit() biocase.initlogs.initWrapperLogging(dsa) pywInstance = pywrapper('biocase', dsa=dsa) # create a request from CGI log.debug("pywInstance.parseRequest...") pywInstance.parseRequest() # print the result to the stream log.debug("pywInstance.doResponse...") def extractContent(fileName): try: dom = xml.dom.minidom.parse(fileName) out = dom.getElementsByTagName("abcd:DataSets") out[0].setAttribute("xmlns:abcd", "http://www.tdwg.org/schemas/abcd/2.06") #sys.stdout.write(out[0].toxml('utf-8')) # c = dom.getElementsByTagName("biocase:content")[0] global hits hits = int(c.getAttribute("totalSearchHits")) return out[0].toxml('utf-8') except: error = str(sys.exc_info()[0]) + "\n" + str(sys.exc_info()[1]) + "\n" + str(sys.exc_info()[2]) + "\n" writeResponse("error", error) exit(0) def writeResponse(status, message): #print "" print "" print "" + status + "" print "" + message + "" print "" + str(hits) + "" print "" if archiveRequest == 1: #sys.stdout.write('Content-Type: text/html\n\n') #sys.stdout.write('Content-Type: text/plain\n\n') sys.stdout.write('Content-Type: text/xml\n\n') archivePath = os.path.abspath(".." + os.sep + "archive") + os.sep + dsa + os.sep if not os.path.exists(archivePath): os.makedirs(archivePath) conf = archivePath + archiveFileName + ".config.xml" _ticket = "" if os.path.exists(conf): _dom = xml.dom.minidom.parse(conf) _ticket = str(_dom.getElementsByTagName("ticket")[0].firstChild.data) #print "_ticket = " + _ticket + ", ticket = " + ticket if (_ticket != ticket) or ticket == "" or _ticket == "": writeResponse("error", "Authorization failed.") sys.exit(0) #print archivePath try: import zlib compression = zipfile.ZIP_DEFLATED except: compression = zipfile.ZIP_STORED try: if archiveAppend == 1: dumpName = archivePath+archiveFileName+".dump.xml" zipName = "response.1.xml" tmpName = archivePath+archiveFileName+"."+zipName if not os.path.exists(archivePath + archiveFileName): zip = zipfile.ZipFile(archivePath + archiveFileName, "w") else: zip = zipfile.ZipFile(archivePath + archiveFileName, "a") tmpName = archivePath+archiveFileName+".response." + str(len(zip.namelist()) + 1) + ".xml" zipName = "response." + str(len(zip.namelist()) + 1) + ".xml" tmpFile = open(dumpName, "w") pywInstance.doResponse(stream=tmpFile, httpHeader=0) tmpFile.close() tmpFile = open(tmpName, "w") tmpFile.write(extractContent(dumpName)) tmpFile.close() zip.write(tmpName, zipName, compression) zip.close() os.remove(tmpFile.name) writeResponse("ok", zipName + " appended to archive " + archiveFileName) else: dumpName = archivePath+archiveFileName+".dump.xml" zipName = "response.1.xml" tmpName = archivePath+archiveFileName+"."+zipName tmpFile = open(dumpName, "w") pywInstance.doResponse(stream=tmpFile, httpHeader=0) tmpFile.close() tmpFile = open(tmpName, "w") tmpFile.write(extractContent(dumpName)) tmpFile.close() zip = zipfile.ZipFile(archivePath + archiveFileName, "w") zip.write(tmpName, zipName, compression) zip.close() os.remove(tmpFile.name) writeResponse("ok", zipName + " stored in archive " + archiveFileName) except: sys.stdout.write(str(sys.exc_info()[0])) sys.stdout.write(str(sys.exc_info()[1])) sys.stdout.write(str(sys.exc_info()[2])) else: pywInstance.doResponse(httpHeader=1) log.info("PyWrapper response successful")