another logo
[sface.git] / sface / sfiprocess.py
1
2 import os
3 import sys
4 import time
5
6 from PyQt4.QtCore import *
7 from sface.config import config
8 from sface.xmlrpcwindow import XmlrpcTracker
9
10 def find_executable(exec_name):
11     """find the given executable in $PATH"""
12     paths = os.getenv("PATH").split(':')
13     for p in paths:
14         exec_path = os.path.join(p, exec_name)
15         if os.path.exists(exec_path):
16             return exec_path
17     return None
18
19
20 class SfiProcess(QObject):
21     def __init__(self, parent=None):
22         QObject.__init__(self, parent)
23
24         env = QProcess.systemEnvironment()
25         env << "PYTHONPATH=%s" % ":".join(sys.path)
26         self.process = QProcess()
27         self.process.setEnvironment(env)
28         self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
29                      self.processFinished)
30         
31         self.xmlrpctracker = XmlrpcTracker()
32         # in case self.output is read by the XmlrpcTracker before any
33         # readyReadStandardOutput signal
34         self.output = ''
35
36         self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
37                      self.processStandardOutput)
38         self.connect(self.process, SIGNAL("readyReadStandardError()"),
39                      self.processStandardError)
40
41     def __init_command(self, args):
42         self.args = QStringList()
43         if config.debug:
44             # this shows xmlrpc conversation, see sfi.py docs.
45             self.args << QString('-D')
46         for arg in args:
47             self.args << QString(arg)
48
49         self.exe = find_executable("sfi.py")
50         if not self.exe:
51             print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
52
53     def isRunning(self):
54         return self.process.state() != QProcess.NotRunning
55
56     def processStandardOutput(self):
57         output = self.process.readAllStandardOutput()
58         if config.debug:
59             print output
60
61     def processStandardError(self):
62         print self.process.readAllStandardError()
63
64     def processFinished(self):
65         if self.process.exitStatus() == QProcess.CrashExit:
66             print self.readOutput()
67             print "Process exited with errors:",
68             err = self.process.error()
69             if err == QProcess.FailedToStart:
70                 print "FailedToStart"
71             elif err == QProcess.Crashed:
72                 print "Crashed"
73             elif err == QProcess.Timedout:
74                 print "Timedout"
75             elif err == QProcess.WriteError:
76                 print "WriteError"
77             elif err == QProcess.ReadError:
78                 print "ReadError"
79             elif err == QProcess.UnknownError:
80                 print "UnknownError"
81         self.trace_end()
82         self.emit(SIGNAL("finished()"))
83
84     def __getRSpec(self, mgr):
85         slice = config.getSlice()
86         # Write RSpec to file for testing.
87         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
88         try:
89             os.remove(filename)
90         except:
91             pass
92         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
93                 "-r", config.getRegistry(), "-s", mgr, "resources", 
94                 "-o", filename, slice]
95
96         self.__init_command(args)
97         self.start()
98         return filename
99
100     def getRSpecFromSM(self):
101         return self.__getRSpec(config.getSlicemgr())
102
103 #    def getRSpecFromAM(self):
104 #        return self.__getRSpec(config.getAggmgr())
105
106     def getRecord(self, hrn):
107         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
108                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
109         self.__init_command(args)
110         self.start()
111
112     def applyRSpec(self, rspec):
113         filename = config.getSliceRSpecFile() + "_new"
114         rspec.save(filename)
115         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
116                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create", 
117                 config.getSlice(), filename]
118         self.__init_command(args)
119         self.start()
120         return filename
121
122     def start(self):
123         self.trace_command()
124         self.process.start(self.exe, self.args)
125
126     def readOutput(self):
127         if self.process.state() == QProcess.NotRunning:
128             return self.process.readAll()
129
130     def trace_command (self):
131         if config.verbose:
132             self._trace=time.time()
133             command = "%s %s" % (self.exe, self.args.join(" "))
134             print time.strftime('%H:%M:%S'),'Invoking',command
135
136     def trace_end (self):
137         if config.verbose:
138 #            command = "%s %s" % (self.exe, self.args.join(" "))
139             print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
140         if config.debug:
141             self.xmlrpctracker.getAndPrint(self.output)
142