correct class name of user screen
[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
33         # holds aggregate output from processStandardOutput(); used by xmlrpc
34         # tracker.
35         self.output = ""
36
37         self.connect(self.process, SIGNAL("readyReadStandardOutput()"),
38                      self.processStandardOutput)
39         self.connect(self.process, SIGNAL("readyReadStandardError()"),
40                      self.processStandardError)
41
42     def __init_command(self, args):
43         self.args = QStringList()
44         if config.debug:
45             # this shows xmlrpc conversation, see sfi.py docs.
46             self.args << QString('-D')
47         for arg in args:
48             self.args << QString(arg)
49
50         self.exe = find_executable("sfi.py")
51         if not self.exe:
52             print "FATAL.. Could not locate binary sfi.py - not much we can do without that"
53
54     def isRunning(self):
55         return self.process.state() != QProcess.NotRunning
56
57     def processStandardOutput(self):
58         output = self.process.readAllStandardOutput()
59         self.output = self.output + output
60         if config.debug:
61             print output
62
63     def processStandardError(self):
64         print self.process.readAllStandardError()
65
66     def processFinished(self):
67         if self.process.exitStatus() == QProcess.CrashExit:
68             print self.readOutput()
69             print "Process exited with errors:",
70             err = self.process.error()
71             if err == QProcess.FailedToStart:
72                 print "FailedToStart"
73             elif err == QProcess.Crashed:
74                 print "Crashed"
75             elif err == QProcess.Timedout:
76                 print "Timedout"
77             elif err == QProcess.WriteError:
78                 print "WriteError"
79             elif err == QProcess.ReadError:
80                 print "ReadError"
81             elif err == QProcess.UnknownError:
82                 print "UnknownError"
83         self.trace_end()
84         self.emit(SIGNAL("finished()"))
85
86     def __getRSpec(self, mgr):
87         slice = config.getSlice()
88         # Write RSpec to file for testing.
89         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
90         try:
91             os.remove(filename)
92         except:
93             pass
94         args = ["-u", config.getUser(), "-a", config.getAuthority(), 
95                 "-r", config.getRegistry(), "-s", mgr, "resources", 
96                 "-o", filename, slice]
97
98         self.__init_command(args)
99         self.start()
100         return filename
101
102     def getRSpecFromSM(self):
103         return self.__getRSpec(config.getSlicemgr())
104
105 #    def getRSpecFromAM(self):
106 #        return self.__getRSpec(config.getAggmgr())
107
108     def listRecords(self, hrn, rectype=None, filename=None):
109         args = ["-u", config.getUser(), "-a", config.getAuthority(),
110                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "list", hrn]
111
112         if filename:
113             # we can't tell whether SFI will create one file or many, so delete
114             # leftovers from last time, then we'll know what we got, after we get it.
115             if os.path.exists(filename):
116                 os.remove(filename)
117             if os.path.exists(filename + ".1"):
118                 os.remove(filename + ".1")
119             args.append("-o")
120             args.append(filename)
121
122         if rectype:
123             args.append("-t")
124             args.append(rectype)
125
126         self.__init_command(args)
127         self.start()
128
129     def getRecord(self, hrn, filename=None):
130         args = ["-u", config.getUser(), "-a", config.getAuthority(),
131                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "show", hrn]
132         if filename:
133             args.append("-o")
134             args.append(filename)
135         self.__init_command(args)
136         self.start()
137
138     def getSliceRecord(self):
139         self.getRecord(config.getSlice(), config.getSliceRecordFile())
140
141     def getAuthorityRecord(self):
142         self.getRecord(config.getAuthority(), config.getAuthorityRecordFile())
143
144     def applyRSpec(self, rspec):
145         filename = config.getSliceRSpecFile() + "_new"
146         rspec.save(filename)
147         args = ["-u", config.getUser(), "-a", config.getAuthority(),
148                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "create",
149                 config.getSlice(), filename]
150         self.__init_command(args)
151         self.start()
152         return filename
153
154     def updateRecord(self, filename):
155         args = ["-u", config.getUser(), "-a", config.getAuthority(),
156                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "update", filename]
157         self.__init_command(args)
158         self.start()
159
160     def renewSlivers(self, expiration):
161         args = ["-u", config.getUser(), "-a", config.getAuthority(),
162                 "-r", config.getRegistry(), "-s", config.getSlicemgr(), "renew",
163                 config.getSlice(), expiration]
164         self.__init_command(args)
165         self.start()
166
167     def start(self):
168         self.output = ""
169         self.trace_command()
170         self.process.start(self.exe, self.args)
171
172     def readOutput(self):
173         if self.process.state() == QProcess.NotRunning:
174             return self.process.readAll()
175
176     def trace_command (self):
177         if config.verbose:
178             self._trace=time.time()
179             command = "%s %s" % (self.exe, self.args.join(" "))
180             print time.strftime('%H:%M:%S'),'Invoking',command
181
182     def trace_end (self):
183         if config.verbose:
184 #            command = "%s %s" % (self.exe, self.args.join(" "))
185             print time.strftime('%H:%M:%S'),"Done [%.3f s]"%(time.time()-self._trace)
186         if config.debug:
187             self.xmlrpctracker.getAndPrint(self.output)
188