6 from PyQt4.QtCore import *
7 from sface.config import config
9 def find_executable(exec_name):
10 """find the given executable in $PATH"""
11 paths = os.getenv("PATH").split(':')
13 exec_path = os.path.join(p, exec_name)
14 if os.path.exists(exec_path):
19 class SfiProcess(QObject):
20 def __init__(self, parent=None):
21 QObject.__init__(self, parent)
23 self.process = QProcess()
24 self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
27 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
28 self.processStandardOutput)
29 self.connect(self.process, SIGNAL("readyReadStandardError()"),
30 self.processStandardError)
32 def __init_command(self, args):
33 self.args = QStringList()
35 self.args << QString(arg)
37 self.exe = find_executable("sfi.py")
39 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
42 return self.process.state() != QProcess.NotRunning
44 def processStandardOutput(self):
45 # NOTE: The signal readyReadStandardOutput is emitted when
46 # the process has made new data available through its standard output channel.
47 # But the process is not necessarily finished.
49 print self.process.readAllStandardOutput()
51 def processStandardError(self):
52 print self.process.readAllStandardError()
54 def processFinished(self):
55 if self.process.exitStatus() == QProcess.CrashExit:
56 print self.readOutput()
57 print "Process exited with errors:",
58 err = self.process.error()
59 if err == QProcess.FailedToStart:
61 elif err == QProcess.Crashed:
63 elif err == QProcess.Timedout:
65 elif err == QProcess.WriteError:
67 elif err == QProcess.ReadError:
69 elif err == QProcess.UnknownError:
72 self.emit(SIGNAL("finished()"))
74 def __getRSpec(self, mgr):
75 slice = config.getSlice()
76 # Write RSpec to file for testing.
77 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
82 args = ["-u", config.getUser(), "-a", config.getAuthority(),
83 "-r", config.getRegistry(), "-s", mgr, "resources",
84 "-o", filename, slice]
86 self.__init_command(args)
90 def getRSpecFromSM(self):
91 return self.__getRSpec(config.getSlicemgr())
93 def getRSpecFromAM(self):
94 return self.__getRSpec(config.getAggmgr())
96 def getRecord(self, hrn):
97 args = ["-u", config.getUser(), "-a", config.getAuthority(),
98 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
99 self.__init_command(args)
102 def applyRSpec(self, rspec):
103 filename = config.getSliceRSpecFile() + "_new"
105 args = ["-u", config.getUser(), "-a", config.getAuthority(),
106 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
107 config.getSlice(), filename]
108 self.__init_command(args)
114 self.process.start(self.exe, self.args)
116 def readOutput(self):
117 if self.process.state() == QProcess.NotRunning:
118 return self.process.readAll()
120 def trace_command (self):
122 self._trace=time.time()
123 command = "%s %s" % (self.exe, self.args.join(" "))
124 print time.strftime('%M:%S'),'Invoking',command
126 def trace_end (self):
128 command = "%s %s" % (self.exe, self.args.join(" "))
129 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'