X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fsfiprocess.py;h=089c7393c2d3fa501c2ad1c6c63e30deab4b4f29;hp=ca2c023f03d88507b43c37a7ce79007e88f7ba1a;hb=5a487f5551aff6228d7615a85ff4c84a54d98e85;hpb=b7b7f439707e66f748e6658758f17f8bcb682c17 diff --git a/sface/sfiprocess.py b/sface/sfiprocess.py index ca2c023..089c739 100644 --- a/sface/sfiprocess.py +++ b/sface/sfiprocess.py @@ -1,22 +1,21 @@ import os +import json +import pickle import sys +import tempfile import time -import re from PyQt4.QtCore import * -from PyQt4.QtXml import QDomDocument from sface.config import config -from sface.logwindow import LogIO -from sface.rspecwindow import RSpecWindow, DomModel - +from sface.xmlrpcwindow import get_tracker, XmlrpcReader def find_executable(exec_name): """find the given executable in $PATH""" paths = os.getenv("PATH").split(':') for p in paths: exec_path = os.path.join(p, exec_name) - if os.path.exists(exec_path): + if os.path.exists(exec_path) and os.access(exec_path,os.X_OK): return exec_path return None @@ -25,25 +24,47 @@ class SfiProcess(QObject): def __init__(self, parent=None): QObject.__init__(self, parent) + env = QProcess.systemEnvironment() + env << "PYTHONPATH=%s" % ":".join(sys.path) self.process = QProcess() + self.process.setEnvironment(env) self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"), self.processFinished) - - self.xmltracker = XmlTracker() - # in case self.output is read by the XmlTracker before any - # readyReadStandardOutput signal - self.output = '' + + self.xmlrpcreader = XmlrpcReader() # this one is for parsing XMLRPC responses + + # holds aggregate output from processStandardOutput(); used by xmlrpc + # tracker. + self.output = "" + + self.do_raw = True # xxx should check version of sfa self.connect(self.process, SIGNAL("readyReadStandardOutput()"), self.processStandardOutput) self.connect(self.process, SIGNAL("readyReadStandardError()"), self.processStandardError) - def __init_command(self, args): + def __init_command(self, args, save_filename=None): self.args = QStringList() - if config.debug: - # this shows xmlrpc conversation, see sfi.py docs. - self.args << QString('-D') + self.args << "-d" + self.args << config.get_dirname() + + # this shows xmlrpc conversation, see sfi.py docs. + # always do this, so we can parse the XML result for faults and show + # then to the users. + self.args << QString('-D') + + if self.do_raw: + self.raw_filename = tempfile.mktemp(suffix=".raw") + self.args << QString('-R') + self.args << QString(self.raw_filename) + self.args << QString('--rawformat') + self.args << QString('json') + else: + self.raw_filename = None + + self.save_filename = save_filename + for arg in args: self.args << QString(arg) @@ -55,16 +76,17 @@ class SfiProcess(QObject): return self.process.state() != QProcess.NotRunning def processStandardOutput(self): - # NOTE: The signal readyReadStandardOutput is emitted when - # the process has made new data available through its standard output channel. - # But the process is not necessarily finished. - # It's cool to have this method wo we print the stdOut live, - # but we must be carefull with self.output, used by xmlTracker too. - print "SETTING SELF.OUTPUT" - tmpOut = self.process.readAllStandardOutput() + output = self.process.readAllStandardOutput() + self.output = self.output + output if config.debug: - print tmpOut - self.output += tmpOut + try: + print output + except IOError, e: + if (e.errno == 4): + # XXX why is this happening?? + print "*** caught EINTR" + else: + raise def processStandardError(self): @@ -87,13 +109,66 @@ class SfiProcess(QObject): print "ReadError" elif err == QProcess.UnknownError: print "UnknownError" + + self.raw_data = None + if (self.raw_filename != None) and os.path.exists(self.raw_filename): + if hasattr(json, "loads"): + self.raw_data = json.loads(open(self.raw_filename,"r").read()) # python 2.6 + else: + self.raw_data = json.read(open(self.raw_filename,"r").read()) # python 2.5 + + os.remove(self.raw_filename) + + self.geni_code=None + self.geni_output=None + if (self.raw_data!=None) and (type(self.raw_data)==dict): + code = self.raw_data.get("code",{}) + self.geni_code = code.get("geni_code",None) + self.geni_output = self.raw_data.get("output","") + + if self.save_filename: + value = self.raw_data.get("value", None) + if value == None: + if os.path.exists(self.save_filename): + os.remove(self.save_filename) + elif type(value) == str: + open(self.save_filename, "w").write(value) + elif hasattr(json, "dumps"): + open(self.save_filename, "w").write(json.dumps(value)) # python 2.6 + else: + open(self.save_filename, "w").write(json.write(value)) # python 2.5 + + # extract any faults from the XMLRPC response(s) + self.xmlrpcreader.responses = [] + self.xmlrpcreader.store(self.output) + self.xmlrpcreader.extractXml() + self.responses = self.xmlrpcreader.responses + self.faults = [x for x in self.responses if (x["kind"]=="fault")] + + # if we got a nonzero + if (self.geni_code!=None) and (self.geni_code!=0): + x = {"kind": "bad_geni_code"} + x["faultCode"] = str(self.geni_code) + faultString = "Nonzero geni_code: " + str(self.geni_code) + if self.geni_output: + faultString = faultString + " output: " + str(self.geni_output) + x["faultString"] = faultString + self.faults.append(x) + self.trace_end() self.emit(SIGNAL("finished()")) - def __getRSpec(self, mgr): + def getFaultString(self): + if self.faults == []: + return None + + return self.faults[0].get("faultString","") + " (" + self.faults[0].get("faultCode","") + ")" + + def retrieveRspec(self): slice = config.getSlice() - # Write RSpec to file for testing. - filename = os.path.expanduser("~/.sfi/" + slice + ".rspec") + mgr = config.getSlicemgr() + # Write RSpec to file + filename = config.fullpath ("%s.rspec"%slice) try: os.remove(filename) except: @@ -106,29 +181,158 @@ class SfiProcess(QObject): self.start() return filename - def getRSpecFromSM(self): - return self.__getRSpec(config.getSlicemgr()) + def retrieveResources(self): + mgr = config.getSlicemgr() + # Write RSpec to file + filename = config.getResourcesRSpecFile() + try: + os.remove(filename) + except: + pass + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", mgr, "resources", + "-o", filename] - def getRSpecFromAM(self): - return self.__getRSpec(config.getAggmgr()) + self.__init_command(args) + self.start() + return filename + + + def listRecords(self, hrn, rectype=None, filename=None): + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", "-F", "xmllist", hrn] + + if not filename: + filename = config.getAuthorityListFile() + + # we can't tell whether SFI will create one file or many, so delete + # leftovers from last time, then we'll know what we got, after we get it. + if os.path.exists(filename): + os.remove(filename) + if os.path.exists(filename + ".1"): + os.remove(filename + ".1") + args.append("-o") + args.append(filename) + + if rectype: + args.append("-t") + args.append(rectype) - def getRecord(self, hrn): - args = ["-u", config.getUser(), "-a", config.getAuthority(), - "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn] self.__init_command(args) self.start() - def applyRSpec(self, rspec): - filename = config.getSliceRSpecFile() + "_new" - rspec.save(filename) - args = ["-u", config.getUser(), "-a", config.getAuthority(), - "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create", - config.getSlice(), filename] + def getRecord(self, hrn, type=None, filename=None): + args = ["-u", config.getUser(), + "-a", config.getAuthority(), + "-r", config.getRegistry(), + "-s", config.getSlicemgr(), + "show", hrn] + if type: + args.append("-t") + args.append(type) + if filename: + args.append("-o") + args.append(filename) + self.__init_command(args) + self.start() + + def getSliceRecord(self): + self.getRecord(config.getSlice(), type="slice", filename=config.getSliceRecordFile()) + + def getAuthorityRecord(self): + self.getRecord(config.getAuthority(), filename=config.getAuthorityRecordFile()) + + def applyRSpec(self, rspec, aggAddr=None, aggPort=None, saveObtained=True): + # that's what we pass, like in what we'd like to get + requested = config.getSliceRSpecFile() + "_new" + # that's what we actually receive + # just overwrite the slice file as if we'd used 'resources' + obtained = config.getSliceRSpecFile() + rspec.save(requested) + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create"] + + if saveObtained: + args = args + ["-o", obtained] + + if aggAddr: + args = args + ["-a", aggAddr, "-p", str(aggPort)] + + args = args + [config.getSlice(), requested] + + self.__init_command(args) + self.start() + + def deleteSlivers(self, slice=None, aggAddr=None, aggPort=None): + if not slice: + slice = config.getSlice() + + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "delete"] + + if aggAddr: + args = args + ["-a", aggAddr, "-p", str(aggPort)] + + args = args + [slice] + + self.__init_command(args) + self.start() + + def updateRecord(self, filename): + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename] + self.__init_command(args) + self.start() + + def addRecord(self, filename): + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "add", filename] + self.__init_command(args) + self.start() + + def removeRecord(self, hrn): + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "remove", hrn] + self.__init_command(args) + self.start() + + def renewSlivers(self, expiration, slice=None): + if not slice: + slice = config.getSlice() + + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew", + slice, expiration] + self.__init_command(args) + self.start() + + def sliverStatus(self, slice=None, filename=None): + if not slice: + slice = config.getSlice() + + if not filename: + filename = config.fullpath(slice+".sliverstatus") + + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "status", + slice] + self.__init_command(args, save_filename = filename) + self.start() + + def getSliceMgrVersion(self, filename=None): + if not filename: + filename = config.fullpath("slicemgr.version") + + args = ["-u", config.getUser(), "-a", config.getAuthority(), + "-r", config.getRegistry(), "-s", config.getSlicemgr(), "version", + "-o", filename, "-F", "pickled",] self.__init_command(args) self.start() - return filename def start(self): + self.respones = [] + self.faults = [] + self.output = "" self.trace_command() self.process.start(self.exe, self.args) @@ -140,75 +344,12 @@ class SfiProcess(QObject): if config.verbose: self._trace=time.time() command = "%s %s" % (self.exe, self.args.join(" ")) - print time.strftime('%M:%S'),'Invoking',command + print time.strftime('%H:%M:%S'),'Invoking',command def trace_end (self): if config.verbose: - command = "%s %s" % (self.exe, self.args.join(" ")) - print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done' - self.xmltracker.getAndPrint(self.output) - -class XmlTracker(): - def __init__(self): - self.xmlrpcWindow = XmlrpcWindow() - - def getAndPrint(self, rawOutput): - self.store(rawOutput) - self.extractXml() - self.xmlrpcWindow.setData(self.xml) - self.showXmlrpc() - - def showXmlrpc(self): - self.xmlrpcWindow.show() - self.xmlrpcWindow.resize(500, 640) - self.xmlrpcWindow.raise_() - self.xmlrpcWindow.activateWindow() - - def store(self, rawOutput): - self.rawOutput = rawOutput - - def extractXml(self): - pttrnAsk = '.*?' - pttrnAns = '.*?' - answers = re.compile(pttrnAsk, re.DOTALL).findall(self.rawOutput) - replies = re.compile(pttrnAns, re.DOTALL).findall(self.rawOutput) - # cleaning - answers = map(lambda x: x.replace('\\n','\n'), answers) - replies = map(lambda x: x.replace('\\n','\n').replace("'\nbody: '", ''), replies) - replies.reverse() # so that I use pop() as popleft - # A well-formed XML document must have one, and only one, top-level element - self.xml = '' - for ans in answers: - self.xml += ans + replies.pop() - self.xml += '' - - def stats(self): - # statistics: round-trip time, size of the com - pass - - def prettyPrint(self, XmlComs): - # just for testing - print XmlComs - -class XmlrpcWindow(RSpecWindow): - def __init__(self, parent=None): - # super __init__() calls updateView, - # which assumes you have some data - self.data = '' - RSpecWindow.__init__(self, parent) - - def setData(self, XmlrpcCom): - self.data = XmlrpcCom - - def updateView(self): - del self.document - del self.model - self.document = None - self.model = None - - self.document = QDomDocument("RSpec") - self.document.setContent(self.data) - self.model = DomModel(self.document, self) - - self.view.setModel(self.model) - self.view.expand(self.model.index(0, 0)) #expand first level only +# command = "%s %s" % (self.exe, self.args.join(" ")) + print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace) + if config.debug: + get_tracker().getAndPrint(self.output) +