X-Git-Url: http://git.onelab.eu/?p=nepi.git;a=blobdiff_plain;f=src%2Fnepi%2Futil%2Fenviron.py;h=32ed19b4c929b7b637a2c965f832e05fc3e19679;hp=59495076faf04e650d1537bec8061533d3d6afd2;hb=cb5d027b813a27d7de263653e1a8e0cef5490f0a;hpb=da6134f8a027890f2ee477a4658163d7d79127f1 diff --git a/src/nepi/util/environ.py b/src/nepi/util/environ.py index 59495076..32ed19b4 100644 --- a/src/nepi/util/environ.py +++ b/src/nepi/util/environ.py @@ -104,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, @@ -130,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() @@ -155,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): @@ -167,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(): @@ -193,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]