cherry picked cb5d027b about context managers
[nepi.git] / src / nepi / resources / linux / node.py
index 16b9b00..b754eba 100644 (file)
@@ -297,7 +297,7 @@ class LinuxNode(ResourceManager):
         if not self.localhost and not self.get("username"):
             msg = "Can't resolve OS, insufficient data "
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         out = self.get_os()
 
@@ -355,7 +355,7 @@ class LinuxNode(ResourceManager):
             trace = traceback.format_exc()
             msg = "Deploy failed. Unresponsive node {} -- traceback {}".format(self.get("hostname"), trace)
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         self.find_home()
 
@@ -477,7 +477,8 @@ class LinuxNode(ResourceManager):
                 ########################
 
                 import pickle
-                pids = pickle.load(open("/tmp/save.proc", "rb"))
+                with open("/tmp/save.proc", "rb") as pickle_file:
+                    pids = pickle.load(pickle_file)
                 pids_temp = dict()
                 ps_aux = "ps aux | awk '{print $2,$11}'"
                 (out, err), proc = self.execute(ps_aux)
@@ -489,7 +490,8 @@ class LinuxNode(ResourceManager):
                     # adding the avoided pids filtered above (avoid_kill) to allow users keep process
                     # alive when using besides ssh connections  
                     kill_pids = set(pids_temp.items()) - set(pids.items())
-                    kill_pids = ' '.join(dict(kill_pids).keys())
+                    # py2/py3 : keep it simple
+                    kill_pids = ' '.join(kill_pids)
 
                     # removing pids from beside connections and its process
                     kill_pids = kill_pids.split(' ')
@@ -765,15 +767,17 @@ class LinuxNode(ResourceManager):
         if text and not os.path.isfile(src):
             # src is text input that should be uploaded as file
             # create a temporal file with the content to upload
-            f = tempfile.NamedTemporaryFile(delete=False)
+            # in python3 we need to open in binary mode if str is bytes
+            mode = 'w' if isinstance(src, str) else 'wb'
+            f = tempfile.NamedTemporaryFile(mode=mode, delete=False)
             f.write(src)
             f.close()
             src = f.name
 
         # If dst files should not be overwritten, check that the files do not
-        # exits already
+        # exist already
         if isinstance(src, str):
-            src = map(str.strip, src.split(";"))
+            src = [s.strip() for s in src.split(";")]
     
         if overwrite == False:
             src = self.filter_existing_files(src, dst)
@@ -796,7 +800,7 @@ class LinuxNode(ResourceManager):
             
             msg = "{} out: {} err: {}".format(msg, out, err)
             if raise_on_error:
-                raise RuntimeError, msg
+                raise RuntimeError(msg)
 
         return ((out, err), proc)
 
@@ -812,7 +816,7 @@ class LinuxNode(ResourceManager):
             self.error(msg, out, err)
 
             if raise_on_error:
-                raise RuntimeError, msg
+                raise RuntimeError(msg)
 
         return ((out, err), proc)
 
@@ -825,7 +829,7 @@ class LinuxNode(ResourceManager):
         else:
             msg = "Error installing packages ( OS not known ) "
             self.error(msg, self.os)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         return command
 
@@ -866,7 +870,7 @@ class LinuxNode(ResourceManager):
         else:
             msg = "Error removing packages ( OS not known ) "
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
         run_home = run_home or home
 
@@ -903,7 +907,7 @@ class LinuxNode(ResourceManager):
         if isinstance(paths, str):
             paths = [paths]
 
-        cmd = " ; ".join(map(lambda path: "rm -rf {}".format(path), paths))
+        cmd = " ; ".join(["rm -rf {}".format(path) for path in paths])
 
         return self.execute(cmd, with_lock = True)
     
@@ -950,7 +954,7 @@ class LinuxNode(ResourceManager):
             msg = " Failed to run command '{}' ".format(command)
             self.error(msg, out, err)
             if raise_on_error:
-                raise RuntimeError, msg
+                raise RuntimeError(msg)
 
         # Wait for pid file to be generated
         pid, ppid = self.wait_pid(
@@ -972,7 +976,7 @@ class LinuxNode(ResourceManager):
                 self.error(msg, eout, err)
 
                 if raise_on_error:
-                    raise RuntimeError, msg
+                    raise RuntimeError(msg)
 
         (out, oerr), proc = self.check_output(home, stdout)
         
@@ -1073,7 +1077,7 @@ class LinuxNode(ResourceManager):
         pid = ppid = None
         delay = 1.0
 
-        for i in xrange(2):
+        for i in range(2):
             pidtuple = self.getpid(home = home, pidfile = pidfile)
             
             if pidtuple:
@@ -1087,7 +1091,7 @@ class LinuxNode(ResourceManager):
             self.error(msg)
     
             if raise_on_error:
-                raise RuntimeError, msg
+                raise RuntimeError(msg)
 
         return pid, ppid
 
@@ -1166,7 +1170,7 @@ class LinuxNode(ResourceManager):
 
         if not self._home_dir:
             self.error(msg)
-            raise RuntimeError, msg
+            raise RuntimeError(msg)
 
     def filter_existing_files(self, src, dst):
         """ Removes files that already exist in the Linux host from src list
@@ -1176,19 +1180,20 @@ class LinuxNode(ResourceManager):
                 if len(src) > 1 else {dst: src[0]}
 
         command = []
-        for d in dests.keys():
+        for d in dests:
             command.append(" [ -f {dst} ] && echo '{dst}' ".format(dst=d) )
 
         command = ";".join(command)
 
         (out, err), proc = self.execute(command, retry = 1, with_lock = True)
         
-        for d in dests.keys():
+        for d in dests:
             if out.find(d) > -1:
                 del dests[d]
 
         if not dests:
             return []
 
-        return dests.values()
+        # list(..) here added by 2to3 - leaving for safety
+        return list(dests.values())