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()
40 print "DEBUG FLAG:", config.debug
42 # this shows xmlrpc conversation, see sfi.py docs.
43 self.args << QString('-D')
45 self.args << QString(arg)
47 self.exe = find_executable("sfi.py")
49 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
52 return self.process.state() != QProcess.NotRunning
54 def processStandardOutput(self):
55 # NOTE: The signal readyReadStandardOutput is emitted when
56 # the process has made new data available through its standard output channel.
57 # But the process is not necessarily finished.
58 # It's cool to have this method wo we print the stdOut live,
59 # but we must be carefull with self.output, used by XmlrpcTracker too.
60 tmpOut = self.process.readAllStandardOutput()
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 getRecord(self, hrn):
112 args = ["-u", config.getUser(), "-a", config.getAuthority(),
113 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
114 self.__init_command(args)
117 def applyRSpec(self, rspec):
118 filename = config.getSliceRSpecFile() + "_new"
120 args = ["-u", config.getUser(), "-a", config.getAuthority(),
121 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
122 config.getSlice(), filename]
123 self.__init_command(args)
129 self.process.start(self.exe, self.args)
131 def readOutput(self):
132 if self.process.state() == QProcess.NotRunning:
133 return self.process.readAll()
135 def trace_command (self):
137 self._trace=time.time()
138 command = "%s %s" % (self.exe, self.args.join(" "))
139 print time.strftime('%M:%S'),'Invoking',command
141 def trace_end (self):
143 command = "%s %s" % (self.exe, self.args.join(" "))
144 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'
145 self.xmlrpctracker.getAndPrint(self.output)