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)"),
26 self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
27 self.processStandardOutput)
28 self.connect(self.process, SIGNAL("readyReadStandardError()"),
29 self.processStandardError)
31 def __init_command(self, args):
32 self.args = QStringList()
34 self.args << QString(arg)
36 self.exe = find_executable("sfi.py")
38 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
41 return self.process.state() != QProcess.NotRunning
43 def processStandardOutput(self):
44 output = self.process.readAllStandardOutput()
48 def processStandardError(self):
49 print self.process.readAllStandardError()
51 def processFinished(self):
52 if self.process.exitStatus() == QProcess.CrashExit:
53 print self.readOutput()
54 print "Process exited with errors:",
55 err = self.process.error()
56 if err == QProcess.FailedToStart:
58 elif err == QProcess.Crashed:
60 elif err == QProcess.Timedout:
62 elif err == QProcess.WriteError:
64 elif err == QProcess.ReadError:
66 elif err == QProcess.UnknownError:
69 self.emit(SIGNAL("finished()"))
71 def __getRSpec(self, mgr):
72 slice = config.getSlice()
73 # Write RSpec to file for testing.
74 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
79 args = ["-u", config.getUser(), "-a", config.getAuthority(),
80 "-r", config.getRegistry(), "-s", mgr, "resources",
81 "-o", filename, slice]
83 self.__init_command(args)
87 def getRSpecFromSM(self):
88 return self.__getRSpec(config.getSlicemgr())
90 def getRSpecFromAM(self):
91 return self.__getRSpec(config.getAggmgr())
93 def getRecord(self, hrn):
94 args = ["-u", config.getUser(), "-a", config.getAuthority(),
95 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
96 self.__init_command(args)
99 def applyRSpec(self, rspec):
100 filename = config.getSliceRSpecFile() + "_new"
102 args = ["-u", config.getUser(), "-a", config.getAuthority(),
103 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
104 config.getSlice(), filename]
105 self.__init_command(args)
111 self.process.start(self.exe, self.args)
113 def readOutput(self):
114 if self.process.state() == QProcess.NotRunning:
115 return self.process.readAll()
117 def trace_command (self):
119 self._trace=time.time()
120 command = "%s %s" % (self.exe, self.args.join(" "))
121 print time.strftime('%M:%S'),'Invoking',command
123 def trace_end (self):
125 command = "%s %s" % (self.exe, self.args.join(" "))
126 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'