X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=test%2Futil%2Fsshfuncs.py;h=2c9c30b656633de57980907153d4e042326c2c99;hb=6285ca51026efb69642eea9dfc7c480e722d84a9;hp=99f44b0eb18ed2a11781df1428cd826c7d7ba0f8;hpb=e55924b6886bd7382a28e1ae235c4810f852e163;p=nepi.git diff --git a/test/util/sshfuncs.py b/test/util/sshfuncs.py index 99f44b0e..2c9c30b6 100755 --- a/test/util/sshfuncs.py +++ b/test/util/sshfuncs.py @@ -47,7 +47,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 @@ -68,9 +68,8 @@ def gen_ssh_keypair(filename): def add_key_to_agent(filename): ssh_add = 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() @@ -93,10 +92,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): @@ -105,11 +103,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(): @@ -131,9 +129,8 @@ 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 = find_bin_or_die("ssh-agent") - null = file("/dev/null", "w") - proc = subprocess.Popen([ssh_agent, "-k"], stdout = null) - null.close() + 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]