6 from PyQt4.QtCore import *
7 from sface.config import config
8 from sface.xmlrpcwindow import get_tracker, 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
33 # holds aggregate output from processStandardOutput(); used by xmlrpc
37 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
38 self.processStandardOutput)
39 self.connect(self.process, SIGNAL("readyReadStandardError()"),
40 self.processStandardError)
42 def __init_command(self, args):
43 self.args = QStringList()
45 self.args << config.get_dirname()
47 # this shows xmlrpc conversation, see sfi.py docs.
48 # always do this, so we can parse the XML result for faults and show
50 self.args << QString('-D')
53 self.args << QString(arg)
55 self.exe = find_executable("sfi.py")
57 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
60 return self.process.state() != QProcess.NotRunning
62 def processStandardOutput(self):
63 output = self.process.readAllStandardOutput()
64 self.output = self.output + output
68 def processStandardError(self):
69 print self.process.readAllStandardError()
71 def processFinished(self):
72 if self.process.exitStatus() == QProcess.CrashExit:
73 print self.readOutput()
74 print "Process exited with errors:",
75 err = self.process.error()
76 if err == QProcess.FailedToStart:
78 elif err == QProcess.Crashed:
80 elif err == QProcess.Timedout:
82 elif err == QProcess.WriteError:
84 elif err == QProcess.ReadError:
86 elif err == QProcess.UnknownError:
89 # extract any faults from the XMLRPC response(s)
90 self.xmlrpcreader.responses = []
91 self.xmlrpcreader.store(self.output)
92 self.xmlrpcreader.extractXml()
93 self.responses = self.xmlrpcreader.responses
94 self.faults = [x for x in self.responses if (x["kind"]=="fault")]
97 self.emit(SIGNAL("finished()"))
99 def getFaultString(self):
100 if self.faults == []:
103 return self.faults[0].get("faultString","") + " (" + self.faults[0].get("faultCode","") + ")"
105 def retrieveRspec(self):
106 slice = config.getSlice()
107 mgr = config.getSlicemgr()
108 # Write RSpec to file
109 filename = config.fullpath ("%s.rspec"%slice)
114 args = ["-u", config.getUser(), "-a", config.getAuthority(),
115 "-r", config.getRegistry(), "-s", mgr, "resources",
116 "-o", filename, slice]
118 self.__init_command(args)
123 def listRecords(self, hrn, rectype=None, filename=None):
124 args = ["-u", config.getUser(), "-a", config.getAuthority(),
125 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
128 # we can't tell whether SFI will create one file or many, so delete
129 # leftovers from last time, then we'll know what we got, after we get it.
130 if os.path.exists(filename):
132 if os.path.exists(filename + ".1"):
133 os.remove(filename + ".1")
135 args.append(filename)
141 self.__init_command(args)
144 def getRecord(self, hrn, filename=None):
145 args = ["-u", config.getUser(), "-a", config.getAuthority(),
146 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
149 args.append(filename)
150 self.__init_command(args)
153 def getSliceRecord(self):
154 self.getRecord(config.getSlice(), config.getSliceRecordFile())
156 def getAuthorityRecord(self):
157 self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
159 def applyRSpec(self, rspec):
160 # that's what we pass, like in what we'd like to get
161 requested = config.getSliceRSpecFile() + "_new"
162 # that's what we actually receive
163 # just overwrite the slice file as if we'd used 'resources'
164 obtained = config.getSliceRSpecFile()
165 rspec.save(requested)
166 args = ["-u", config.getUser(), "-a", config.getAuthority(),
167 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
169 config.getSlice(), requested]
170 self.__init_command(args)
173 def updateRecord(self, filename):
174 args = ["-u", config.getUser(), "-a", config.getAuthority(),
175 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
176 self.__init_command(args)
179 def addRecord(self, filename):
180 args = ["-u", config.getUser(), "-a", config.getAuthority(),
181 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "add", filename]
182 self.__init_command(args)
185 def renewSlivers(self, expiration):
186 args = ["-u", config.getUser(), "-a", config.getAuthority(),
187 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
188 config.getSlice(), expiration]
189 self.__init_command(args)
197 self.process.start(self.exe, self.args)
199 def readOutput(self):
200 if self.process.state() == QProcess.NotRunning:
201 return self.process.readAll()
203 def trace_command (self):
205 self._trace=time.time()
206 command = "%s %s" % (self.exe, self.args.join(" "))
207 print time.strftime('%H:%M:%S'),'Invoking',command
209 def trace_end (self):
211 # command = "%s %s" % (self.exe, self.args.join(" "))
212 print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
214 get_tracker().getAndPrint(self.output)