From cb5d027b813a27d7de263653e1a8e0cef5490f0a Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Mon, 12 Oct 2015 14:50:26 +0200 Subject: [PATCH] systematic use of context managers for dealing with files instead of open()/close() - and also a few file() --- examples/ccn_emu_live/dce.py | 5 +- examples/ccn_emu_live/planetlab.py | 5 +- examples/linux/ccn_simple_transfer.py | 5 +- examples/linux/ccn_transfer_using_linuxapp.py | 5 +- examples/linux/netcat_file_transfer.py | 10 +- examples/linux/vlc_streaming.py | 5 +- examples/omf/nitos_omf6_vlc.py | 5 +- .../nepi_omf6_plexus_ping_with_traces.py | 10 +- examples/omf/vod_exp/demo_plot.py | 58 ++++---- .../openvswitch/ovs_ping_3switches_loop.py | 59 ++++---- ...TapTunnel_performance_test_triangleTopo.py | 68 +++++---- examples/planetlab/ccn_simple_transfer.py | 5 +- src/nepi/data/processing/ccn/parser.py | 139 +++++++++--------- src/nepi/data/processing/ping/parser.py | 40 +++-- src/nepi/resources/all/collector.py | 5 +- src/nepi/resources/linux/application.py | 3 +- src/nepi/resources/linux/node.py | 3 +- .../resources/linux/scripts/fd-udp-connect.py | 15 +- .../linux/scripts/linux-tap-create.py | 1 + .../linux/scripts/linux-udp-connect.py | 15 +- src/nepi/resources/netns/netnswrapper.py | 1 + .../resources/netns/netnswrapper_debug.py | 5 +- src/nepi/resources/ns3/ns3wrapper_debug.py | 5 +- .../ns3/resource_manager_generator.py | 24 ++- src/nepi/resources/omf/application.py | 6 +- src/nepi/resources/omf/omf6_parser.py | 18 +-- src/nepi/resources/planetlab/node.py | 6 +- .../planetlab/scripts/pl-vif-create.py | 5 +- .../resources/planetlab/scripts/pl-vif-up.py | 6 +- src/nepi/resources/planetlab/sfa_node.py | 6 +- src/nepi/util/environ.py | 37 +++-- src/nepi/util/parallel.py | 5 +- src/nepi/util/serializer.py | 10 +- src/nepi/util/sfaapi.py | 14 +- src/nepi/util/sshfuncs.py | 22 +-- test/resources/linux/netns/netnsclient.py | 9 +- test/resources/linux/node.py | 5 +- .../linux/ns3/cross_dce_linux_ccn.py | 5 +- test/resources/netns/netnswrapper.py | 8 +- test/resources/omf/omf6_vlc_traces.py | 21 ++- test/util/sshfuncs.py | 25 ++-- 41 files changed, 326 insertions(+), 378 deletions(-) diff --git a/examples/ccn_emu_live/dce.py b/examples/ccn_emu_live/dce.py index 79507b5d..4b51b170 100644 --- a/examples/ccn_emu_live/dce.py +++ b/examples/ccn_emu_live/dce.py @@ -168,9 +168,8 @@ def avg_interests(ec, run): # TODO: DUMP RESULTS TO FILE # TODO: DUMP GRAPH DELAYS! - f = open("/tmp/metric", "a+") - f.write("%.2f\n" % metric) - f.close() + with open("/tmp/metric", "a+") as f: + f.write("%.2f\n" % metric) print(" METRIC", metric) return metric diff --git a/examples/ccn_emu_live/planetlab.py b/examples/ccn_emu_live/planetlab.py index b2548f37..b51925d9 100644 --- a/examples/ccn_emu_live/planetlab.py +++ b/examples/ccn_emu_live/planetlab.py @@ -172,9 +172,8 @@ def avg_interests(ec, run): # TODO: DUMP RESULTS TO FILE # TODO: DUMP GRAPH DELAYS! - f = open("/tmp/metric", "a+") - f.write("%.2f\n" % metric) - f.close() + with open("/tmp/metric", "a+") as f: + f.write("%.2f\n" % metric) print(" METRIC", metric) return metric diff --git a/examples/linux/ccn_simple_transfer.py b/examples/linux/ccn_simple_transfer.py index 39ea8f0a..ccf92eb2 100644 --- a/examples/linux/ccn_simple_transfer.py +++ b/examples/linux/ccn_simple_transfer.py @@ -147,9 +147,8 @@ ec.deploy() ec.wait_finished([ccncat]) stdout = ec.trace(ccncat, "stdout") -f = open("video.ts", "w") -f.write(stdout) -f.close() +with open("video.ts", "w") as f: + f.write(stdout) ec.shutdown() diff --git a/examples/linux/ccn_transfer_using_linuxapp.py b/examples/linux/ccn_transfer_using_linuxapp.py index 0395a409..1db375d5 100644 --- a/examples/linux/ccn_transfer_using_linuxapp.py +++ b/examples/linux/ccn_transfer_using_linuxapp.py @@ -207,9 +207,8 @@ apps = [ccncat] ec.wait_finished(apps) stdout = ec.trace(ccncat, "stdout") -f = open("video.ts", "w") -f.write(stdout) -f.close() +with open("video.ts", "w") as f: + f.write(stdout) # Shutdown the experiment controller ec.shutdown() diff --git a/examples/linux/netcat_file_transfer.py b/examples/linux/netcat_file_transfer.py index 3734af31..6d43764a 100644 --- a/examples/linux/netcat_file_transfer.py +++ b/examples/linux/netcat_file_transfer.py @@ -131,12 +131,10 @@ bw = ec.trace(server, "bw.txt") pcap = ec.trace(capture, "file_transfer.pcap") # Choose a directory to store the traces, example f = open("/home//bw.txt", "w") -f = open("bw.txt", "w") -f.write(bw) -f.close() -f = open("video_transfer.pcap", "w") -f.write(pcap) -f.close() +with open("bw.txt", "w") as f: + f.write(bw) +with open("video_transfer.pcap", "w") as f: + f.write(pcap) ec.shutdown() diff --git a/examples/linux/vlc_streaming.py b/examples/linux/vlc_streaming.py index 4b5d83d1..9f31286e 100644 --- a/examples/linux/vlc_streaming.py +++ b/examples/linux/vlc_streaming.py @@ -116,9 +116,8 @@ ec.deploy() ec.wait_finished([server]) video = ec.trace(client, "VIDEO") -f = open("video.ts", "w") -f.write(video) -f.close() +with open("video.ts", "w") as f: + f.write(video) ec.shutdown() diff --git a/examples/omf/nitos_omf6_vlc.py b/examples/omf/nitos_omf6_vlc.py index eb615fe3..c6c2600d 100644 --- a/examples/omf/nitos_omf6_vlc.py +++ b/examples/omf/nitos_omf6_vlc.py @@ -163,9 +163,8 @@ print("BYTES transmitted", byte_count) ## If you redirected the video to standard output, you can try to ## retrieve the stdout of the VLC client ## video = ec.trace(app2, "stdout") -#f = open("video.ts", "w") -#f.write(video) -#f.close() +#with open("video.ts", "w") as f: +# f.write(video) # Stop Experiment ec.shutdown() diff --git a/examples/omf/testing/nepi_omf6_plexus_ping_with_traces.py b/examples/omf/testing/nepi_omf6_plexus_ping_with_traces.py index fd507ccd..01125f3f 100644 --- a/examples/omf/testing/nepi_omf6_plexus_ping_with_traces.py +++ b/examples/omf/testing/nepi_omf6_plexus_ping_with_traces.py @@ -90,13 +90,11 @@ stdout_2 = ec.trace(app2, "stdout") # Choose a directory to store the traces, by default # It it the folder ehere you run Nepi. -f = open("app1.txt", "w") -f.write(stdout_1) -f.close() +with open("app1.txt", "w") as f: + f.write(stdout_1) -g = open("app2.txt", "w") -g.write(stdout_2) -g.close() +with open("app2.txt", "w") as g: + g.write(stdout_2) # Stop Experiment ec.shutdown() diff --git a/examples/omf/vod_exp/demo_plot.py b/examples/omf/vod_exp/demo_plot.py index 5d16c9e1..26ed22ac 100644 --- a/examples/omf/vod_exp/demo_plot.py +++ b/examples/omf/vod_exp/demo_plot.py @@ -95,21 +95,20 @@ def nb_client(s): def get_broad_values(list_files, type_file): for s in list_files: nb = nb_client(s) - o = open(s, 'r') - for l in o: - if 'udp' in l: - row = l.split(':') - f = row[1].split(' ') - frame = int(f[0]) - byte = int(row[2]) - - res = {} - res['frames'] = frame - res['bytes'] = byte - if frame < 20 : - continue - overall_stats_broad[nb][type_file].append(res) - o.close() + with open(s, 'r') as o: + for l in o: + if 'udp' in l: + row = l.split(':') + f = row[1].split(' ') + frame = int(f[0]) + byte = int(row[2]) + + res = {} + res['frames'] = frame + res['bytes'] = byte + if frame < 20 : + continue + overall_stats_broad[nb][type_file].append(res) get_broad_values(stats_broad_wlan, 'wlan') get_broad_values(stats_broad_eth, 'eth') @@ -146,21 +145,20 @@ for exp in data_vod_folders : def get_vod_values(list_files, type_file): for s in list_files: nb = nb_client(s) - o = open(s, 'r') - for l in o: - if 'udp' in l: - row = l.split(':') - f = row[1].split(' ') - frame = int(f[0]) - byte = int(row[2]) - - res = {} - res['frames'] = frame - res['bytes'] = byte - if frame < 100 : - continue - overall_stats_vod[nb][type_file].append(res) - o.close() + with open(s, 'r') as o: + for l in o: + if 'udp' in l: + row = l.split(':') + f = row[1].split(' ') + frame = int(f[0]) + byte = int(row[2]) + + res = {} + res['frames'] = frame + res['bytes'] = byte + if frame < 100 : + continue + overall_stats_vod[nb][type_file].append(res) get_vod_values(stats_vod_wlan, 'wlan') get_vod_values(stats_vod_eth, 'eth') diff --git a/examples/openvswitch/ovs_ping_3switches_loop.py b/examples/openvswitch/ovs_ping_3switches_loop.py index 30967868..bd246dad 100644 --- a/examples/openvswitch/ovs_ping_3switches_loop.py +++ b/examples/openvswitch/ovs_ping_3switches_loop.py @@ -184,36 +184,35 @@ ping11 = ec.trace(app11, 'stdout') ping12 = ec.trace(app12, 'stdout') -f = open("examples/openvswitch/ovs_ping_3switches_loop.txt", 'w') - -if not ping12: - ec.shutdown() - -f.write("************ Ping From Switch 1 : 192.168.3.2 ********************\n\n") -f.write(ping1) -f.write("--------------------------------------\n") -f.write(ping2) -f.write("************ Ping From Switch 2 : 192.168.3.4 ********************\n\n") -f.write(ping3) -f.write("--------------------------------------\n") -f.write(ping4) -f.write("************ Ping From Switch 3 : 192.168.3.6 ********************\n\n") -f.write(ping5) -f.write("--------------------------------------\n") -f.write(ping6) -f.write("************ Ping From Host 1 : 192.168.3.1 ********************\n\n") -f.write(ping7) -f.write("--------------------------------------\n") -f.write(ping8) -f.write("************ Ping From Host 2 : 192.168.3.3 ********************\n\n") -f.write(ping9) -f.write("--------------------------------------\n") -f.write(ping10) -f.write("************ Ping From Host 3 : 192.168.3.5 ********************\n\n") -f.write(ping11) -f.write("--------------------------------------\n") -f.write(ping12) -f.close() +with open("examples/openvswitch/ovs_ping_3switches_loop.txt", 'w') as f: + + if not ping12: + ec.shutdown() + + f.write("************ Ping From Switch 1 : 192.168.3.2 ********************\n\n") + f.write(ping1) + f.write("--------------------------------------\n") + f.write(ping2) + f.write("************ Ping From Switch 2 : 192.168.3.4 ********************\n\n") + f.write(ping3) + f.write("--------------------------------------\n") + f.write(ping4) + f.write("************ Ping From Switch 3 : 192.168.3.6 ********************\n\n") + f.write(ping5) + f.write("--------------------------------------\n") + f.write(ping6) + f.write("************ Ping From Host 1 : 192.168.3.1 ********************\n\n") + f.write(ping7) + f.write("--------------------------------------\n") + f.write(ping8) + f.write("************ Ping From Host 2 : 192.168.3.3 ********************\n\n") + f.write(ping9) + f.write("--------------------------------------\n") + f.write(ping10) + f.write("************ Ping From Host 3 : 192.168.3.5 ********************\n\n") + f.write(ping11) + f.write("--------------------------------------\n") + f.write(ping12) # Delete the overlay network ec.shutdown() diff --git a/examples/openvswitch/ping_over_udpTapTunnel_performance_test_triangleTopo.py b/examples/openvswitch/ping_over_udpTapTunnel_performance_test_triangleTopo.py index 75575c1a..51e4a037 100644 --- a/examples/openvswitch/ping_over_udpTapTunnel_performance_test_triangleTopo.py +++ b/examples/openvswitch/ping_over_udpTapTunnel_performance_test_triangleTopo.py @@ -180,44 +180,42 @@ ping23 = ec.trace(app23, 'stdout') ping24 = ec.trace(app24, 'stdout') ping25 = ec.trace(app25, 'stdout') -f = open("examples/openvswitch/ping_over_udpTapTunnel_performance_test.txt", 'w') +with open("examples/openvswitch/ping_over_udpTapTunnel_performance_test.txt", 'w') as f: -if not ping25: - ec.shutdown() + if not ping25: + ec.shutdown() -f.write("************ Ping From Host 1 : 192.168.3.1 ********************\n\n") -f.write(ping1) -f.write("----------------------------------------\n\n") -f.write(ping2) -f.write("----------------------------------------\n\n") -f.write(ping3) -f.write("----------------------------------------\n\n") -f.write(ping4) -f.write("----------------------------------------\n\n") -f.write(ping5) -f.write("************ Ping From Host 2 : 192.168.3.13 ********************\n\n") -f.write(ping11) -f.write("----------------------------------------\n\n") -f.write(ping12) -f.write("----------------------------------------\n\n") -f.write(ping13) -f.write("----------------------------------------\n\n") -f.write(ping14) -f.write("----------------------------------------\n\n") -f.write(ping15) -f.write("************ Ping From Host 3 : 192.168.3.25 ********************\n\n") -f.write(ping21) -f.write("----------------------------------------\n\n") -f.write(ping22) -f.write("----------------------------------------\n\n") -f.write(ping23) -f.write("----------------------------------------\n\n") -f.write(ping24) -f.write("----------------------------------------\n\n") -f.write(ping25) - -f.close() + f.write("************ Ping From Host 1 : 192.168.3.1 ********************\n\n") + f.write(ping1) + f.write("----------------------------------------\n\n") + f.write(ping2) + f.write("----------------------------------------\n\n") + f.write(ping3) + f.write("----------------------------------------\n\n") + f.write(ping4) + f.write("----------------------------------------\n\n") + f.write(ping5) + f.write("************ Ping From Host 2 : 192.168.3.13 ********************\n\n") + f.write(ping11) + f.write("----------------------------------------\n\n") + f.write(ping12) + f.write("----------------------------------------\n\n") + f.write(ping13) + f.write("----------------------------------------\n\n") + f.write(ping14) + f.write("----------------------------------------\n\n") + f.write(ping15) + f.write("************ Ping From Host 3 : 192.168.3.25 ********************\n\n") + f.write(ping21) + f.write("----------------------------------------\n\n") + f.write(ping22) + f.write("----------------------------------------\n\n") + f.write(ping23) + f.write("----------------------------------------\n\n") + f.write(ping24) + f.write("----------------------------------------\n\n") + f.write(ping25) # Delete the overlay network ec.shutdown() diff --git a/examples/planetlab/ccn_simple_transfer.py b/examples/planetlab/ccn_simple_transfer.py index 64c78883..fabbcbc5 100644 --- a/examples/planetlab/ccn_simple_transfer.py +++ b/examples/planetlab/ccn_simple_transfer.py @@ -160,9 +160,8 @@ ec.deploy() ec.wait_finished([ccncat]) stdout = ec.trace(ccncat, "stdout") -f = open("video.ts", "w") -f.write(stdout) -f.close() +with open("video.ts", "w") as f: + f.write(stdout) ec.shutdown() diff --git a/src/nepi/data/processing/ccn/parser.py b/src/nepi/data/processing/ccn/parser.py index 7d35d9e3..676a2058 100644 --- a/src/nepi/data/processing/ccn/parser.py +++ b/src/nepi/data/processing/ccn/parser.py @@ -69,82 +69,80 @@ def parse_file(filename): faces = dict() sep = " " - f = open(filename, "r") - - data = [] - - for line in f: - cols = line.strip().split(sep) - - # CCN_PEEK - # MESSAGE interest_from - # 1374181938.808523 ccnd[9245]: debug.4352 interest_from 6 ccnx:/test/bunny.ts (23 bytes,sim=0CDCC1D7) - # - # MESSAGE interest_to - # 1374181938.812750 ccnd[9245]: debug.3502 interest_to 5 ccnx:/test/bunny.ts (39 bytes,i=2844,sim=0CDCC1D7) - # - # MESSAGE CONTENT FROM - # 1374181938.868682 ccnd[9245]: debug.4643 content_from 5 ccnx:/test/bunny.ts/%FD%05%1E%85%8FVw/%00/%9E%3D%01%D9%3Cn%95%2BvZ%8 - # - # MESSAGE CONTENT_TO - # 1374181938.868772 ccnd[9245]: debug.1619 content_to 6 ccnx:/test/bunny.ts/%FD%05%1E%85%8FVw/%00/%9E%3D%01%D9%3Cn%95%2BvZ%8 - # - # 1375596708.222304 ccnd[9758]: debug.3692 interest_expiry ccnx:/test/bunny.ts/%FD%05%1E%86%B1GS/%00%0A%F7 (44 bytes,c=0:1,i=2819,sim=49FA8048) - - # External face creation - # 1374181452.965961 ccnd[9245]: accepted datagram client id=5 (flags=0x40012) 204.85.191.10 port 9695 - - if line.find("accepted datagram client") > -1: - face_id = (cols[5]).replace("id=",'') - ip = cols[7] - port = cols[9] - faces[face_id] = (ip, port) - continue - - # 1374181452.985296 ccnd[9245]: releasing face id 4 (slot 4) - if line.find("releasing face id") > -1: - face_id = cols[5] - if face_id in faces: - del faces[face_id] - continue - - if len(cols) < 6: - continue - - timestamp = cols[0] - message_type = cols[3] + with open(filename, "r") as f: + + data = [] + + for line in f: + cols = line.strip().split(sep) + + # CCN_PEEK + # MESSAGE interest_from + # 1374181938.808523 ccnd[9245]: debug.4352 interest_from 6 ccnx:/test/bunny.ts (23 bytes,sim=0CDCC1D7) + # + # MESSAGE interest_to + # 1374181938.812750 ccnd[9245]: debug.3502 interest_to 5 ccnx:/test/bunny.ts (39 bytes,i=2844,sim=0CDCC1D7) + # + # MESSAGE CONTENT FROM + # 1374181938.868682 ccnd[9245]: debug.4643 content_from 5 ccnx:/test/bunny.ts/%FD%05%1E%85%8FVw/%00/%9E%3D%01%D9%3Cn%95%2BvZ%8 + # + # MESSAGE CONTENT_TO + # 1374181938.868772 ccnd[9245]: debug.1619 content_to 6 ccnx:/test/bunny.ts/%FD%05%1E%85%8FVw/%00/%9E%3D%01%D9%3Cn%95%2BvZ%8 + # + # 1375596708.222304 ccnd[9758]: debug.3692 interest_expiry ccnx:/test/bunny.ts/%FD%05%1E%86%B1GS/%00%0A%F7 (44 bytes,c=0:1,i=2819,sim=49FA8048) + + # External face creation + # 1374181452.965961 ccnd[9245]: accepted datagram client id=5 (flags=0x40012) 204.85.191.10 port 9695 + + if line.find("accepted datagram client") > -1: + face_id = (cols[5]).replace("id=",'') + ip = cols[7] + port = cols[9] + faces[face_id] = (ip, port) + continue - if message_type not in ["interest_from", "interest_to", "content_from", - "content_to", "interest_dupnonce", "interest_expiry"]: - continue + # 1374181452.985296 ccnd[9245]: releasing face id 4 (slot 4) + if line.find("releasing face id") > -1: + face_id = cols[5] + if face_id in faces: + del faces[face_id] + continue - face_id = cols[4] - content_name = cols[5] + if len(cols) < 6: + continue - # Interest Nonce ? -> 412A74-0844-0008-50AA-F6EAD4 - nonce = "" - if message_type in ["interest_from", "interest_to", "interest_dupnonce"]: - last = cols[-1] - if len(last.split("-")) == 5: - nonce = last + timestamp = cols[0] + message_type = cols[3] - try: - size = int((cols[6]).replace('(','')) - except: - print("interest_expiry without face id!", line) - continue + if message_type not in ["interest_from", "interest_to", "content_from", + "content_to", "interest_dupnonce", "interest_expiry"]: + continue - # If no external IP address was identified for this face - # asume it is a local face - peer = "localhost" + face_id = cols[4] + content_name = cols[5] - if face_id in faces: - peer, port = faces[face_id] + # Interest Nonce ? -> 412A74-0844-0008-50AA-F6EAD4 + nonce = "" + if message_type in ["interest_from", "interest_to", "interest_dupnonce"]: + last = cols[-1] + if len(last.split("-")) == 5: + nonce = last - data.append((content_name, timestamp, message_type, peer, face_id, - size, nonce, line)) + try: + size = int((cols[6]).replace('(','')) + except: + print("interest_expiry without face id!", line) + continue - f.close() + # If no external IP address was identified for this face + # asume it is a local face + peer = "localhost" + + if face_id in faces: + peer, port = faces[face_id] + + data.append((content_name, timestamp, message_type, peer, face_id, + size, nonce, line)) return data @@ -155,9 +153,8 @@ def dump_content_history(content_history): return f.name def load_content_history(fname): - f = open(fname, "r") - content_history = pickle.load(f) - f.close() + with open(fname, "r") as f: + content_history = pickle.load(f) os.remove(fname) return content_history diff --git a/src/nepi/data/processing/ping/parser.py b/src/nepi/data/processing/ping/parser.py index b8132e52..b51f37f2 100644 --- a/src/nepi/data/processing/ping/parser.py +++ b/src/nepi/data/processing/ping/parser.py @@ -39,29 +39,27 @@ def parse_file(filename): """ - f = open(filename, "r") + with open(filename, "r") as f: - # Traceroute info - target_ip = None - target_hostname = None + # Traceroute info + target_ip = None + target_hostname = None - data = [] - - for line in f: - # match traceroute to ... - m = re.match(_rre, line) - if not m: - continue - - target_ip = m.groupdict()["ip"] - # FIX THIS: Make sure the regular expression does not inlcude - # the ')' in the ip group - target_ip = target_ip.replace(")","") - target_hostname = m.groupdict()["hostname"] - time = m.groupdict()["time"] - data.append((target_ip, target_hostname, time)) - - f.close() + data = [] + + for line in f: + # match traceroute to ... + m = re.match(_rre, line) + if not m: + continue + + target_ip = m.groupdict()["ip"] + # FIX THIS: Make sure the regular expression does not inlcude + # the ')' in the ip group + target_ip = target_ip.replace(")","") + target_hostname = m.groupdict()["hostname"] + time = m.groupdict()["time"] + data.append((target_ip, target_hostname, time)) return data diff --git a/src/nepi/resources/all/collector.py b/src/nepi/resources/all/collector.py index ebcd335a..3573a037 100644 --- a/src/nepi/resources/all/collector.py +++ b/src/nepi/resources/all/collector.py @@ -117,9 +117,8 @@ class Collector(ResourceManager): try: result = self.ec.trace(rm.guid, trace_name) - f = open(fpath, "w") - f.write(result) - f.close() + with open(fpath, "w") as f: + f.write(result) except: import traceback err = traceback.format_exc() diff --git a/src/nepi/resources/linux/application.py b/src/nepi/resources/linux/application.py index e73c867f..7f7fbcac 100644 --- a/src/nepi/resources/linux/application.py +++ b/src/nepi/resources/linux/application.py @@ -294,7 +294,8 @@ class LinuxApplication(ResourceManager): for line in out.strip().split("\n"): parts = line.strip().split(" ") procs[parts[0]] = parts[1] - pickle.dump(procs, open("/tmp/save.proc", "wb")) + with open("/tmp/save.proc", "wb") as pickle_file: + pickle.dump(procs, pickle_file) # create run dir for application self.node.mkdir(self.run_home) diff --git a/src/nepi/resources/linux/node.py b/src/nepi/resources/linux/node.py index 16b9b003..df3030b4 100644 --- a/src/nepi/resources/linux/node.py +++ b/src/nepi/resources/linux/node.py @@ -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) diff --git a/src/nepi/resources/linux/scripts/fd-udp-connect.py b/src/nepi/resources/linux/scripts/fd-udp-connect.py index 83bd845b..7096cf1a 100644 --- a/src/nepi/resources/linux/scripts/fd-udp-connect.py +++ b/src/nepi/resources/linux/scripts/fd-udp-connect.py @@ -114,9 +114,8 @@ if __name__ == '__main__': (local_host, local_port) = rsock.getsockname() # Save local port information to file - f = open(local_port_file, 'w') - f.write("%d\n" % local_port) - f.close() + with open(local_port_file, 'w') as f: + f.write("%d\n" % local_port) # Wait until remote port information is available while not os.path.exists(remote_port_file): @@ -130,9 +129,8 @@ if __name__ == '__main__': # the read operation returns empty string! # Maybe a race condition? for i in xrange(10): - f = open(remote_port_file, 'r') - remote_port = f.read() - f.close() + with open(remote_port_file, 'r') as f: + remote_port = f.read() if remote_port: break @@ -157,9 +155,8 @@ if __name__ == '__main__': # TODO: Test connectivity! # Create a ret_file to indicate success - f = open(ret_file, 'w') - f.write("0") - f.close() + with open(ret_file, 'w') as f: + f.write("0") STARTED = True diff --git a/src/nepi/resources/linux/scripts/linux-tap-create.py b/src/nepi/resources/linux/scripts/linux-tap-create.py index bd14cee9..5792e7de 100644 --- a/src/nepi/resources/linux/scripts/linux-tap-create.py +++ b/src/nepi/resources/linux/scripts/linux-tap-create.py @@ -140,6 +140,7 @@ def create_tap(vif_name, vif_type, pi): if not pi: flags |= IFF_NO_PI + # xxx : Thierry : not quite sure where this gets closed fd = os.open("/dev/net/tun", os.O_RDWR) ifreq = struct.pack("16sH", vif_name, flags) diff --git a/src/nepi/resources/linux/scripts/linux-udp-connect.py b/src/nepi/resources/linux/scripts/linux-udp-connect.py index a4b662e9..f8f8502c 100644 --- a/src/nepi/resources/linux/scripts/linux-udp-connect.py +++ b/src/nepi/resources/linux/scripts/linux-udp-connect.py @@ -139,9 +139,8 @@ if __name__ == '__main__': (local_host, local_port) = sock.getsockname() # Save local port information to file - f = open(local_port_file, 'w') - f.write("%d\n" % local_port) - f.close() + with open(local_port_file, 'w') as f: + f.write("%d\n" % local_port) # Wait until remote port information is available while not os.path.exists(remote_port_file): @@ -155,9 +154,8 @@ if __name__ == '__main__': # the read operation returns empty string! # Maybe a race condition? for i in xrange(10): - f = open(remote_port_file, 'r') - remote_port = f.read() - f.close() + with open(remote_port_file, 'r') as f: + remote_port = f.read() if remote_port: break @@ -174,9 +172,8 @@ if __name__ == '__main__': # TODO: Test connectivity! # Create a ret_file to indicate success - f = open(ret_file, 'w') - f.write("0") - f.close() + with open(ret_file, 'w') as f: + f.write("0") # Establish tunnel tunchannel.tun_fwd(tun, remote, diff --git a/src/nepi/resources/netns/netnswrapper.py b/src/nepi/resources/netns/netnswrapper.py index 6c0968b5..4456f976 100644 --- a/src/nepi/resources/netns/netnswrapper.py +++ b/src/nepi/resources/netns/netnswrapper.py @@ -69,6 +69,7 @@ class NetNSWrapper(object): if clazzname == "open": path = args[0] mode = args[1] + # xxx Thierry: not sure where this gets closed obj = open(path, mode) else: clazz = getattr(netns, clazzname) diff --git a/src/nepi/resources/netns/netnswrapper_debug.py b/src/nepi/resources/netns/netnswrapper_debug.py index 17ae1f26..462b7308 100644 --- a/src/nepi/resources/netns/netnswrapper_debug.py +++ b/src/nepi/resources/netns/netnswrapper_debug.py @@ -46,9 +46,8 @@ class NetNSWrapperDebuger(object): return self._script_path def dump_to_script(self, command): - f = open(self.script_path, "a") - f.write("%s" % command) - f.close() + with open(self.script_path, "a") as f: + f.write("%s" % command) def dump_header(self): if not self.enabled: diff --git a/src/nepi/resources/ns3/ns3wrapper_debug.py b/src/nepi/resources/ns3/ns3wrapper_debug.py index 7368448d..332f63bf 100644 --- a/src/nepi/resources/ns3/ns3wrapper_debug.py +++ b/src/nepi/resources/ns3/ns3wrapper_debug.py @@ -48,9 +48,8 @@ class NS3WrapperDebuger(object): return self._script_path def dump_to_script(self, command): - f = open(self.script_path, "a") - f.write("%s" % command) - f.close() + with open(self.script_path, "a") as f: + f.write("%s" % command) def dump_header(self): if not self.enabled: diff --git a/src/nepi/resources/ns3/resource_manager_generator.py b/src/nepi/resources/ns3/resource_manager_generator.py index 90979065..de737757 100644 --- a/src/nepi/resources/ns3/resource_manager_generator.py +++ b/src/nepi/resources/ns3/resource_manager_generator.py @@ -127,9 +127,8 @@ def create_ns3_rms(): short_rtype = uncamm_rtype.replace("::","-") d = os.path.dirname(os.path.realpath(__file__)) - ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r") - template = ftemp.read() - ftemp.close() + with open(os.path.join(d, "templates", "resource_manager_template.txt"), "r") as ftemp: + template = ftemp.read() template = template. \ replace("", classname). \ @@ -144,17 +143,15 @@ def create_ns3_rms(): replace('::', ''). \ replace("-","_").lower() + ".py" - f = open(os.path.join(d, "classes", fname), "w") - print(os.path.join(d, fname)) - print(template) - f.write(template) - f.close() + with open(os.path.join(d, "classes", fname), "w") as f: + print(os.path.join(d, fname)) + print(template) + f.write(template) def template_attributes(ns3, tid): d = os.path.dirname(os.path.realpath(__file__)) - ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r") - template = ftemp.read() - ftemp.close() + with open(os.path.join(d, "templates", "attribute_template.txt"), "r") as ftemp: + template = ftemp.read() attributes = "" @@ -217,9 +214,8 @@ def template_attributes(ns3, tid): def template_traces(ns3, tid): d = os.path.dirname(os.path.realpath(__file__)) - ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r") - template = ftemp.read() - ftemp.close() + with open(os.path.join(d, "templates", "trace_template.txt"), "r") as ftemp: + template = ftemp.read() traces = "" diff --git a/src/nepi/resources/omf/application.py b/src/nepi/resources/omf/application.py index 8384024d..8afb7c16 100644 --- a/src/nepi/resources/omf/application.py +++ b/src/nepi/resources/omf/application.py @@ -286,13 +286,11 @@ class OMFApplication(OMFResource): if attr == TraceAttr.ALL: try: - f = open(trace_path ,'r') + with open(trace_path ,'r') as f: + return f.read() except IOError: print("File with traces has not been found") return False - out = f.read() - f.close() - return out def do_start(self): diff --git a/src/nepi/resources/omf/omf6_parser.py b/src/nepi/resources/omf/omf6_parser.py index 1220e004..b542e8cc 100644 --- a/src/nepi/resources/omf/omf6_parser.py +++ b/src/nepi/resources/omf/omf6_parser.py @@ -185,18 +185,16 @@ class OMF6Parser(Logger): elif tag == "msg": if event == "STDOUT" : filename = os.path.join("/tmp", "%s.out" % uid) - f = open(filename,'a+') - # XXX: Adding fake \n for visual formatting - msg = props[elt] # + "\n" - f.write(msg) - f.close() + with open(filename,'a+') as f: + # XXX: Adding fake \n for visual formatting + msg = props[elt] # + "\n" + f.write(msg) elif event == "STDERR" : filename = os.path.join("/tmp", "%s.err" % uid) - f = open(filename,'a+') - # XXX: Adding fake \n for visual formatting - msg = props[elt] # + "\n" - f.write(msg) - f.close() + with open(filename,'a+') as f: + # XXX: Adding fake \n for visual formatting + msg = props[elt] # + "\n" + f.write(msg) log = log + tag +" : " + props[elt]+" -- " else: log = log + tag +" : " + props[elt]+" -- " diff --git a/src/nepi/resources/planetlab/node.py b/src/nepi/resources/planetlab/node.py index fe7f0581..9ef4a199 100644 --- a/src/nepi/resources/planetlab/node.py +++ b/src/nepi/resources/planetlab/node.py @@ -210,10 +210,12 @@ class PlanetlabNode(LinuxNode): plblacklist_file = os.path.join(nepi_home, "plblacklist.txt") if not os.path.exists(plblacklist_file): if os.path.isdir(nepi_home): - open(plblacklist_file, 'w').close() + with open(plblacklist_file, 'w') as clear: + pass else: os.makedirs(nepi_home) - open(plblacklist_file, 'w').close() + with open(plblacklist_file, 'w') as clear: + pass def _skip_provision(self): pl_user = self.get("pluser") diff --git a/src/nepi/resources/planetlab/scripts/pl-vif-create.py b/src/nepi/resources/planetlab/scripts/pl-vif-create.py index 1c19e3ee..df9fbdf7 100644 --- a/src/nepi/resources/planetlab/scripts/pl-vif-create.py +++ b/src/nepi/resources/planetlab/scripts/pl-vif-create.py @@ -146,9 +146,8 @@ if __name__ == '__main__': pointopoint = pointopoint, txqueuelen = txqueuelen) # Saving interface name to vif_name_file - f = open(vif_name_file, 'w') - f.write(vif_name) - f.close() + with open(vif_name_file, 'w') as f: + f.write(vif_name) # create unix socket to receive instructions sock = create_socket(socket_name) diff --git a/src/nepi/resources/planetlab/scripts/pl-vif-up.py b/src/nepi/resources/planetlab/scripts/pl-vif-up.py index c00181cf..8e9342a8 100644 --- a/src/nepi/resources/planetlab/scripts/pl-vif-up.py +++ b/src/nepi/resources/planetlab/scripts/pl-vif-up.py @@ -125,8 +125,6 @@ if __name__ == '__main__': sys.exit(1) # Saving interface name to vif_name_file - f = open(vif_name_file, 'w') - f.write(vif_name) - f.close() - + with open(vif_name_file, 'w') as f: + f.write(vif_name) diff --git a/src/nepi/resources/planetlab/sfa_node.py b/src/nepi/resources/planetlab/sfa_node.py index 81783cdd..d122cb48 100644 --- a/src/nepi/resources/planetlab/sfa_node.py +++ b/src/nepi/resources/planetlab/sfa_node.py @@ -191,10 +191,12 @@ class PlanetlabSfaNode(LinuxNode): plblacklist_file = os.path.join(nepi_home, "plblacklist.txt") if not os.path.exists(plblacklist_file): if os.path.isdir(nepi_home): - open(plblacklist_file, 'w').close() + with open(plblacklist_file, 'w') as clear: + pass else: os.makedirs(nepi_home) - open(plblacklist_file, 'w').close() + with open(plblacklist_file, 'w') as clear: + pass def _skip_provision(self): sfa_user = self.get("sfauser") 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] diff --git a/src/nepi/util/parallel.py b/src/nepi/util/parallel.py index 3b6e281b..bf86b574 100644 --- a/src/nepi/util/parallel.py +++ b/src/nepi/util/parallel.py @@ -84,11 +84,8 @@ class ParallelRun(object): if maxthreads is None: if N_PROCS is None: try: - f = open("/proc/cpuinfo") - try: + with open("/proc/cpuinfo") as f: N_PROCS = sum("processor" in l for l in f) - finally: - f.close() except: pass maxthreads = N_PROCS diff --git a/src/nepi/util/serializer.py b/src/nepi/util/serializer.py index ad7a4c31..0e36a92e 100644 --- a/src/nepi/util/serializer.py +++ b/src/nepi/util/serializer.py @@ -28,9 +28,8 @@ class ECSerializer(object): from nepi.util.parsers.xml_parser import ECXMLParser parser = ECXMLParser() - f = open(filepath, "r") - xml = f.read() - f.close() + with open(filepath, "r") as f: + xml = f.read() ec = parser.from_xml(xml) @@ -52,9 +51,8 @@ class ECSerializer(object): if format == SFormats.XML: filepath = os.path.join(dirpath, "%s.xml" % filename) sec = self.serialize(ec, format = format) - f = open(filepath, "w") - f.write(sec) - f.close() + with open(filepath, "w") as f: + f.write(sec) return filepath diff --git a/src/nepi/util/sfaapi.py b/src/nepi/util/sfaapi.py index 3dcc7e68..136ae70b 100644 --- a/src/nepi/util/sfaapi.py +++ b/src/nepi/util/sfaapi.py @@ -219,10 +219,9 @@ class SFAAPI(object): resources_urn = self._get_resources_urn(resources_hrn_new) rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, None, leases) - f = open("/tmp/rspec_input.rspec", "w") - f.truncate(0) - f.write(rspec) - f.close() + with open("/tmp/rspec_input.rspec", "w") as f: + f.truncate(0) + f.write(rspec) if not os.path.getsize("/tmp/rspec_input.rspec") > 0: raise RuntimeError("Fail to create rspec file to allocate resource in slice %s" % slicename) @@ -269,10 +268,9 @@ class SFAAPI(object): # Re implementing urn from hrn because the library sfa-common doesn't work for wilabt resources_urn = self._get_urn(resources_hrn_new) rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, properties, leases) - f = open("/tmp/rspec_input.rspec", "w") - f.truncate(0) - f.write(rspec) - f.close() + with open("/tmp/rspec_input.rspec", "w") as f: + f.truncate(0) + f.write(rspec) if not os.path.getsize("/tmp/rspec_input.rspec") > 0: raise RuntimeError("Fail to create rspec file to allocate resources in slice %s" % slicename) diff --git a/src/nepi/util/sshfuncs.py b/src/nepi/util/sshfuncs.py index 12c21388..cfb9908d 100644 --- a/src/nepi/util/sshfuncs.py +++ b/src/nepi/util/sshfuncs.py @@ -120,14 +120,15 @@ def openssh_has_persist(): """ global OPENSSH_HAS_PERSIST if OPENSSH_HAS_PERSIST is None: - proc = subprocess.Popen( - ["ssh", "-v"], - stdout = subprocess.PIPE, - stderr = subprocess.STDOUT, - stdin = open("/dev/null"), - ) - out,err = proc.communicate() - proc.wait() + with open("/dev/null") as null: + proc = subprocess.Popen( + ["ssh", "-v"], + stdout = subprocess.PIPE, + stderr = subprocess.STDOUT, + stdin = null, + ) + out,err = proc.communicate() + proc.wait() vre = re.compile(r'OpenSSH_(?:[6-9]|5[.][8-9]|5[.][1-9][0-9]|[1-9][0-9]).*', re.I) OPENSSH_HAS_PERSIST = bool(vre.match(out)) @@ -165,9 +166,8 @@ def make_server_key_args(server_key, host, port): if os.environ.get('NEPI_STRICT_AUTH_MODE',"").lower() not in ('1','true','on'): user_hosts_path = '%s/.ssh/known_hosts' % (os.environ.get('HOME',""),) if os.access(user_hosts_path, os.R_OK): - f = open(user_hosts_path, "r") - tmp_known_hosts.write(f.read()) - f.close() + with open(user_hosts_path, "r") as f: + tmp_known_hosts.write(f.read()) tmp_known_hosts.flush() diff --git a/test/resources/linux/netns/netnsclient.py b/test/resources/linux/netns/netnsclient.py index 13823c9b..48e6bf6d 100755 --- a/test/resources/linux/netns/netnsclient.py +++ b/test/resources/linux/netns/netnsclient.py @@ -153,11 +153,10 @@ class LinuxNetNSClientTest(unittest.TestCase): p1 = client.invoke(a1, "poll") p2 = client.invoke(a2, "poll") - stdout1 = open(path1, "r") - stdout2 = open(path2, "r") - - s1 = stdout1.read() - s2 = stdout2.read() + with open(path1, "r") as stdout1: + with open(path2, "r") as stdout2: + s1 = stdout1.read() + s2 = stdout2.read() print(s1, s2) diff --git a/test/resources/linux/node.py b/test/resources/linux/node.py index 11b66bb5..10f306a1 100755 --- a/test/resources/linux/node.py +++ b/test/resources/linux/node.py @@ -279,9 +279,8 @@ main (void) node.remove_packages("gcc", app_home) node.rmdir(app_home) - f = open(dst, "r") - out = f.read() - f.close() + with open(dst, "r") as f: + out = f.read() self.assertEquals(out, "Hello, world!\n") diff --git a/test/resources/linux/ns3/cross_dce_linux_ccn.py b/test/resources/linux/ns3/cross_dce_linux_ccn.py index 7dbee17e..109d44d3 100755 --- a/test/resources/linux/ns3/cross_dce_linux_ccn.py +++ b/test/resources/linux/ns3/cross_dce_linux_ccn.py @@ -243,9 +243,8 @@ class LinuxNS3FdNetDeviceTest(unittest.TestCase): time.sleep(60) stdout = ec.trace(ccncat, "stdout") - f = open("bunny.ts", "w") - f.write(stdout) - f.close() + with open("bunny.ts", "w") as f: + f.write(stdout) #expected = "DATA" #self.assertTrue(stdout.find(expected) > -1) diff --git a/test/resources/netns/netnswrapper.py b/test/resources/netns/netnswrapper.py index 8e33b0c1..0fe179f1 100755 --- a/test/resources/netns/netnswrapper.py +++ b/test/resources/netns/netnswrapper.py @@ -122,11 +122,11 @@ class NetNSWrapperTest(unittest.TestCase): p1 = wrapper.invoke(a1, "poll") p2 = wrapper.invoke(a2, "poll") - stdout1 = open(path1, "r") - stdout2 = open(path2, "r") + with open(path1, "r") as stdout1: + with open(path2, "r") as stdout2: - s1 = stdout1.read() - s2 = stdout2.read() + s1 = stdout1.read() + s2 = stdout2.read() expected = "1 packets transmitted, 1 received, 0% packet loss" self.assertTrue(s1.find(expected) > -1) diff --git a/test/resources/omf/omf6_vlc_traces.py b/test/resources/omf/omf6_vlc_traces.py index 2c34599f..c9ab328a 100755 --- a/test/resources/omf/omf6_vlc_traces.py +++ b/test/resources/omf/omf6_vlc_traces.py @@ -76,14 +76,12 @@ class OMFPingNormalCase(unittest.TestCase): stderr_1 = ec.trace(self.app1, "stderr") if stdout_1: - f = open("app1_out.txt", "w") - f.write(stdout_1) - f.close() + with open("app1_out.txt", "w") as f: + f.write(stdout_1) if stderr_1: - f = open("app1_err.txt", "w") - f.write(stderr_1) - f.close() + with open("app1_err.txt", "w") as f: + f.write(stderr_1) self.assertEquals(ec.get_resource(self.node1).state, ResourceState.STARTED) self.assertEquals(ec.get_resource(self.iface1).state, ResourceState.STARTED) @@ -97,13 +95,12 @@ class OMFPingNormalCase(unittest.TestCase): self.assertEquals(ec.get_resource(self.channel).state, ResourceState.RELEASED) self.assertEquals(ec.get_resource(self.app1).state, ResourceState.RELEASED) - t = open("app1_out.txt", "r") - l = t.readlines() - self.assertEquals(l[0], "PING 10.0.0.17 (10.0.0.17) 56(84) bytes of data.\n") - self.assertIn("5 packets transmitted, 5 received, 0% packet loss, time", l[-2]) - self.assertIn("rtt min/avg/max/mdev = ", l[-1]) + with open("app1_out.txt", "r") as t: + l = t.readlines() + self.assertEquals(l[0], "PING 10.0.0.17 (10.0.0.17) 56(84) bytes of data.\n") + self.assertIn("5 packets transmitted, 5 received, 0% packet loss, time", l[-2]) + self.assertIn("rtt min/avg/max/mdev = ", l[-1]) - t.close() os.remove("app1_out.txt") diff --git a/test/util/sshfuncs.py b/test/util/sshfuncs.py index 99f44b0e..539af78c 100755 --- a/test/util/sshfuncs.py +++ b/test/util/sshfuncs.py @@ -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] -- 2.43.0