fix nodeview issues and expand all
[sface.git] / sface / sfiprocess.py
1
2 import os
3 import time
4
5 from PyQt4.QtCore import *
6 from sface.sficonfig 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         self.process = QProcess()
29
30         self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
31                      self.processFinished)
32
33     def processFinished(self):
34         self.trace_end()
35         self.emit(SIGNAL("finished()"))
36
37     def __getRSpec(self, mgr):
38         slice = config.getSlice()
39         # Write RSpec to file for testing.
40         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
41         try:
42             os.remove(filename)
43         except:
44             pass
45         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
46                 "-r", config.registry(), "-s", mgr, "resources", 
47                 "-o", filename, slice]
48
49         self.__init_command(args)
50         self.start()
51         return filename
52
53     def getRSpecFromSM(self):
54         return self.__getRSpec(config.slicemgr())
55
56     def getRSpecFromAM(self):
57         return self.__getRSpec(config.aggmgr())
58
59     def getRecord(self, hrn):
60         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
61                 "-r", config.registry(), "-s", config.slicemgr(), "show", hrn]
62         self.__init_command(args)
63         self.start()
64
65     def start(self):
66         self.trace_command()
67         self.process.start(self.exe, self.args)
68
69     def readOutput(self):
70         if self.process.state() == QProcess.NotRunning:
71             return self.process.readAll()
72
73     def trace_command (self):
74         if config.SFACE_VERBOSE:
75             self._trace=time.time()
76             command = "%s %s" % (self.exe, self.args.join(" "))
77             print time.strftime('%M:%S'),'Invoking',command
78
79     def trace_end (self):
80         if config.SFACE_VERBOSE:
81             command = "%s %s" % (self.exe, self.args.join(" "))
82             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'