use QProcess instead of subprocess
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 8 Sep 2010 16:17:50 +0000 (18:17 +0200)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 8 Sep 2010 16:17:50 +0000 (18:17 +0200)
sfadata.py

index 42b5642..040a0ba 100644 (file)
@@ -1,9 +1,31 @@
 import os
 import re
 import time
-import subprocess
+from PyQt4.QtCore import QProcess, QString, QStringList
+
 from sfa.util.rspecHelper import RSpec
 
+
+def find_executable(exec_name):
+    """find the given executable in $PATH"""
+    paths = os.getenv("PATH").split(':')
+    for p in paths:
+        exec_path = os.path.join(p, exec_name)
+        if os.path.exists(exec_path):
+            return exec_path
+    return None
+
+def process(command):
+    SFI_CMD = find_executable("sfi.py")
+    arguments = QStringList()
+    for c in command:
+        arguments << QString(c)
+    process = QProcess()
+    process.start(SFI_CMD, arguments)
+    process.waitForFinished()
+    print process.readAll()
+
+
 class SfaData:
     defaults = { 'SFI_AUTH' : None,
                  'SFI_USER' : None,
@@ -86,19 +108,19 @@ class SfaData:
             print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
 
     def getRecord(self, hrn):
-        command = ["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
+        command = ["-u", self.getUser(), "-a", self.getAuthority(), 
                    "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
         self.trace_command(command)
-        text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
+        process(command)
         self.trace_end()
         return text
 
     def listChildren(self, hrn):
         children = []
-        command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
+        command=["-u", self.getUser(), "-a", self.getAuthority(), 
                  "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
         self.trace_command(command)
-        text = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
+        process(command)
         self.trace_end()
         lines = text.split('\n')
         for line in lines:
@@ -109,12 +131,12 @@ class SfaData:
         return children
 
     def getRSpecFromSM(self):
-        return __getRSpec(self.slicemgr)
+        return self.__getRSpec(self.slicemgr())
 
     def getRSpecFromAM(self):
-        return __getRSpec(self.aggmgr)
+        return self.__getRSpec(self.aggmgr())
 
-    def __getRspec(self, mgr):
+    def __getRSpec(self, mgr):
         slice = self.getSlice()
         # Write RSpec to file for testing.
         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
@@ -122,11 +144,11 @@ class SfaData:
             os.remove(filename)
         except:
             pass
-        command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
+        command=["-u", self.getUser(), "-a", self.getAuthority(), 
                  "-r", self.registry(), "-s", mgr, "resources", 
                  "-o", filename, slice]
         self.trace_command(command)
-        subprocess.call(command)
+        process(command)
         self.trace_end()
         f = open(filename, "r")
         xml = f.read()
@@ -139,10 +161,10 @@ class SfaData:
         f = open(filename, "w")
         f.write(xml)
         f.close()
-        command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
+        command=["-u", self.getUser(), "-a", self.getAuthority(), 
                  "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
         self.trace_command(command)
-        subprocess.call(command)
+        process(command)
         self.trace_end()
 
 class PlanetLabData(SfaData):