refresh (update) slice data 1 second after submitting the new rspec.
[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         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:
42                 print "FailedToStart"
43             elif err == QProcess.Crashed:
44                 print "Crashed"
45             elif err == QProcess.Timedout:
46                 print "Timedout"
47             elif err == QProcess.WriteError:
48                 print "WriteError"
49             elif err == QProcess.ReadError:
50                 print "ReadError"
51             elif err == QProcess.UnknownError:
52                 print "UnknownError"
53         self.trace_end()
54         self.emit(SIGNAL("finished()"))
55
56     def __getRSpec(self, mgr):
57         slice = config.getSlice()
58         # Write RSpec to file for testing.
59         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
60         try:
61             os.remove(filename)
62         except:
63             pass
64         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
65                 "-r", config.getRegistry(), "-s", mgr, "resources", 
66                 "-o", filename, slice]
67
68         self.__init_command(args)
69         self.start()
70         return filename
71
72     def getRSpecFromSM(self):
73         return self.__getRSpec(config.getSlicemgr())
74
75     def getRSpecFromAM(self):
76         return self.__getRSpec(config.getAggmgr())
77
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)
82         self.start()
83
84     def applyRSpec(self, rspec):
85         filename = config.getSliceRSpecFile() + "_new"
86         rspec.save(filename)
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)
91         self.start()
92         return filename
93
94     def start(self):
95         self.trace_command()
96         self.process.start(self.exe, self.args)
97
98     def readOutput(self):
99         if self.process.state() == QProcess.NotRunning:
100             return self.process.readAll()
101
102     def trace_command (self):
103         if config.verbose:
104             self._trace=time.time()
105             command = "%s %s" % (self.exe, self.args.join(" "))
106             print time.strftime('%M:%S'),'Invoking',command
107
108     def trace_end (self):
109         if config.verbose:
110             command = "%s %s" % (self.exe, self.args.join(" "))
111             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'