# 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
# 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
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()
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()
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()
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()
## 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()
# 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()
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')
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')
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()
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()
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()
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
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
"""
- 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
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()
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)
########################
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)
(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):
# 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
# 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
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)
(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):
# 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
# 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,
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)
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:
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:
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). \
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 = ""
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 = ""
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):
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]+" -- "
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")
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)
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)
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")
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,
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()
"""
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):
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():
# 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]
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
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)
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
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)
# 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)
"""
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))
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()
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)
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")
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)
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)
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)
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")
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()
"""
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):
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():
# 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]