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 self.args << config.get_dirname()
48 # this shows xmlrpc conversation, see sfi.py docs.
49 self.args << QString('-D')
51 self.args << QString(arg)
53 self.exe = find_executable("sfi.py")
55 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
58 return self.process.state() != QProcess.NotRunning
60 def processStandardOutput(self):
61 output = self.process.readAllStandardOutput()
62 self.output = self.output + output
66 def processStandardError(self):
67 print self.process.readAllStandardError()
69 def processFinished(self):
70 if self.process.exitStatus() == QProcess.CrashExit:
71 print self.readOutput()
72 print "Process exited with errors:",
73 err = self.process.error()
74 if err == QProcess.FailedToStart:
76 elif err == QProcess.Crashed:
78 elif err == QProcess.Timedout:
80 elif err == QProcess.WriteError:
82 elif err == QProcess.ReadError:
84 elif err == QProcess.UnknownError:
87 self.emit(SIGNAL("finished()"))
89 def __getRSpec(self, mgr):
90 slice = config.getSlice()
91 # Write RSpec to file for testing.
92 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
97 args = ["-u", config.getUser(), "-a", config.getAuthority(),
98 "-r", config.getRegistry(), "-s", mgr, "resources",
99 "-o", filename, slice]
101 self.__init_command(args)
105 def getRSpecFromSM(self):
106 return self.__getRSpec(config.getSlicemgr())
108 # def getRSpecFromAM(self):
109 # return self.__getRSpec(config.getAggmgr())
111 def listRecords(self, hrn, rectype=None, filename=None):
112 args = ["-u", config.getUser(), "-a", config.getAuthority(),
113 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
116 # we can't tell whether SFI will create one file or many, so delete
117 # leftovers from last time, then we'll know what we got, after we get it.
118 if os.path.exists(filename):
120 if os.path.exists(filename + ".1"):
121 os.remove(filename + ".1")
123 args.append(filename)
129 self.__init_command(args)
132 def getRecord(self, hrn, filename=None):
133 args = ["-u", config.getUser(), "-a", config.getAuthority(),
134 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
137 args.append(filename)
138 self.__init_command(args)
141 def getSliceRecord(self):
142 self.getRecord(config.getSlice(), config.getSliceRecordFile())
144 def getAuthorityRecord(self):
145 self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
147 def applyRSpec(self, rspec):
148 filename = config.getSliceRSpecFile() + "_new"
150 args = ["-u", config.getUser(), "-a", config.getAuthority(),
151 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
152 config.getSlice(), filename]
153 self.__init_command(args)
157 def updateRecord(self, filename):
158 args = ["-u", config.getUser(), "-a", config.getAuthority(),
159 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
160 self.__init_command(args)
163 def addRecord(self, filename):
164 args = ["-u", config.getUser(), "-a", config.getAuthority(),
165 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "add", filename]
166 self.__init_command(args)
169 def renewSlivers(self, expiration):
170 args = ["-u", config.getUser(), "-a", config.getAuthority(),
171 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
172 config.getSlice(), expiration]
173 self.__init_command(args)
179 self.process.start(self.exe, self.args)
181 def readOutput(self):
182 if self.process.state() == QProcess.NotRunning:
183 return self.process.readAll()
185 def trace_command (self):
187 self._trace=time.time()
188 command = "%s %s" % (self.exe, self.args.join(" "))
189 print time.strftime('%H:%M:%S'),'Invoking',command
191 def trace_end (self):
193 # command = "%s %s" % (self.exe, self.args.join(" "))
194 print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
196 self.xmlrpctracker.getAndPrint(self.output)