Trying to make LinuxNS3Simulator to deploy remotely ....
[nepi.git] / src / nepi / util / execfuncs.py
index 7e2d506..98b9d73 100644 (file)
@@ -1,11 +1,29 @@
-from nepi.util.sshfuncs import RUNNING, FINISHED, NOT_STARTED, STDOUT 
+#
+#    NEPI, a framework to manage network experiments
+#    Copyright (C) 2013 INRIA
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
+
+from nepi.util.sshfuncs import ProcStatus, STDOUT
 
 import subprocess
 
 def lexec(command, 
         user = None, 
         sudo = False,
-        stdin = None,
         env = None):
     """
     Executes a local command, returns ((stdout,stderr),process)
@@ -21,12 +39,12 @@ def lexec(command,
     elif user:
         command = "su %s ; %s " % (user, command)
 
-    p = subprocess.Popen(command, 
+
+    proc = subprocess.Popen(command, shell=True, 
             stdout = subprocess.PIPE, 
-            stderr = subprocess.PIPE,
-            stdin  = stdin)
+            stderr = subprocess.PIPE)
 
-    out, err = p.communicate()
+    out, err = proc.communicate()
     return ((out, err), proc)
 
 def lcopy(source, dest, recursive = False):
@@ -44,7 +62,7 @@ def lcopy(source, dest, recursive = False):
     command.append(src)
     command.append(dst)
     
-    p = subprocess.Popen(command, 
+    proc = subprocess.Popen(command, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE)
 
@@ -108,14 +126,14 @@ def lspawn(command, pidfile,
             'create' : 'mkdir -p %s ; ' % (shell_escape(home),) if create_home else '',
         }
 
-    (out,err),proc = lexec(cmd)
+    (out,err), proc = lexec(cmd)
     
     if proc.wait():
         raise RuntimeError, "Failed to set up application on host %s: %s %s" % (host, out,err,)
 
-    return (out,err),proc
+    return ((out,err), proc)
 
-def lcheckpid(pidfile):
+def lgetpid(pidfile):
     """
     Check the pidfile of a process spawned with remote_spawn.
     
@@ -128,7 +146,7 @@ def lcheckpid(pidfile):
         or None if the pidfile isn't valid yet (maybe the process is still starting).
     """
 
-    (out,err),proc = lexec("cat %s" % pidfile )
+    (out,err), proc = lexec("cat %s" % pidfile )
         
     if proc.wait():
         return None
@@ -152,7 +170,7 @@ def lstatus(pid, ppid):
         One of NOT_STARTED, RUNNING, FINISHED
     """
 
-    (out,err),proc = lexec(
+    (out,err), proc = lexec(
         # Check only by pid. pid+ppid does not always work (especially with sudo) 
         " (( ps --pid %(pid)d -o pid | grep -c %(pid)d && echo 'wait')  || echo 'done' ) | tail -n 1" % {
             'ppid' : ppid,
@@ -160,15 +178,15 @@ def lstatus(pid, ppid):
         })
     
     if proc.wait():
-        return NOT_STARTED
+        return ProcStatus.NOT_STARTED
     
     status = False
     if out:
         status = (out.strip() == 'wait')
     else:
-        return NOT_STARTED
-    return RUNNING if status else FINISHED
+        return ProcStatus.NOT_STARTED
+
+    return ProcStatus.RUNNING if status else ProcStatus.FINISHED
 
 def lkill(pid, ppid, sudo = False):
     """