start select delegate.
[sface.git] / sface / sfiprocess.py
1
2 import os
3 import time
4
5 from PyQt4.QtCore import *
6 from sface.config import config
7
8 def find_executable(exec_name):
9     """find the given executable in $PATH"""
10     paths = os.getenv("PATH").split(':')
11     for p in paths:
12         exec_path = os.path.join(p, exec_name)
13         if os.path.exists(exec_path):
14             return exec_path
15     return None
16
17
18 class SfiProcess(QObject):
19     def __init__(self, parent=None):
20         QObject.__init__(self, parent)
21
22     def __init_command(self, args):
23         self.args = QStringList()
24         for arg in args:
25             self.args << QString(arg)
26
27         self.exe = find_executable("sfi.py")
28         if not self.exe:
29             print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
30
31         self.process = QProcess()
32
33         self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
34                      self.processFinished)
35
36     def processFinished(self):
37         self.trace_end()
38         self.emit(SIGNAL("finished()"))
39
40     def __getRSpec(self, mgr):
41         slice = config.getSlice()
42         # Write RSpec to file for testing.
43         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
44         try:
45             os.remove(filename)
46         except:
47             pass
48         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
49                 "-r", config.getRegistry(), "-s", mgr, "resources", 
50                 "-o", filename, slice]
51
52         self.__init_command(args)
53         self.start()
54         return filename
55
56     def getRSpecFromSM(self):
57         return self.__getRSpec(config.getSlicemgr())
58
59     def getRSpecFromAM(self):
60         return self.__getRSpec(config.getAggmgr())
61
62     def getRecord(self, hrn):
63         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
64                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
65         self.__init_command(args)
66         self.start()
67
68     def start(self):
69         self.trace_command()
70         self.process.start(self.exe, self.args)
71
72     def readOutput(self):
73         if self.process.state() == QProcess.NotRunning:
74             return self.process.readAll()
75
76     def trace_command (self):
77         if config.verbose:
78             self._trace=time.time()
79             command = "%s %s" % (self.exe, self.args.join(" "))
80             print time.strftime('%M:%S'),'Invoking',command
81
82     def trace_end (self):
83         if config.verbose:
84             command = "%s %s" % (self.exe, self.args.join(" "))
85             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'