6 from PyQt4.QtCore import *
7 from sface.config import config
8 from sface.xmlrpcwindow import XmlrpcTracker
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.xmlrpctracker = XmlrpcTracker()
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 # this shows xmlrpc conversation, see sfi.py docs.
46 self.args << QString('-D')
48 self.args << QString(arg)
50 self.exe = find_executable("sfi.py")
52 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
55 return self.process.state() != QProcess.NotRunning
57 def processStandardOutput(self):
58 output = self.process.readAllStandardOutput()
59 self.output = self.output + output
63 def processStandardError(self):
64 print self.process.readAllStandardError()
66 def processFinished(self):
67 if self.process.exitStatus() == QProcess.CrashExit:
68 print self.readOutput()
69 print "Process exited with errors:",
70 err = self.process.error()
71 if err == QProcess.FailedToStart:
73 elif err == QProcess.Crashed:
75 elif err == QProcess.Timedout:
77 elif err == QProcess.WriteError:
79 elif err == QProcess.ReadError:
81 elif err == QProcess.UnknownError:
84 self.emit(SIGNAL("finished()"))
86 def __getRSpec(self, mgr):
87 slice = config.getSlice()
88 # Write RSpec to file for testing.
89 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
94 args = ["-u", config.getUser(), "-a", config.getAuthority(),
95 "-r", config.getRegistry(), "-s", mgr, "resources",
96 "-o", filename, slice]
98 self.__init_command(args)
102 def getRSpecFromSM(self):
103 return self.__getRSpec(config.getSlicemgr())
105 # def getRSpecFromAM(self):
106 # return self.__getRSpec(config.getAggmgr())
108 def listRecords(self, hrn, rectype=None, filename=None):
109 args = ["-u", config.getUser(), "-a", config.getAuthority(),
110 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
113 # we can't tell whether SFI will create one file or many, so delete
114 # leftovers from last time, then we'll know what we got, after we get it.
115 if os.path.exists(filename):
117 if os.path.exists(filename + ".1"):
118 os.remove(filename + ".1")
120 args.append(filename)
126 self.__init_command(args)
129 def getRecord(self, hrn, filename=None):
130 args = ["-u", config.getUser(), "-a", config.getAuthority(),
131 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
134 args.append(filename)
135 self.__init_command(args)
138 def getSliceRecord(self):
139 self.getRecord(config.getSlice(), config.getSliceRecordFile())
141 def getAuthorityRecord(self):
142 self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
144 def applyRSpec(self, rspec):
145 filename = config.getSliceRSpecFile() + "_new"
147 args = ["-u", config.getUser(), "-a", config.getAuthority(),
148 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
149 config.getSlice(), filename]
150 self.__init_command(args)
154 def updateRecord(self, filename):
155 args = ["-u", config.getUser(), "-a", config.getAuthority(),
156 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
157 self.__init_command(args)
160 def addRecord(self, filename):
161 args = ["-u", config.getUser(), "-a", config.getAuthority(),
162 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "add", filename]
163 self.__init_command(args)
166 def renewSlivers(self, expiration):
167 args = ["-u", config.getUser(), "-a", config.getAuthority(),
168 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
169 config.getSlice(), expiration]
170 self.__init_command(args)
176 self.process.start(self.exe, self.args)
178 def readOutput(self):
179 if self.process.state() == QProcess.NotRunning:
180 return self.process.readAll()
182 def trace_command (self):
184 self._trace=time.time()
185 command = "%s %s" % (self.exe, self.args.join(" "))
186 print time.strftime('%H:%M:%S'),'Invoking',command
188 def trace_end (self):
190 # command = "%s %s" % (self.exe, self.args.join(" "))
191 print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
193 self.xmlrpctracker.getAndPrint(self.output)