Fix #128 - [NS3] Test ns-3 with localhost
[nepi.git] / src / nepi / util / execfuncs.py
index cd11bfa..c998038 100644 (file)
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
-from nepi.util.sshfuncs import ProcStatus, STDOUT
+from nepi.util.sshfuncs import ProcStatus, STDOUT, log, shell_escape
 
+import logging
+import shlex
 import subprocess
 
 def lexec(command, 
         user = None, 
         sudo = False,
-        stdin = None,
         env = None):
     """
     Executes a local command, returns ((stdout,stderr),process)
@@ -40,13 +41,21 @@ def lexec(command,
     elif user:
         command = "su %s ; %s " % (user, command)
 
+    proc = subprocess.Popen(command,
+                shell = True, 
+                stdout = subprocess.PIPE, 
+                stderr = subprocess.PIPE)
 
-    proc = subprocess.Popen(command, shell=True, 
-            stdout = subprocess.PIPE, 
-            stderr = subprocess.PIPE)
-            #stdin  = stdin)
+    out = err = ""
+    log_msg = "lexec - command %s " % command
+
+    try:
+        out, err = proc.communicate()
+        log(log_msg, logging.DEBUG, out, err)
+    except:
+        log(log_msg, logging.ERROR, out, err)
+        raise
 
-    out, err = proc.communicate()
     return ((out, err), proc)
 
 def lcopy(source, dest, recursive = False):
@@ -54,21 +63,35 @@ def lcopy(source, dest, recursive = False):
     Copies from/to localy.
     """
     
-    if TRACE:
-        print "scp", source, dest
-    
-    command = ["cp"]
+    args = ["cp"]
     if recursive:
-        command.append("-R")
-    
-    command.append(src)
-    command.append(dst)
-    
-    proc = subprocess.Popen(command, 
+        args.append("-r")
+  
+    if isinstance(source, list):
+        args.extend(source)
+    else:
+        args.append(source)
+
+    if isinstance(dest, list):
+        args.extend(dest)
+    else:
+        args.append(dest)
+
+    proc = subprocess.Popen(args, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE)
 
-    out, err = p.communicate()
+    out = err = ""
+    command = " ".join(args)
+    log_msg = " lcopy - command %s " % command
+
+    try:
+        out, err = proc.communicate()
+        log(log_msg, logging.DEBUG, out, err)
+    except:
+        log(log_msg, logging.ERROR, out, err)
+        raise
+
     return ((out, err), proc)
    
 def lspawn(command, pidfile, 
@@ -120,7 +143,7 @@ def lspawn(command, pidfile,
         'stdin' : stdin,
     }
     
-    cmd = "%(create)s%(gohome)s rm -f %(pidfile)s ; %(sudo)s nohup bash -c %(command)s " % {
+    cmd = "%(create)s%(gohome)s rm -f %(pidfile)s ; %(sudo)s bash -c %(command)s " % {
             'command' : shell_escape(daemon_command),
             'sudo' : 'sudo -S' if sudo else '',
             'pidfile' : shell_escape(pidfile),