From feebd8704f3dbcd3a41b7ffa41d032aea1e54787 Mon Sep 17 00:00:00 2001 From: bharnden Date: Sun, 5 May 2019 16:52:55 -0700 Subject: [PATCH] updated all print calls to use print() method for 2/3 --- daemon/examples/api/emane80211.py | 4 +- daemon/examples/api/switch.py | 8 +-- daemon/examples/api/wlan.py | 8 +-- daemon/examples/netns/daemonnodes.py | 16 +++-- daemon/examples/netns/distributed.py | 12 ++-- daemon/examples/netns/howmanynodes.py | 40 ++++++------- daemon/examples/netns/ospfmanetmdrtest.py | 10 ++-- daemon/examples/netns/wlanemanetests.py | 10 ++-- daemon/examples/stopsession.py | 2 +- daemon/tests/test_gui.py | 2 +- scripts/perf/perflogserver.py | 72 +++++++++++------------ 11 files changed, 89 insertions(+), 95 deletions(-) diff --git a/daemon/examples/api/emane80211.py b/daemon/examples/api/emane80211.py index 390d84c9..1804d50d 100644 --- a/daemon/examples/api/emane80211.py +++ b/daemon/examples/api/emane80211.py @@ -55,9 +55,9 @@ def example(options): def main(): options = parser.parse_options("emane80211") start = datetime.datetime.now() - print "running emane 80211 example: nodes(%s) time(%s)" % (options.nodes, options.time) + print("running emane 80211 example: nodes(%s) time(%s)" % (options.nodes, options.time)) example(options) - print "elapsed time: %s" % (datetime.datetime.now() - start) + print("elapsed time: %s" % (datetime.datetime.now() - start)) if __name__ == "__main__" or __name__ == "__builtin__": diff --git a/daemon/examples/api/switch.py b/daemon/examples/api/switch.py index 4e83cbe9..f8419a47 100644 --- a/daemon/examples/api/switch.py +++ b/daemon/examples/api/switch.py @@ -44,10 +44,10 @@ def example(options): first_node = session.get_node(2) last_node = session.get_node(options.nodes + 1) - print "starting iperf server on node: %s" % first_node.name + print("starting iperf server on node: %s" % first_node.name) first_node.cmd(["iperf", "-s", "-D"]) first_node_address = prefixes.ip4_address(first_node) - print "node %s connecting to %s" % (last_node.name, first_node_address) + print("node %s connecting to %s" % (last_node.name, first_node_address)) last_node.client.icmd(["iperf", "-t", str(options.time), "-c", first_node_address]) first_node.cmd(["killall", "-9", "iperf"]) @@ -59,9 +59,9 @@ def main(): options = parser.parse_options("switch") start = datetime.datetime.now() - print "running switch example: nodes(%s) time(%s)" % (options.nodes, options.time) + print("running switch example: nodes(%s) time(%s)" % (options.nodes, options.time)) example(options) - print "elapsed time: %s" % (datetime.datetime.now() - start) + print("elapsed time: %s" % (datetime.datetime.now() - start)) if __name__ == "__main__": diff --git a/daemon/examples/api/wlan.py b/daemon/examples/api/wlan.py index 8536cbf5..9ac516b7 100644 --- a/daemon/examples/api/wlan.py +++ b/daemon/examples/api/wlan.py @@ -48,10 +48,10 @@ def example(options): first_node = session.get_node(2) last_node = session.get_node(options.nodes + 1) - print "starting iperf server on node: %s" % first_node.name + print("starting iperf server on node: %s" % first_node.name) first_node.cmd(["iperf", "-s", "-D"]) address = prefixes.ip4_address(first_node) - print "node %s connecting to %s" % (last_node.name, address) + print("node %s connecting to %s" % (last_node.name, address)) last_node.client.icmd(["iperf", "-t", str(options.time), "-c", address]) first_node.cmd(["killall", "-9", "iperf"]) @@ -63,9 +63,9 @@ def main(): options = parser.parse_options("wlan") start = datetime.datetime.now() - print "running wlan example: nodes(%s) time(%s)" % (options.nodes, options.time) + print("running wlan example: nodes(%s) time(%s)" % (options.nodes, options.time)) example(options) - print "elapsed time: %s" % (datetime.datetime.now() - start) + print("elapsed time: %s" % (datetime.datetime.now() - start)) if __name__ == "__main__": diff --git a/daemon/examples/netns/daemonnodes.py b/daemon/examples/netns/daemonnodes.py index 3dec17dc..87b9564f 100755 --- a/daemon/examples/netns/daemonnodes.py +++ b/daemon/examples/netns/daemonnodes.py @@ -65,7 +65,7 @@ def cmd(node, exec_cmd): msgdata = server.sock.recv(msglen) # If we get the right response return the results - print "received response message: %s" % MessageTypes(msgtype) + print("received response message: %s" % MessageTypes(msgtype)) if msgtype == MessageTypes.EXECUTE.value: msg = coreapi.CoreExecMessage(msgflags, msghdr, msgdata) result = msg.get_tlv(ExecuteTlvs.RESULT.value) @@ -120,7 +120,7 @@ def main(): port = int(daemonport[1]) else: port = CORE_API_PORT - print "connecting to daemon at %s:%d" % (daemon, port) + print("connecting to daemon at %s:%d" % (daemon, port)) session.broker.addserver(daemon, daemonip, port) # Set the local session id to match the port. @@ -145,7 +145,7 @@ def main(): number_of_nodes = options.numnodes - print "creating %d remote nodes with addresses from %s" % (options.numnodes, prefix) + print("creating %d remote nodes with addresses from %s" % (options.numnodes, prefix)) # create remote nodes via API for i in range(1, number_of_nodes + 1): @@ -175,13 +175,11 @@ def main(): session.broker.handlerawmsg(msg) # Get the ip or last node and ping it from the first - print "Pinging from the first to the last node" + print("Pinging from the first to the last node") pingip = cmd(n[-1], "ip -4 -o addr show dev eth0").split()[3].split("/")[0] - print cmd(n[1], "ping -c 5 " + pingip) - - print "elapsed time: %s" % (datetime.datetime.now() - start) - - print "To stop this session, use the core-cleanup script on the remote daemon server." + print(cmd(n[1], "ping -c 5 " + pingip)) + print("elapsed time: %s" % (datetime.datetime.now() - start)) + print("To stop this session, use the core-cleanup script on the remote daemon server.") raw_input("press enter to exit") diff --git a/daemon/examples/netns/distributed.py b/daemon/examples/netns/distributed.py index b4fe7b22..ca6aec1a 100755 --- a/daemon/examples/netns/distributed.py +++ b/daemon/examples/netns/distributed.py @@ -68,7 +68,7 @@ def main(): port = int(slaveport[1]) else: port = CORE_API_PORT - print "connecting to slave at %s:%d" % (slave, port) + print("connecting to slave at %s:%d" % (slave, port)) session.broker.addserver(slave, slave, port) session.broker.setupserver(slave) session.set_state(EventTypes.CONFIGURATION_STATE) @@ -79,8 +79,8 @@ def main(): switch.setposition(x=80, y=50) num_local = options.numnodes / 2 num_remote = options.numnodes / 2 + options.numnodes % 2 - print "creating %d (%d local / %d remote) nodes with addresses from %s" % \ - (options.numnodes, num_local, num_remote, prefix) + print("creating %d (%d local / %d remote) nodes with addresses from %s" % \ + (options.numnodes, num_local, num_remote, prefix)) for i in range(1, num_local + 1): node = session.create_node(cls=core.nodes.base.CoreNode, name="n%d" % i, _id=i) node.newnetif(switch, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)]) @@ -120,9 +120,9 @@ def main(): # start a shell on node 1 n[1].client.term("bash") - print "elapsed time: %s" % (datetime.datetime.now() - start) - print "To stop this session, use the 'core-cleanup' script on this server" - print "and on the remote slave server." + print("elapsed time: %s" % (datetime.datetime.now() - start)) + print("To stop this session, use the 'core-cleanup' script on this server") + print("and on the remote slave server.") if __name__ == "__main__" or __name__ == "__builtin__": diff --git a/daemon/examples/netns/howmanynodes.py b/daemon/examples/netns/howmanynodes.py index bf709a57..e5989a3c 100755 --- a/daemon/examples/netns/howmanynodes.py +++ b/daemon/examples/netns/howmanynodes.py @@ -115,16 +115,16 @@ def main(): start = datetime.datetime.now() prefix = ipaddress.Ipv4Prefix("10.83.0.0/16") - print "Testing how many network namespace nodes this machine can create." - print " - %s" % linuxversion() + print("Testing how many network namespace nodes this machine can create.") + print(" - %s" % linuxversion()) mem = memfree() - print " - %.02f GB total memory (%.02f GB swap)" % (mem["total"] / GBD, mem["stotal"] / GBD) - print " - using IPv4 network prefix %s" % prefix - print " - using wait time of %s" % options.waittime - print " - using %d nodes per bridge" % options.bridges - print " - will retry %d times on failure" % options.retries - print " - adding these services to each node: %s" % options.services - print " " + print(" - %.02f GB total memory (%.02f GB swap)" % (mem["total"] / GBD, mem["stotal"] / GBD)) + print(" - using IPv4 network prefix %s" % prefix) + print(" - using wait time of %s" % options.waittime) + print(" - using %d nodes per bridge" % options.bridges) + print(" - will retry %d times on failure" % options.retries) + print(" - adding these services to each node: %s" % options.services) + print(" ") lfp = None if options.logfile is not None: @@ -138,7 +138,7 @@ def main(): session = Session(1) switch = session.create_node(cls=core.nodes.network.SwitchNode) switchlist.append(switch) - print "Added bridge %s (%d)." % (switch.brname, len(switchlist)) + print("Added bridge %s (%d)." % (switch.brname, len(switchlist))) i = 0 retry_count = options.retries @@ -149,9 +149,9 @@ def main(): if 0 < options.bridges <= switch.numnetif(): switch = session.create_node(cls=core.nodes.network.SwitchNode) switchlist.append(switch) - print "\nAdded bridge %s (%d) for node %d." % (switch.brname, len(switchlist), i) + print("\nAdded bridge %s (%d) for node %d." % (switch.brname, len(switchlist), i)) except Exception, e: - print "At %d bridges (%d nodes) caught exception:\n%s\n" % (len(switchlist), i - 1, e) + print("At %d bridges (%d nodes) caught exception:\n%s\n" % (len(switchlist), i - 1, e)) break # create a node @@ -164,11 +164,11 @@ def main(): session.services.boot_services(n) nodelist.append(n) if i % 25 == 0: - print "\n%s nodes created " % i, + print("\n%s nodes created " % i,) mem = memfree() free = mem["free"] + mem["buff"] + mem["cached"] swap = mem["stotal"] - mem["sfree"] - print "(%.02f/%.02f GB free/swap)" % (free / GBD, swap / GBD), + print("(%.02f/%.02f GB free/swap)" % (free / GBD, swap / GBD),) if lfp: lfp.write("%d," % i) lfp.write("%s\n" % ",".join(str(mem[x]) for x in MEMKEYS)) @@ -178,20 +178,20 @@ def main(): sys.stdout.flush() time.sleep(options.waittime) except Exception, e: - print "At %d nodes caught exception:\n" % i, e + print("At %d nodes caught exception:\n" % i, e) if retry_count > 0: - print "\nWill retry creating node %d." % i + print("\nWill retry creating node %d." % i) shutil.rmtree(n.nodedir, ignore_errors=True) retry_count -= 1 i -= 1 time.sleep(options.waittime) continue else: - print "Stopping at %d nodes!" % i + print("Stopping at %d nodes!" % i) break if i == options.numnodes: - print "Stopping at %d nodes due to numnodes option." % i + print("Stopping at %d nodes due to numnodes option." % i) break # node creation was successful at this point retry_count = options.retries @@ -200,8 +200,8 @@ def main(): lfp.flush() lfp.close() - print "elapsed time: %s" % (datetime.datetime.now() - start) - print "Use the core-cleanup script to remove nodes and bridges." + print("elapsed time: %s" % (datetime.datetime.now() - start)) + print("Use the core-cleanup script to remove nodes and bridges.") if __name__ == "__main__": diff --git a/daemon/examples/netns/ospfmanetmdrtest.py b/daemon/examples/netns/ospfmanetmdrtest.py index aa31f1b4..89f5f9e3 100755 --- a/daemon/examples/netns/ospfmanetmdrtest.py +++ b/daemon/examples/netns/ospfmanetmdrtest.py @@ -171,13 +171,13 @@ class ManetExperiment(object): def info(self, msg): ''' Utility method for writing output to stdout. ''' - print msg + print(msg) sys.stdout.flush() self.log(msg) def warn(self, msg): ''' Utility method for writing output to stderr. ''' - print >> sys.stderr, msg + sys.stderr.write(msg) sys.stderr.flush() self.log(msg) @@ -204,7 +204,7 @@ class ManetExperiment(object): """ Write to the log file, if any. """ if not self.logfp: return - print >> self.logfp, msg + self.logfp.write(msg) def logdata(self, nbrs, mdrs, lsdbs, krs, zrs): """ Dump experiment parameters and data to the log file. """ @@ -368,12 +368,12 @@ class Cmd: def info(self, msg): ''' Utility method for writing output to stdout.''' - print msg + print(msg) sys.stdout.flush() def warn(self, msg): ''' Utility method for writing output to stderr. ''' - print >> sys.stderr, "XXX %s:" % self.node.routerid, msg + sys.stderr.write("XXX %s:" % self.node.routerid, msg) sys.stderr.flush() def run(self): diff --git a/daemon/examples/netns/wlanemanetests.py b/daemon/examples/netns/wlanemanetests.py index a0786ec4..452f1049 100755 --- a/daemon/examples/netns/wlanemanetests.py +++ b/daemon/examples/netns/wlanemanetests.py @@ -114,12 +114,12 @@ class Cmd(object): def info(self, msg): """ Utility method for writing output to stdout.""" - print msg + print(msg) sys.stdout.flush() def warn(self, msg): """ Utility method for writing output to stderr. """ - print >> sys.stderr, "XXX %s:" % self.node.name, msg + sys.stderr.write("XXX %s:" % self.node.name, msg) sys.stderr.flush() def run(self): @@ -358,13 +358,13 @@ class Experiment(object): def info(self, msg): """ Utility method for writing output to stdout. """ - print msg + print(msg) sys.stdout.flush() self.log(msg) def warn(self, msg): """ Utility method for writing output to stderr. """ - print >> sys.stderr, msg + sys.stderr.write(msg) sys.stderr.flush() self.log(msg) @@ -394,7 +394,7 @@ class Experiment(object): """ Write to the log file, if any. """ if not self.logfp: return - print >> self.logfp, msg + self.logfp.write(msg) def reset(self): """ Prepare for another experiment run. diff --git a/daemon/examples/stopsession.py b/daemon/examples/stopsession.py index d6b2349d..3deaae53 100755 --- a/daemon/examples/stopsession.py +++ b/daemon/examples/stopsession.py @@ -40,7 +40,7 @@ def main(): data = sock.recv(msglen) message = coreapi.CoreMessage(msgflags, hdr, data) sessions = message.get_tlv(coreapi.SessionTlvs.NUMBER.value) - print "sessions:", sessions + print("sessions: {}".format(sessions)) sock.close() diff --git a/daemon/tests/test_gui.py b/daemon/tests/test_gui.py index a06d61a9..3083ea2d 100644 --- a/daemon/tests/test_gui.py +++ b/daemon/tests/test_gui.py @@ -90,7 +90,7 @@ def run_cmd(node, exec_cmd): message_data = server.sock.recv(message_length) # If we get the right response return the results - print "received response message: %s" % message_type + print("received response message: %s" % message_type) if message_type == MessageTypes.EXECUTE.value: message = coreapi.CoreExecMessage(message_flags, message_header, message_data) result = message.get_tlv(ExecuteTlvs.RESULT.value) diff --git a/scripts/perf/perflogserver.py b/scripts/perf/perflogserver.py index a4c2c469..cc682703 100755 --- a/scripts/perf/perflogserver.py +++ b/scripts/perf/perflogserver.py @@ -23,8 +23,8 @@ def readfile(fname): f = open(fname, "r") except IOError: if options.timestamp == True: - print str(time.time()), - print "ERROR: failed to open file %s\n" % fname + print(str(time.time()),) + print("ERROR: failed to open file %s\n" % fname) else: lines = f.readlines() f.close() @@ -42,8 +42,7 @@ def numcpus(): def handler(signum, frame): - print "stop timestamp:", str( - time.time()) + ", cyclecount=", cyclecount, ", caught signal", signum + print("stop timestamp:", str(time.time()) + ", cyclecount=", cyclecount, ", caught signal", signum) sys.exit(0) @@ -120,15 +119,15 @@ def checkserverthreshold(metricval): alarm = ["server", os.uname()[1], str(ind) + key, "%.2f" % pcpus[ind], ">", serverthresholds.getvalue(key)] if options.timestamp: - print str(time.time()) + ",", - print ", ".join(str(x) for x in alarm) + print(str(time.time()) + ",",) + print(", ".join(str(x) for x in alarm)) else: if metricval.getvalue(key) > serverthresholds.getvalue(key): alarm = ["server", os.uname()[1], key, "%.2f" % metricval.getvalue(key), ">", serverthresholds.getvalue(key)] if options.timestamp: - print str(time.time()) + ",", - print ", ".join(str(x) for x in alarm) + print(str(time.time()) + ",",) + print(", ".join(str(x) for x in alarm)) def collectservercputimes(): @@ -310,8 +309,8 @@ class LogSession(object): for nname in self.pids: if self.pids[nname] == "": if options.timestamp == True: - print str(time.time()), - print "ERROR: null vnoded pid of node: %s" % nname + print(str(time.time()),) + print("ERROR: null vnoded pid of node: %s" % nname) else: childprocs = [] ppid = self.pids[nname] @@ -331,13 +330,13 @@ class LogSession(object): if self.pids[pp] != []: for ap in range(len(self.pids[pp]) - 1): # ap pid - print ", " + self.pids[pp][ap][0], + print(", " + self.pids[pp][ap][0],) # ap cmd - print ", " + self.pids[pp][ap][1], + print(", " + self.pids[pp][ap][1],) procmetrics = [str(x) for x in self.pids[pp][ap][-1]] - print ", " + ", ".join(procmetrics), + print(", " + ", ".join(procmetrics),) nodemetrics = [str(x) for x in self.pids[pp][-1]] - print ", " + ", ".join(nodemetrics) + print(", " + ", ".join(nodemetrics)) def getprocessmetrics(self, pid): """ @@ -401,9 +400,9 @@ class LogSession(object): procm = self.getprocessmetrics(self.pids[nod][ap][0]) if procm == []: if options.timestamp == True: - print str(time.time()), - print "WARNING: transient process", self.pids[nod][ap][1], \ - "/", self.pids[nod][ap][0], "on node %s" % nod + print(str(time.time()),) + print("WARNING: transient process", self.pids[nod][ap][1], + "/", self.pids[nod][ap][0], "on node %s" % nod) else: nodeapps[ap] = procm self.pids[nod][ap].append(nodeapps[ap]) @@ -436,8 +435,8 @@ class LogSession(object): for k in self.nodemetricsC: if options.timestamp: - print str(time.time()) + ",", - print k, ",", mm[k].tocsv() + print(str(time.time()) + ",",) + print(k, ",", mm[k].tocsv()) def readnodethresholds(self, filename): if filename is None: @@ -458,8 +457,8 @@ class LogSession(object): alarm = ["node", nname + "/" + self.pids[nname][0][0], keyname, calcm.getvalue(keyname), ">", self.nodethresholds.getvalue(keyname)] if options.timestamp: - print str(time.time()) + ",", - print ", ".join(str(x) for x in alarm) + print(str(time.time()) + ",",) + print(", ".join(str(x) for x in alarm)) def calcnodemetrics(self, cputimea, cputimeb, mems): """ @@ -472,10 +471,10 @@ class LogSession(object): hostusedcpu = p[0] + p[1] hostusedmem = mems[0] - mems[1] if hostusedcpu == 0: - print "WARNING: host used cpu = 0, ", p[0], p[1] + print("WARNING: host used cpu = 0, ", p[0], p[1]) hostusedcpu = 1 if hostusedmem == 0: - print "WARNING: host used mem = 0, ", mems[0], mems[1] + print("WARNING: host used mem = 0, ", mems[0], mems[1]) hostusedmem = 1 nodesa = self.nodemetricsA @@ -490,8 +489,8 @@ class LogSession(object): (False == isinstance(nodesb[nod], NodeMetrics)) | \ (False == isinstance(nodesa[nod], NodeMetrics)): if options.timestamp == True: - print str(time.time()), - print "Warning: nodes %s is not fully instanciated" % nod + print(str(time.time()),) + print("Warning: nodes %s is not fully instanciated" % nod) else: # calc throughput kbps calcm.setvalue("nodethroughput", "%.2f" % (8 * (nodesb[nod].getvalue("nodethroughput") @@ -519,7 +518,7 @@ class LogSession(object): except IndexError: pass else: - print "Warning: transient node %s " % nod + print("Warning: transient node %s " % nod) return nodesb @@ -531,21 +530,18 @@ def main(): configfile="/etc/core/perflogserver.conf", alarm=True, session=None) parser.add_option("-i", "--interval", dest="interval", type=int, - help="seconds to wait between samples; default=%s" % - parser.defaults["interval"]) + help="seconds to wait between samples; default=%s" % parser.defaults["interval"]) parser.add_option("-t", "--timestamp", action="store_true", dest="timestamp", help="include timestamp on each line") parser.add_option("-c", "--configfile", dest="configfile", type="string", - help="read threshold values from the specified file;\ - default=%s" % parser.defaults["configfile"]) + help="read threshold values from the specified file;default=%s" % parser.defaults["configfile"]) parser.add_option("-a", "--alarm", action="store_true", dest="alarm", help="generate alarms based threshold check on each cycle") parser.add_option("-s", "--session", dest="session", type=int, - help="CORE session id; default=%s" % - parser.defaults["session"]) + help="CORE session id; default=%s" % parser.defaults["session"]) global options global ncpus global serverthresholds @@ -573,12 +569,12 @@ def main(): logsession = LogSession() # mark host log baseline - print "server: ", ", ".join(str(x) for x in os.uname()), ",", ncpus, "CPU cores" - print "start timestamp:", time.time(), ", baseline data: " - print csvserverbaseline() - print "server metrics: ", ", ".join(str(x) for x in serverthresholds.getkeys()) + print("server: ", ", ".join(str(x) for x in os.uname()), ",", ncpus, "CPU cores") + print("start timestamp:", time.time(), ", baseline data: ") + print(csvserverbaseline()) + print("server metrics: ", ", ".join(str(x) for x in serverthresholds.getkeys())) if options.session is not None: - print "node metrics: nodename, ", ", ".join(str(x) for x in logsession.nodethresholds.getkeys()) + print("node metrics: nodename, ", ", ".join(str(x) for x in logsession.nodethresholds.getkeys())) cyclecount = 0 while True: @@ -591,7 +587,7 @@ def main(): calccputime = calcservercputimes(cputimea, cputimeb) m = csvservermetrics(collectservermetrics( calccputime, mems, options.alarm)) - print m + print(m) if options.session is not None: nodesb = logsession.getnodemetrics('b')