6 from PyQt4.QtCore import *
7 from sface.config import config
8 from sface.xmlrpcwindow import XmlrpcTracker, XmlrpcReader
10 def find_executable(exec_name):
11 """find the given executable in $PATH"""
12 paths = os.getenv("PATH").split(':')
14 exec_path = os.path.join(p, exec_name)
15 if os.path.exists(exec_path):
20 class SfiProcess(QObject):
21 def __init__(self, parent=None):
22 QObject.__init__(self, parent)
24 env = QProcess.systemEnvironment()
25 env << "PYTHONPATH=%s" % ":".join(sys.path)
26 self.process = QProcess()
27 self.process.setEnvironment(env)
28 self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
31 self.xmlrpcreader = XmlrpcReader() # this one is for parsing XMLRPC responses
32 self.xmlrpctracker = XmlrpcTracker() # this one is for the debug window
34 # holds aggregate output from processStandardOutput(); used by xmlrpc
38 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
39 self.processStandardOutput)
40 self.connect(self.process, SIGNAL("readyReadStandardError()"),
41 self.processStandardError)
43 def __init_command(self, args):
44 self.args = QStringList()
46 self.args << config.get_dirname()
48 # this shows xmlrpc conversation, see sfi.py docs.
49 # always do this, so we can parse the XML result for faults and show
51 self.args << QString('-D')
54 self.args << QString(arg)
56 self.exe = find_executable("sfi.py")
58 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
61 return self.process.state() != QProcess.NotRunning
63 def processStandardOutput(self):
64 output = self.process.readAllStandardOutput()
65 self.output = self.output + output
69 def processStandardError(self):
70 print self.process.readAllStandardError()
72 def processFinished(self):
73 if self.process.exitStatus() == QProcess.CrashExit:
74 print self.readOutput()
75 print "Process exited with errors:",
76 err = self.process.error()
77 if err == QProcess.FailedToStart:
79 elif err == QProcess.Crashed:
81 elif err == QProcess.Timedout:
83 elif err == QProcess.WriteError:
85 elif err == QProcess.ReadError:
87 elif err == QProcess.UnknownError:
90 # extract any faults from the XMLRPC response(s)
91 self.xmlrpcreader.responses = []
92 self.xmlrpcreader.store(self.output)
93 self.xmlrpcreader.extractXml()
94 self.responses = self.xmlrpcreader.responses
95 self.faults = [x for x in self.responses if (x["kind"]=="fault")]
98 self.emit(SIGNAL("finished()"))
100 def getFaultString(self):
101 if self.faults == []:
104 return self.faults[0].get("faultString","") + " (" + self.faults[0].get("faultCode","") + ")"
106 def retrieveRspec(self):
107 slice = config.getSlice()
108 mgr = config.getSlicemgr()
109 # Write RSpec to file
110 filename = config.fullpath ("%s.rspec"%slice)
115 args = ["-u", config.getUser(), "-a", config.getAuthority(),
116 "-r", config.getRegistry(), "-s", mgr, "resources",
117 "-o", filename, slice]
119 self.__init_command(args)
124 def listRecords(self, hrn, rectype=None, filename=None):
125 args = ["-u", config.getUser(), "-a", config.getAuthority(),
126 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
129 # we can't tell whether SFI will create one file or many, so delete
130 # leftovers from last time, then we'll know what we got, after we get it.
131 if os.path.exists(filename):
133 if os.path.exists(filename + ".1"):
134 os.remove(filename + ".1")
136 args.append(filename)
142 self.__init_command(args)
145 def getRecord(self, hrn, filename=None):
146 args = ["-u", config.getUser(), "-a", config.getAuthority(),
147 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
150 args.append(filename)
151 self.__init_command(args)
154 def getSliceRecord(self):
155 self.getRecord(config.getSlice(), config.getSliceRecordFile())
157 def getAuthorityRecord(self):
158 self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
160 def applyRSpec(self, rspec):
161 # that's what we pass, like in what we'd like to get
162 requested = config.getSliceRSpecFile() + "_new"
163 # that's what we actually receive
164 # just overwrite the slice file as if we'd used 'resources'
165 obtained = config.getSliceRSpecFile()
166 rspec.save(requested)
167 args = ["-u", config.getUser(), "-a", config.getAuthority(),
168 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
170 config.getSlice(), requested]
171 self.__init_command(args)
174 def updateRecord(self, filename):
175 args = ["-u", config.getUser(), "-a", config.getAuthority(),
176 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
177 self.__init_command(args)
180 def addRecord(self, filename):
181 args = ["-u", config.getUser(), "-a", config.getAuthority(),
182 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "add", filename]
183 self.__init_command(args)
186 def renewSlivers(self, expiration):
187 args = ["-u", config.getUser(), "-a", config.getAuthority(),
188 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
189 config.getSlice(), expiration]
190 self.__init_command(args)
198 self.process.start(self.exe, self.args)
200 def readOutput(self):
201 if self.process.state() == QProcess.NotRunning:
202 return self.process.readAll()
204 def trace_command (self):
206 self._trace=time.time()
207 command = "%s %s" % (self.exe, self.args.join(" "))
208 print time.strftime('%H:%M:%S'),'Invoking',command
210 def trace_end (self):
212 # command = "%s %s" % (self.exe, self.args.join(" "))
213 print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
215 self.xmlrpctracker.getAndPrint(self.output)