Skip to content

Commit

Permalink
Replace print command with print function
Browse files Browse the repository at this point in the history
This works with any python version 2.6 or higher.
  • Loading branch information
Daniel Thayer committed Sep 23, 2014
1 parent 7f7d1c4 commit e5c5da4
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 131 deletions.
2 changes: 1 addition & 1 deletion BroControl/brocmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def cmdloop(self, intro=None):
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
self.stdout.write("%s\n" % self.intro)
self._stopping = False
success = True
while not self._stopping:
Expand Down
5 changes: 3 additions & 2 deletions BroControl/broctl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# The BroControl interactive shell.

from __future__ import print_function
import os
import sys
import time
Expand All @@ -22,12 +23,12 @@ def __init__(self):
pass

def output(self, txt):
print txt
print(txt)
info = output
debug = output

def error(self, txt):
print >>sys.stderr, txt
print(txt, file=sys.stderr)
warn = error

def expose(func):
Expand Down
25 changes: 13 additions & 12 deletions BroControl/broctld.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from collections import defaultdict
from threading import Thread, Lock
from Queue import Queue
Expand Down Expand Up @@ -31,11 +32,11 @@ def __init__(self):
pass

def output(self, msg):
print msg
print(msg)
warn = info = output

def error(self, msg):
print "ERROR", msg
print("ERROR", msg)

class Logs:
def __init__(self):
Expand Down Expand Up @@ -82,7 +83,7 @@ def init(self):

def recv(self):
msg = self.sock.recv()
#print "Received", self.load(msg)
#print("Received", self.load(msg))
return self.load(msg)

def send(self, *args):
Expand All @@ -105,31 +106,31 @@ def _bg(self):
time.sleep(10)

def handle_result(self, id, result):
print "Got result id=%r result=%r" % (id, result)
print("Got result id=%r result=%r" % (id, result))
self.results[id] = result
self.send("ok")

def handle_setstate(self, key, value):
print "Set state key=%r value=%r" % (key, value)
print("Set state key=%r value=%r" % (key, value))
self.state.set(key, value)
self.send("ok")

def handle_getstate(self, key):
value = self.state.get(key)
print "Get state key=%r value=%r" % (key, value)
print("Get state key=%r value=%r" % (key, value))
self.send(value)

def handle_getstateitems(self):
value = self.state.items()
self.send(value)

def handle_out(self, id, txt):
print "Got %s id=%r result=%r" % ('out', id, txt)
print("Got %s id=%r result=%r" % ('out', id, txt))
self.logs.append(id, 'out', txt)
self.send("ok")

def handle_err(self, id, txt):
print "Got %s id=%r result=%r" % ('err', id, txt)
print("Got %s id=%r result=%r" % ('err', id, txt))
self.logs.append(id, 'err', txt)
self.send("ok")

Expand All @@ -138,12 +139,12 @@ def handle_getresult(self, id):
if result:
del self.results[id]
del self.threads[id]
print "sending result=%r for id=%r" % (result, id)
print("sending result=%r for id=%r" % (result, id))
self.send(result)

def handle_getlog(self, id, since):
result = self.logs.get(id, since)
print "sending log=%r for id=%r" % (result, id)
print("sending log=%r for id=%r" % (result, id))
self.send(result)

def _run(self):
Expand All @@ -157,7 +158,7 @@ def _run(self):
t_id, t = self.spawn_worker(cmd, args)
self.send(t_id)
self.threads[t_id] = t
print "started thread for id=%r func=%r args=%r" % (t_id, func, args)
print("started thread for id=%r func=%r args=%r" % (t_id, func, args))

def spawn_worker(self, cmd, args):
t_id = self.id_gen()
Expand Down Expand Up @@ -208,7 +209,7 @@ def call(self, func, *args):

def sync_call(self, func, *args):
id = self.call(func, *args)
print "got id", id
print("got id", id)
while True:
res = self.getresult(id)
if res:
Expand Down
2 changes: 1 addition & 1 deletion BroControl/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def waitForBros(self, nodes, status, timeout, ensurerunning):
def logAction(self, node, action):
t = time.time()
out = open(self.config.statslog, "a")
print >>out, t, node, "action", action
out.write("%s %s action %s\n" % (t, node, action))
out.close()

# Do a "post-terminate crash" for the given nodes.
Expand Down
24 changes: 12 additions & 12 deletions BroControl/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def _logStats(interval, cmdout):
type = proc["proc"]
for (val, key) in proc.items():
if val != "proc":
print >>out, t, node, type, val, key
out.write("%s %s %s %s %s\n" % (t, node, type, val, key))
else:
print >>out, t, node, "error", "error", error
out.write("%s %s error error %s\n" % (t, node, error))

for (node, error, vals) in capstats:
if not error:
for (key, val) in vals.items():
print >>out, t, node, "interface", key, val
out.write("%s %s interface %s %s\n" % (t, node, key, val))

if key == "pkts" and str(node) != "$total":
# Report if we don't see packets on an interface.
Expand All @@ -133,12 +133,12 @@ def _logStats(interval, cmdout):
config.Config._setState(tag, val)

else:
print >>out, t, node, "error", "error", error
out.write("%s %s error error %s\n" % (t, node, error))

for (port, error, vals) in cflow_rates:
if not error:
for (key, val) in vals.items():
print >>out, t, "cflow", port.lower(), key, val
out.write("%s cflow %s %s %s\n" % (t, port.lower(), key, val))

out.close()

Expand Down Expand Up @@ -224,20 +224,20 @@ def _updateHTTPStats(cmdout):
return

for node in config.Config.hosts():
print >>meta, "node", node, node.type, node.host
meta.write("node %s %s %s\n" % (node, node.type, node.host))

print >>meta, "time", time.asctime()
print >>meta, "version", config.Config.version
meta.write("time %s\n" % time.asctime())
meta.write("version %s\n" % config.Config.version)

try:
print >>meta, "os", execute.runLocalCmd("uname -a")[1][0]
meta.write("os %s\n" % execute.runLocalCmd("uname -a")[1][0])
except IndexError:
print >>meta, "os <error>"
meta.write("os <error>\n")

try:
print >>meta, "host", execute.runLocalCmd("hostname")[1][0]
meta.write("host %s\n" % execute.runLocalCmd("hostname")[1][0])
except IndexError:
print >>meta, "host <error>"
meta.write("host <error>\n")

meta.close()

Expand Down
2 changes: 1 addition & 1 deletion BroControl/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _runHelperInit(host, cmdout, cmd, args, fullcmd, env):
cmdline = fullcmd

util.debug(1, cmdline, prefix=host.host)
print >>stdin, cmdline
stdin.write("%s\n" % cmdline)
stdin.flush()

return host
Expand Down
74 changes: 37 additions & 37 deletions BroControl/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ def nextPort(self, node):

filename = os.path.join(path, "standalone-layout.bro")
out = open(filename, "w")
print >>out, "# Automatically generated. Do not edit."
out.write("# Automatically generated. Do not edit.\n")
# This is the port that standalone nodes listen on for remote control by default.
print >>out, "redef Communication::listen_port = %s/tcp;" % broport.nextPort(manager)
print >>out, "redef Communication::nodes += {"
print >>out, " [\"control\"] = [$host=%s, $zone_id=\"%s\", $class=\"control\", $events=Control::controller_events]," % (util.formatBroAddr(manager.addr), manager.zone_id)
print >>out, "};"
out.write("redef Communication::listen_port = %s/tcp;\n" % broport.nextPort(manager))
out.write("redef Communication::nodes += {\n")
out.write(" [\"control\"] = [$host=%s, $zone_id=\"%s\", $class=\"control\", $events=Control::controller_events],\n" % (util.formatBroAddr(manager.addr), manager.zone_id))
out.write("};\n")
out.close()

else:
Expand All @@ -237,39 +237,39 @@ def nextPort(self, node):
workers = config.Config.nodes("workers")
proxies = config.Config.nodes("proxies")

print >>out, "# Automatically generated. Do not edit."
print >>out, "redef Cluster::nodes = {"
out.write("# Automatically generated. Do not edit.\n")
out.write("redef Cluster::nodes = {\n")

# Control definition. For now just reuse the manager information.
print >>out, "\t[\"control\"] = [$node_type=Cluster::CONTROL, $ip=%s, $zone_id=\"%s\", $p=%s/tcp]," % (util.formatBroAddr(manager.addr), config.Config.zoneid, broport.nextPort(manager))
out.write("\t[\"control\"] = [$node_type=Cluster::CONTROL, $ip=%s, $zone_id=\"%s\", $p=%s/tcp],\n" % (util.formatBroAddr(manager.addr), config.Config.zoneid, broport.nextPort(manager)))

# Manager definition
print >>out, "\t[\"%s\"] = [$node_type=Cluster::MANAGER, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $workers=set(" % (manager.name, util.formatBroAddr(manager.addr), manager.zone_id, broport.nextPort(manager)),
out.write("\t[\"%s\"] = [$node_type=Cluster::MANAGER, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $workers=set(" % (manager.name, util.formatBroAddr(manager.addr), manager.zone_id, broport.nextPort(manager)))
for s in workers:
print >>out, "\"%s\"" % (s.name),
out.write("\"%s\"" % s.name)
if s != workers[-1]:
print >>out, ",",
print >>out, ")],"
out.write(", ")
out.write(")],\n")

# Proxies definition
for p in proxies:
print >>out, "\t[\"%s\"] = [$node_type=Cluster::PROXY, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $manager=\"%s\", $workers=set(" % (p.name, util.formatBroAddr(p.addr), p.zone_id, broport.nextPort(p), manager.name),
out.write("\t[\"%s\"] = [$node_type=Cluster::PROXY, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $manager=\"%s\", $workers=set(" % (p.name, util.formatBroAddr(p.addr), p.zone_id, broport.nextPort(p), manager.name))
for s in workers:
print >>out, "\"%s\"" % (s.name),
out.write("\"%s\"" % s.name)
if s != workers[-1]:
print >>out, ",",
print >>out, ")],"
out.write(", ")
out.write(")],\n")

# Workers definition
for w in workers:
p = w.count % len(proxies)
print >>out, "\t[\"%s\"] = [$node_type=Cluster::WORKER, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $interface=\"%s\", $manager=\"%s\", $proxy=\"%s\"]," % (w.name, util.formatBroAddr(w.addr), w.zone_id, broport.nextPort(w), w.interface, manager.name, proxies[p].name)
out.write("\t[\"%s\"] = [$node_type=Cluster::WORKER, $ip=%s, $zone_id=\"%s\", $p=%s/tcp, $interface=\"%s\", $manager=\"%s\", $proxy=\"%s\"],\n" % (w.name, util.formatBroAddr(w.addr), w.zone_id, broport.nextPort(w), w.interface, manager.name, proxies[p].name))

# Activate time-machine support if configured.
if config.Config.timemachinehost:
print >>out, "\t[\"time-machine\"] = [$node_type=Cluster::TIME_MACHINE, $ip=%s, $p=%s]," % (config.Config.timemachinehost, config.Config.timemachineport)
out.write("\t[\"time-machine\"] = [$node_type=Cluster::TIME_MACHINE, $ip=%s, $p=%s],\n" % (config.Config.timemachinehost, config.Config.timemachineport))

print >>out, "};"
out.write("};\n")

out.close()

Expand Down Expand Up @@ -310,15 +310,15 @@ def makeLocalNetworks(path, cmdout, silent=False):
nets = readNetworks(netcfg)

out = open(os.path.join(path, "local-networks.bro"), "w")
print >>out, "# Automatically generated. Do not edit.\n"
out.write("# Automatically generated. Do not edit.\n\n")

print >>out, "redef Site::local_nets = {"
out.write("redef Site::local_nets = {\n")
for (cidr, tag) in nets:
print >>out, "\t%s," % cidr,
out.write("\t%s," % cidr)
if tag:
print >>out, "\t# %s" % tag,
print >>out
print >>out, "};\n"
out.write("\t# %s" % tag)
out.write("\n")
out.write("};\n\n")
out.close()

return True
Expand All @@ -335,22 +335,22 @@ def makeConfig(path, cmdout, silent=False):

filename = os.path.join(path, "broctl-config.bro")
out = open(filename, "w")
print >>out, "# Automatically generated. Do not edit."
print >>out, "redef Notice::mail_dest = \"%s\";" % config.Config.mailto
print >>out, "redef Notice::mail_dest_pretty_printed = \"%s\";" % config.Config.mailalarmsto
print >>out, "redef Notice::sendmail = \"%s\";" % config.Config.sendmail
print >>out, "redef Notice::mail_subject_prefix = \"%s\";" % config.Config.mailsubjectprefix
print >>out, "redef Notice::mail_from = \"%s\";" % config.Config.mailfrom
out.write("# Automatically generated. Do not edit.\n")
out.write("redef Notice::mail_dest = \"%s\";\n" % config.Config.mailto)
out.write("redef Notice::mail_dest_pretty_printed = \"%s\";\n" % config.Config.mailalarmsto)
out.write("redef Notice::sendmail = \"%s\";\n" % config.Config.sendmail)
out.write("redef Notice::mail_subject_prefix = \"%s\";\n" % config.Config.mailsubjectprefix)
out.write("redef Notice::mail_from = \"%s\";\n" % config.Config.mailfrom)
if manager.type != "standalone":
print >>out, "@if ( Cluster::local_node_type() == Cluster::MANAGER )"
print >>out, "redef Log::default_rotation_interval = %s secs;" % config.Config.logrotationinterval
print >>out, "redef Log::default_mail_alarms_interval = %s secs;" % config.Config.mailalarmsinterval
out.write("@if ( Cluster::local_node_type() == Cluster::MANAGER )\n")
out.write("redef Log::default_rotation_interval = %s secs;\n" % config.Config.logrotationinterval)
out.write("redef Log::default_mail_alarms_interval = %s secs;\n" % config.Config.mailalarmsinterval)
if manager.type != "standalone":
print >>out, "@endif"
out.write("@endif\n")
if config.Config.ipv6comm == "1":
print >>out, "redef Communication::listen_ipv6 = T ;"
out.write("redef Communication::listen_ipv6 = T ;\n")
else:
print >>out, "redef Communication::listen_ipv6 = F ;"
out.write("redef Communication::listen_ipv6 = F ;\n")

out.close()

Loading

0 comments on commit e5c5da4

Please sign in to comment.