displays if assigned or not, currently assigned first
[sface.git] / sfadata.py
index 433868e..1fc3708 100644 (file)
@@ -1,28 +1,78 @@
 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( 200000 )
+    print process.readAll()
+
+
 class SfaData:
     defaults = { 'SFI_AUTH' : None,
                  'SFI_USER' : None,
                  'SFI_SLICE' : None,
                  'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
-                 'SFI_SM' : "http://www.planet-lab.org:12346",
-                 'SFAUI_VERBOSE' : False,
+                 'SFI_AM' : "http://www.planet-lab.org:12346",
+                 'SFI_SM' : "http://www.planet-lab.org:12347",
+                 'SFACE_VERBOSE' : False,
+                 'SFACE_DEBUG' : False,
                  }
 
-    def __init__(self):
+    # let the UI set the slicename if that was set by user
+    def __init__(self, slice=None):
+        self.read_config()
+
+    def read_config(self):
         filename = os.path.expanduser("~/.sfi/sfi_config")
         execfile(filename, SfaData.__dict__)
         for (k,v) in SfaData.defaults.items():
             if not hasattr(SfaData,k): setattr(SfaData,k,v)
-        if SfaData.SFAUI_VERBOSE:
+        if SfaData.SFACE_VERBOSE:
             print "After reading config from %s"%filename
             for (k,v) in SfaData.defaults.items():
                 print "%-20s: %r"%(k,getattr(SfaData,k))
 
+    def save_config(self):
+        config_keys = SfaData.defaults.keys()
+        configfile = os.path.expanduser("~/.sfi/sfi_config")
+        tmpfile = configfile + ".tmp"
+
+        out = open(tmpfile, "w")
+        for line in open(os.path.expanduser("~/.sfi/sfi_config")):
+            try:
+                key, val = line.split('=')
+                key = key.strip()
+                val = val.strip()
+                if key in config_keys:
+                    line = "%s = '%s'\n" % (key, getattr(self, key))
+            except:
+                pass
+            out.write(line)
+        out.close()
+
+        os.unlink(configfile)
+        os.rename(tmpfile, configfile)
+                    
+
     def getAuthority(self):
         return SfaData.SFI_AUTH
 
@@ -48,28 +98,32 @@ class SfaData:
     def slicemgr(self):
         return SfaData.SFI_SM
 
+    def aggmgr(self):
+        return SfaData.SFI_AM
+
     def trace_command (self, command):
         self._trace=time.time()
-        if self.SFAUI_VERBOSE:
+        if self.SFACE_VERBOSE:
             print time.strftime('%M:%S'),'Invoking',' '.join(command)
+
     def trace_end (self):
-        if self.SFAUI_VERBOSE:
+        if self.SFACE_VERBOSE:
             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:
@@ -79,7 +133,13 @@ class SfaData:
                 
         return children
 
-    def getRSpec(self):
+    def getRSpecFromSM(self):
+        return self.__getRSpec(self.slicemgr())
+
+    def getRSpecFromAM(self):
+        return self.__getRSpec(self.aggmgr())
+
+    def __getRSpec(self, mgr):
         slice = self.getSlice()
         # Write RSpec to file for testing.
         filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
@@ -87,11 +147,11 @@ class SfaData:
             os.remove(filename)
         except:
             pass
-        command=["sfi.py", "-u", self.getUser(), "-a", self.getAuthority(), 
-                 "-r", self.registry(), "-s", self.slicemgr(), "resources", 
+        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()
@@ -104,10 +164,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):
@@ -131,28 +191,28 @@ class PlanetLabData(SfaData):
 class PLEData(PlanetLabData):
     def __init__(self):
         PlanetLabData.__init__(self)
-        self.SFI_SM = "http://www.planet-lab.eu:12346"
+        self.SFI_AM = "http://www.planet-lab.eu:12346"
 
 class PLJData(PlanetLabData):
     def __init__(self):
         PlanetLabData.__init__(self)
-        self.SFI_SM = "http://www.planet-lab.jp:12346"
+        self.SFI_AM = "http://www.planet-lab.jp:12346"
 
 class ViniData(PlanetLabData):
     def __init__(self):
         PlanetLabData.__init__(self)
-        self.SFI_SM = "http://www.vini-veritas.net:12346"
+        self.SFI_AM = "http://www.vini-veritas.net:12346"
 
 class GpENIData(PlanetLabData):
     def __init__(self):
         PlanetLabData.__init__(self)
-        self.SFI_SM = "http://198.248.241.100:12346"
+        self.SFI_AM = "http://198.248.241.100:12346"
 
 class OpenCirrusData(SfaData):
     def __init__(self):
         SfaData.__init__(self)
         self.SFI_REGISTRY = "http://198.55.37.29:12345"
-        self.SFI_SM = "http://198.55.37.29:12346"
+        self.SFI_AM = "http://198.55.37.29:12346"
 
     def refreshRSpec(self):
         xml = SfaData.getRSpec(self)