systematic use of context managers for dealing with files instead of open()/close...
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Mon, 12 Oct 2015 12:50:26 +0000 (14:50 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Mon, 12 Oct 2015 12:50:26 +0000 (14:50 +0200)
41 files changed:
examples/ccn_emu_live/dce.py
examples/ccn_emu_live/planetlab.py
examples/linux/ccn_simple_transfer.py
examples/linux/ccn_transfer_using_linuxapp.py
examples/linux/netcat_file_transfer.py
examples/linux/vlc_streaming.py
examples/omf/nitos_omf6_vlc.py
examples/omf/testing/nepi_omf6_plexus_ping_with_traces.py
examples/omf/vod_exp/demo_plot.py
examples/openvswitch/ovs_ping_3switches_loop.py
examples/openvswitch/ping_over_udpTapTunnel_performance_test_triangleTopo.py
examples/planetlab/ccn_simple_transfer.py
src/nepi/data/processing/ccn/parser.py
src/nepi/data/processing/ping/parser.py
src/nepi/resources/all/collector.py
src/nepi/resources/linux/application.py
src/nepi/resources/linux/node.py
src/nepi/resources/linux/scripts/fd-udp-connect.py
src/nepi/resources/linux/scripts/linux-tap-create.py
src/nepi/resources/linux/scripts/linux-udp-connect.py
src/nepi/resources/netns/netnswrapper.py
src/nepi/resources/netns/netnswrapper_debug.py
src/nepi/resources/ns3/ns3wrapper_debug.py
src/nepi/resources/ns3/resource_manager_generator.py
src/nepi/resources/omf/application.py
src/nepi/resources/omf/omf6_parser.py
src/nepi/resources/planetlab/node.py
src/nepi/resources/planetlab/scripts/pl-vif-create.py
src/nepi/resources/planetlab/scripts/pl-vif-up.py
src/nepi/resources/planetlab/sfa_node.py
src/nepi/util/environ.py
src/nepi/util/parallel.py
src/nepi/util/serializer.py
src/nepi/util/sfaapi.py
src/nepi/util/sshfuncs.py
test/resources/linux/netns/netnsclient.py
test/resources/linux/node.py
test/resources/linux/ns3/cross_dce_linux_ccn.py
test/resources/netns/netnswrapper.py
test/resources/omf/omf6_vlc_traces.py
test/util/sshfuncs.py

index 79507b5..4b51b17 100644 (file)
@@ -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
index b2548f3..b51925d 100644 (file)
@@ -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
index 39ea8f0..ccf92eb 100644 (file)
@@ -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()
 
index 0395a40..1db375d 100644 (file)
@@ -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()
index 3734af3..6d43764 100644 (file)
@@ -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/<user>/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()
 
index 4b5d83d..9f31286 100644 (file)
@@ -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()
 
index eb615fe..c6c2600 100644 (file)
@@ -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()
index fd507cc..01125f3 100644 (file)
@@ -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()
index 5d16c9e..26ed22a 100644 (file)
@@ -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')
index 3096786..bd246da 100644 (file)
@@ -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()
index 75575c1..51e4a03 100644 (file)
@@ -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()
index 64c7888..fabbcbc 100644 (file)
@@ -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()
 
index 7d35d9e..676a205 100644 (file)
@@ -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
index b8132e5..b51f37f 100644 (file)
@@ -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
 
index ebcd335..3573a03 100644 (file)
@@ -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()
index e73c867..7f7fbca 100644 (file)
@@ -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)
index 16b9b00..df3030b 100644 (file)
@@ -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)
index 83bd845..7096cf1 100644 (file)
@@ -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
 
index bd14cee..5792e7d 100644 (file)
@@ -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)
index a4b662e..f8f8502 100644 (file)
@@ -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,
index 6c0968b..4456f97 100644 (file)
@@ -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)
index 17ae1f2..462b730 100644 (file)
@@ -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:
index 7368448..332f63b 100644 (file)
@@ -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:
index 9097906..de73775 100644 (file)
@@ -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("<CLASS_NAME>", 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 = ""
 
index 8384024..8afb7c1 100644 (file)
@@ -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):
index 1220e00..b542e8c 100644 (file)
@@ -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]+" -- "
index fe7f058..9ef4a19 100644 (file)
@@ -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")
index 1c19e3e..df9fbdf 100644 (file)
@@ -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)
index c00181c..8e9342a 100644 (file)
@@ -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)
 
index 81783cd..d122cb4 100644 (file)
@@ -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")
index 5949507..32ed19b 100644 (file)
@@ -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]
 
index 3b6e281..bf86b57 100644 (file)
@@ -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
index ad7a4c3..0e36a92 100644 (file)
@@ -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
 
index 3dcc7e6..136ae70 100644 (file)
@@ -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)
index 12c2138..cfb9908 100644 (file)
@@ -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()
     
index 13823c9..48e6bf6 100755 (executable)
@@ -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)
 
index 11b66bb..10f306a 100755 (executable)
@@ -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")
 
index 7dbee17..109d44d 100755 (executable)
@@ -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)
index 8e33b0c..0fe179f 100755 (executable)
@@ -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)
index 2c34599..c9ab328 100755 (executable)
@@ -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")
         
 
index 99f44b0..539af78 100755 (executable)
@@ -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]