X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Futil%2Fenviron.py;h=e67249a6f99fcc5dcc799ef5fd09a34f350e7d58;hb=6285ca51026efb69642eea9dfc7c480e722d84a9;hp=5a435ff697bacc1e0166d2a48e7e0471ebcbe2dd;hpb=4896d77f40a611a22f9f1f8f2ae0e63e9008fee1;p=nepi.git
diff --git a/src/nepi/util/environ.py b/src/nepi/util/environ.py
index 5a435ff6..e67249a6 100644
--- a/src/nepi/util/environ.py
+++ b/src/nepi/util/environ.py
@@ -1,21 +1,23 @@
-"""
- 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.
+#
+# 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 version 2 as
+# published by the Free Software Foundation;
+#
+# 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 .
+#
+# Author: Alina Quereilhac
+# Martin Ferrari
- 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 .
-
-"""
import ctypes
import imp
@@ -60,7 +62,7 @@ def find_bin(name, extra_path = None):
try:
os.stat(d + "/" + name)
return d + "/" + name
- except OSError, e:
+ except OSError as e:
if e.errno != os.errno.ENOENT:
raise
return None
@@ -86,7 +88,7 @@ def find_bin(name, extra_path = None):
try:
os.stat(d + "/" + name)
return d + "/" + name
- except OSError, e:
+ except OSError as e:
if e.errno != os.errno.ENOENT:
raise
return None
@@ -102,11 +104,11 @@ sshd_path = find_bin("sshd")
def execute(cmd):
# FIXME: create a global debug variable
#print "[pid %d]" % os.getpid(), " ".join(cmd)
- null = open("/dev/null", "r+")
- p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
- out, err = p.communicate()
- if p.returncode != 0:
- raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
+ with open("/dev/null", "r+") as null:
+ p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
+ out, err = p.communicate()
+ if p.returncode != 0:
+ raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
def backticks(cmd):
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
@@ -128,9 +130,8 @@ def gen_ssh_keypair(filename):
def add_key_to_agent(filename):
ssh_add = nepi.util.environ.find_bin_or_die("ssh-add")
args = [ssh_add, filename]
- null = file("/dev/null", "w")
- assert subprocess.Popen(args, stderr = null).wait() == 0
- null.close()
+ with open("/dev/null", "w") as null:
+ assert subprocess.Popen(args, stderr = null).wait() == 0
def get_free_port():
s = socket.socket()
@@ -153,10 +154,9 @@ PermitUserEnvironment yes
"""
def gen_sshd_config(filename, port, server_key, auth_keys):
- conf = open(filename, "w")
- text = _SSH_CONF % (port, server_key, auth_keys)
- conf.write(text)
- conf.close()
+ with open(filename, "w") as conf:
+ text = _SSH_CONF % (port, server_key, auth_keys)
+ conf.write(text)
return filename
def gen_auth_keys(pubkey, output, environ):
@@ -165,11 +165,11 @@ def gen_auth_keys(pubkey, output, environ):
for k, v in environ.items():
opts.append('environment="%s=%s"' % (k, v))
- lines = file(pubkey).readlines()
+ with open(pubkey) as f:
+ lines = f.readlines()
pubkey = lines[0].split()[0:2]
- out = file(output, "w")
- out.write("%s %s %s\n" % (",".join(opts), pubkey[0], pubkey[1]))
- out.close()
+ with open(output, "w") as out:
+ out.write("%s %s %s\n" % (",".join(opts), pubkey[0], pubkey[1]))
return output
def start_ssh_agent():
@@ -191,10 +191,9 @@ def stop_ssh_agent(data):
# No need to gather the pid, ssh-agent knows how to kill itself; after we
# had set up the environment
ssh_agent = nepi.util.environ.find_bin_or_die("ssh-agent")
- null = file("/dev/null", "w")
- proc = subprocess.Popen([ssh_agent, "-k"], stdout = null)
- null.close()
- assert proc.wait() == 0
+ with open("/dev/null", "w") as null:
+ proc = subprocess.Popen([ssh_agent, "-k"], stdout = null)
+ assert proc.wait() == 0
for k in data:
del os.environ[k]