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()
32 # in case self.output is read by the XmlrpcTracker before any
33 # readyReadStandardOutput signal
36 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
37 self.processStandardOutput)
38 self.connect(self.process, SIGNAL("readyReadStandardError()"),
39 self.processStandardError)
41 def __init_command(self, args):
42 self.args = QStringList()
44 # this shows xmlrpc conversation, see sfi.py docs.
45 self.args << QString('-D')
47 self.args << QString(arg)
49 self.exe = find_executable("sfi.py")
51 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
54 return self.process.state() != QProcess.NotRunning
56 def processStandardOutput(self):
57 output = self.process.readAllStandardOutput()
61 def processStandardError(self):
62 print self.process.readAllStandardError()
64 def processFinished(self):
65 if self.process.exitStatus() == QProcess.CrashExit:
66 print self.readOutput()
67 print "Process exited with errors:",
68 err = self.process.error()
69 if err == QProcess.FailedToStart:
71 elif err == QProcess.Crashed:
73 elif err == QProcess.Timedout:
75 elif err == QProcess.WriteError:
77 elif err == QProcess.ReadError:
79 elif err == QProcess.UnknownError:
82 self.emit(SIGNAL("finished()"))
84 def __getRSpec(self, mgr):
85 slice = config.getSlice()
86 # Write RSpec to file for testing.
87 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
92 args = ["-u", config.getUser(), "-a", config.getAuthority(),
93 "-r", config.getRegistry(), "-s", mgr, "resources",
94 "-o", filename, slice]
96 self.__init_command(args)
100 def getRSpecFromSM(self):
101 return self.__getRSpec(config.getSlicemgr())
103 # def getRSpecFromAM(self):
104 # return self.__getRSpec(config.getAggmgr())
106 def getRecord(self, hrn, filename=None):
107 args = ["-u", config.getUser(), "-a", config.getAuthority(),
108 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
111 args.append(filename)
112 self.__init_command(args)
115 def applyRSpec(self, rspec):
116 filename = config.getSliceRSpecFile() + "_new"
118 args = ["-u", config.getUser(), "-a", config.getAuthority(),
119 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
120 config.getSlice(), filename]
121 self.__init_command(args)
125 def updateRecord(self, filename):
126 args = ["-u", config.getUser(), "-a", config.getAuthority(),
127 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
128 self.__init_command(args)
131 def renewSlivers(self, expiration):
132 args = ["-u", config.getUser(), "-a", config.getAuthority(),
133 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
134 config.getSlice(), expiration]
135 self.__init_command(args)
140 self.process.start(self.exe, self.args)
142 def readOutput(self):
143 if self.process.state() == QProcess.NotRunning:
144 return self.process.readAll()
146 def trace_command (self):
148 self._trace=time.time()
149 command = "%s %s" % (self.exe, self.args.join(" "))
150 print time.strftime('%H:%M:%S'),'Invoking',command
152 def trace_end (self):
154 # command = "%s %s" % (self.exe, self.args.join(" "))
155 print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
157 self.xmlrpctracker.getAndPrint(self.output)