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 self.process = QProcess()
25 self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
28 self.xmlrpctracker = XmlrpcTracker()
29 # in case self.output is read by the XmlrpcTracker before any
30 # readyReadStandardOutput signal
33 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
34 self.processStandardOutput)
35 self.connect(self.process, SIGNAL("readyReadStandardError()"),
36 self.processStandardError)
38 def __init_command(self, args):
39 self.args = QStringList()
41 # this shows xmlrpc conversation, see sfi.py docs.
42 self.args << QString('-D')
44 self.args << QString(arg)
46 self.exe = find_executable("sfi.py")
48 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
51 return self.process.state() != QProcess.NotRunning
53 def processStandardOutput(self):
54 # NOTE: The signal readyReadStandardOutput is emitted when
55 # the process has made new data available through its standard output channel.
56 # But the process is not necessarily finished.
57 # It's cool to have this method wo we print the stdOut live,
58 # but we must be carefull with self.output, used by XmlrpcTracker too.
59 tmpOut = self.process.readAllStandardOutput()
64 def processStandardError(self):
65 print self.process.readAllStandardError()
67 def processFinished(self):
68 if self.process.exitStatus() == QProcess.CrashExit:
69 print self.readOutput()
70 print "Process exited with errors:",
71 err = self.process.error()
72 if err == QProcess.FailedToStart:
74 elif err == QProcess.Crashed:
76 elif err == QProcess.Timedout:
78 elif err == QProcess.WriteError:
80 elif err == QProcess.ReadError:
82 elif err == QProcess.UnknownError:
85 self.emit(SIGNAL("finished()"))
87 def __getRSpec(self, mgr):
88 slice = config.getSlice()
89 # Write RSpec to file for testing.
90 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
95 args = ["-u", config.getUser(), "-a", config.getAuthority(),
96 "-r", config.getRegistry(), "-s", mgr, "resources",
97 "-o", filename, slice]
99 self.__init_command(args)
103 def getRSpecFromSM(self):
104 return self.__getRSpec(config.getSlicemgr())
106 def getRSpecFromAM(self):
107 return self.__getRSpec(config.getAggmgr())
109 def getRecord(self, hrn):
110 args = ["-u", config.getUser(), "-a", config.getAuthority(),
111 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
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)
127 self.process.start(self.exe, self.args)
129 def readOutput(self):
130 if self.process.state() == QProcess.NotRunning:
131 return self.process.readAll()
133 def trace_command (self):
135 self._trace=time.time()
136 command = "%s %s" % (self.exe, self.args.join(" "))
137 print time.strftime('%M:%S'),'Invoking',command
139 def trace_end (self):
141 command = "%s %s" % (self.exe, self.args.join(" "))
142 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'
143 self.xmlrpctracker.getAndPrint(self.output)