d39bd756cfe5b06b40937064eaa8a4ef738f054e
[sface.git] / sface / sfiprocess.py
1
2 import os
3 from PyQt4.QtCore import *
4 from sface.sficonfig import config
5
6 def find_executable(exec_name):
7     """find the given executable in $PATH"""
8     paths = os.getenv("PATH").split(':')
9     for p in paths:
10         exec_path = os.path.join(p, exec_name)
11         if os.path.exists(exec_path):
12             return exec_path
13     return None
14
15
16 class SfiProcess(QObject):
17     def __init__(self, parent=None):
18         QObject.__init__(self, parent)
19
20     def __init_command(self, args):
21         self.args = QStringList()
22         for arg in args:
23             self.args << QString(arg)
24
25         self.exe = find_executable("sfi.py")
26         self.process = QProcess()
27
28         self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
29                      self.processFinished)
30
31     def processFinished(self):
32         self.emit(SIGNAL("finished()"))
33
34     def __getRSpec(self, mgr):
35         slice = config.getSlice()
36         # Write RSpec to file for testing.
37         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
38         try:
39             os.remove(filename)
40         except:
41             pass
42         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
43                 "-r", config.registry(), "-s", mgr, "resources", 
44                 "-o", filename, slice]
45
46         self.__init_command(args)
47         self.start()
48         return filename
49
50     def getRSpecFromSM(self):
51         return self.__getRSpec(config.slicemgr())
52
53     def getRSpecFromAM(self):
54         return self.__getRSpec(config.aggmgr())
55
56     def getRecord(self, hrn):
57         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
58                 "-r", config.registry(), "-s", config.slicemgr(), "show", hrn]
59         self.__init_command(args)
60         self.start()
61
62     def start(self):
63         self.process.start(self.exe, self.args)
64
65     def readOutput(self):
66         if self.process.state() == QProcess.NotRunning:
67             return self.process.readAll()
68