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