on the way of getting rid of HTML in XML viz. The model passes a QObject instead...
[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         self.process = QProcess()
25         self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
26                      self.processFinished)
27         
28         self.xmlrpctracker = XmlrpcTracker()
29         # in case self.output is read by the XmlrpcTracker before any
30         # readyReadStandardOutput signal
31         self.output = ''
32
33         self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
34                      self.processStandardOutput)
35         self.connect(self.process, SIGNAL("readyReadStandardError()"),
36                      self.processStandardError)
37
38     def __init_command(self, args):
39         self.args = QStringList()
40         if config.debug:
41             # this shows xmlrpc conversation, see sfi.py docs.
42             self.args << QString('-D')
43         for arg in args:
44             self.args << QString(arg)
45
46         self.exe = find_executable("sfi.py")
47         if not self.exe:
48             print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
49
50     def isRunning(self):
51         return self.process.state() != QProcess.NotRunning
52
53     def processStandardOutput(self):
54         # NOTE: The signal readyReadStandardOutput is emitted when
55         # the process has made new data available through its standard output channel.
56         # But the process is not necessarily finished.
57         # It's cool to have this method wo we print the stdOut live,
58         # but we must be carefull with self.output, used by XmlrpcTracker too.
59         tmpOut = self.process.readAllStandardOutput()
60         if config.debug:
61             print tmpOut        
62         self.output += tmpOut
63
64     def processStandardError(self):
65         print self.process.readAllStandardError()
66
67     def processFinished(self):
68         if self.process.exitStatus() == QProcess.CrashExit:
69             print self.readOutput()
70             print "Process exited with errors:",
71             err = self.process.error()
72             if err == QProcess.FailedToStart:
73                 print "FailedToStart"
74             elif err == QProcess.Crashed:
75                 print "Crashed"
76             elif err == QProcess.Timedout:
77                 print "Timedout"
78             elif err == QProcess.WriteError:
79                 print "WriteError"
80             elif err == QProcess.ReadError:
81                 print "ReadError"
82             elif err == QProcess.UnknownError:
83                 print "UnknownError"
84         self.trace_end()
85         self.emit(SIGNAL("finished()"))
86
87     def __getRSpec(self, mgr):
88         slice = config.getSlice()
89         # Write RSpec to file for testing.
90         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
91         try:
92             os.remove(filename)
93         except:
94             pass
95         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
96                 "-r", config.getRegistry(), "-s", mgr, "resources", 
97                 "-o", filename, slice]
98
99         self.__init_command(args)
100         self.start()
101         return filename
102
103     def getRSpecFromSM(self):
104         return self.__getRSpec(config.getSlicemgr())
105
106     def getRSpecFromAM(self):
107         return self.__getRSpec(config.getAggmgr())
108
109     def getRecord(self, hrn):
110         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
111                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
112         self.__init_command(args)
113         self.start()
114
115     def applyRSpec(self, rspec):
116         filename = config.getSliceRSpecFile() + "_new"
117         rspec.save(filename)
118         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
119                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create", 
120                 config.getSlice(), filename]
121         self.__init_command(args)
122         self.start()
123         return filename
124
125     def start(self):
126         self.trace_command()
127         self.process.start(self.exe, self.args)
128
129     def readOutput(self):
130         if self.process.state() == QProcess.NotRunning:
131             return self.process.readAll()
132
133     def trace_command (self):
134         if config.verbose:
135             self._trace=time.time()
136             command = "%s %s" % (self.exe, self.args.join(" "))
137             print time.strftime('%M:%S'),'Invoking',command
138
139     def trace_end (self):
140         if config.verbose:
141             command = "%s %s" % (self.exe, self.args.join(" "))
142             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),command,'Done'
143             self.xmlrpctracker.getAndPrint(self.output)
144