5 from PyQt4.QtCore import *
6 from sface.config import config
8 def find_executable(exec_name):
9 """find the given executable in $PATH"""
10 paths = os.getenv("PATH").split(':')
12 exec_path = os.path.join(p, exec_name)
13 if os.path.exists(exec_path):
18 class SfiProcess(QObject):
19 def __init__(self, parent=None):
20 QObject.__init__(self, parent)
22 def __init_command(self, args):
23 self.args = QStringList()
25 self.args << QString(arg)
27 self.exe = find_executable("sfi.py")
29 print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
31 self.process = QProcess()
33 self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
36 def processFinished(self):
37 if self.process.exitStatus() == QProcess.CrashExit:
38 print self.readOutput()
39 print "Process exited with errors:",
40 err = self.process.error()
41 if err == QProcess.FailedToStart:
43 elif err == QProcess.Crashed:
45 elif err == QProcess.Timedout:
47 elif err == QProcess.WriteError:
49 elif err == QProcess.ReadError:
51 elif err == QProcess.UnknownError:
54 self.emit(SIGNAL("finished()"))
56 def __getRSpec(self, mgr):
57 slice = config.getSlice()
58 # Write RSpec to file for testing.
59 filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
64 args = ["-u", config.getUser(), "-a", config.getAuthority(),
65 "-r", config.getRegistry(), "-s", mgr, "resources",
66 "-o", filename, slice]
68 self.__init_command(args)
72 def getRSpecFromSM(self):
73 return self.__getRSpec(config.getSlicemgr())
75 def getRSpecFromAM(self):
76 return self.__getRSpec(config.getAggmgr())
78 def getRecord(self, hrn):
79 args = ["-u", config.getUser(), "-a", config.getAuthority(),
80 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
81 self.__init_command(args)
84 def applyRSpec(self, rspec):
85 filename = config.getSliceRSpecFile() + "_new"
87 args = ["-u", config.getUser(), "-a", config.getAuthority(),
88 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
89 config.getSlice(), filename]
90 self.__init_command(args)
96 self.process.start(self.exe, self.args)
99 if self.process.state() == QProcess.NotRunning:
100 return self.process.readAll()
102 def trace_command (self):
104 self._trace=time.time()
105 command = "%s %s" % (self.exe, self.args.join(" "))
106 print time.strftime('%M:%S'),'Invoking',command
108 def trace_end (self):
110 command = "%s %s" % (self.exe, self.args.join(" "))
111 print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'