Commit 75f3762b authored by Martín Ferrari's avatar Martín Ferrari

Some linting of the code.

parent 675a3819
......@@ -56,7 +56,7 @@ print "Connectivity IPv4 OK!"
if X:
app1 = node1.Popen("%s -geometry -0+0 -e %s -ni %s" %
(xterm, nemu.environ.tcpdump_path, if1b.name), shell = True)
(xterm, nemu.environ.TCPDUMP_PATH, if1b.name), shell = True)
time.sleep(3)
app0 = node0.Popen("%s -geometry +0+0 -e ping -c 10 10.0.1.2" % xterm,
shell = True)
......
......@@ -20,54 +20,54 @@
import errno, os, os.path, socket, subprocess, sys, syslog
from syslog import LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG
__all__ = ["ip_path", "tc_path", "brctl_path", "sysctl_path", "hz"]
__all__ += ["tcpdump_path", "netperf_path", "xauth_path", "xdpyinfo_path"]
__all__ = ["IP_PATH", "TC_PATH", "BRCTL_PATH", "SYSCTL_PATH", "HZ"]
__all__ += ["TCPDUMP_PATH", "NETPERF_PATH", "XAUTH_PATH", "XDPYINFO_PATH"]
__all__ += ["execute", "backticks", "eintr_wrapper"]
__all__ += ["find_listen_port"]
__all__ += ["LOG_ERR", "LOG_WARNING", "LOG_NOTICE", "LOG_INFO", "LOG_DEBUG"]
__all__ += ["set_log_level", "logger"]
__all__ += ["error", "warning", "notice", "info", "debug"]
def find_bin(name, extra_path = None):
"""Try hard to find the location of needed programs."""
search = []
if "PATH" in os.environ:
search += os.environ["PATH"].split(":")
for pref in ("/", "/usr/", "/usr/local/"):
for d in ("bin", "sbin"):
search.append(pref + d)
search.extend(os.path.join(x, y)
for x in ("/", "/usr/", "/usr/local/")
for y in ("bin", "sbin"))
if extra_path:
search += extra_path
for d in search:
try:
os.stat(d + "/" + name)
return d + "/" + name
except OSError, e:
if e.errno != os.errno.ENOENT:
raise
for dirr in search:
path = os.path.join(dirr, name)
if os.path.exists(path):
return path
return None
def find_bin_or_die(name, extra_path = None):
r = find_bin(name)
if not r:
raise RuntimeError(("Cannot find `%s' command, impossible to " +
"continue.") % name)
return r
"""Try hard to find the location of needed programs; raise on failure."""
res = find_bin(name, extra_path)
if not res:
raise RuntimeError("Cannot find `%s', impossible to continue." % name)
return res
ip_path = find_bin_or_die("ip")
tc_path = find_bin_or_die("tc")
brctl_path = find_bin_or_die("brctl")
sysctl_path = find_bin_or_die("sysctl")
IP_PATH = find_bin_or_die("ip")
TC_PATH = find_bin_or_die("tc")
BRCTL_PATH = find_bin_or_die("brctl")
SYSCTL_PATH = find_bin_or_die("sysctl")
# Optional tools
tcpdump_path = find_bin("tcpdump")
netperf_path = find_bin("netperf")
xauth_path = find_bin("xauth")
xdpyinfo_path = find_bin("xdpyinfo")
TCPDUMP_PATH = find_bin("tcpdump")
NETPERF_PATH = find_bin("netperf")
XAUTH_PATH = find_bin("xauth")
XDPYINFO_PATH = find_bin("xdpyinfo")
# Seems this is completely bogus. At least, we can assume that the internal HZ
# is bigger than this.
hz = os.sysconf("SC_CLK_TCK")
HZ = os.sysconf("SC_CLK_TCK")
try:
os.stat("/sys/class/net")
......@@ -76,43 +76,56 @@ except:
"continue.")
def execute(cmd):
"""Execute a command, if the return value is non-zero, raise an exception.
Raises:
RuntimeError: the command was unsuccessful (return code != 0).
"""
debug("execute(%s)" % cmd)
null = open("/dev/null", "r+")
p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
proc = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
_, err = proc.communicate()
if proc.returncode != 0:
raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
def backticks(cmd):
"""Execute a command and capture its output.
If the return value is non-zero, raise an exception.
Returns:
(stdout, stderr): tuple containing the captured output.
Raises:
RuntimeError: the command was unsuccessful (return code != 0).
"""
debug("backticks(%s)" % cmd)
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
out, err = proc.communicate()
if proc.returncode != 0:
raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
return out
def eintr_wrapper(f, *args):
def eintr_wrapper(func, *args):
"Wraps some callable with a loop that retries on EINTR."
while True:
try:
return f(*args)
except OSError, e: # pragma: no cover
if e.errno == errno.EINTR:
return func(*args)
except OSError, ex: # pragma: no cover
if ex.errno == errno.EINTR:
continue
raise
except IOError, e: # pragma: no cover
if e.errno == errno.EINTR:
except IOError, ex: # pragma: no cover
if ex.errno == errno.EINTR:
continue
raise
def find_listen_port(family = socket.AF_INET, type = socket.SOCK_STREAM,
proto = 0, addr = "127.0.0.1", min_port = 1, max_port = 65535):
s = socket.socket(family, type, proto)
for p in range(min_port, max_port + 1):
sock = socket.socket(family, type, proto)
for port in range(min_port, max_port + 1):
try:
s.bind((addr, p))
return s, p
sock.bind((addr, port))
return sock, port
except socket.error:
pass
raise RuntimeError("Cannot find an usable port in the range specified")
......@@ -130,36 +143,36 @@ def set_log_level(level):
assert level > LOG_ERR and level <= LOG_DEBUG
_log_level = level
def set_log_output(file):
def set_log_output(stream):
"Redirect console messages to the provided stream."
global _log_stream
assert hasattr(file, "write") and hasattr(file, "flush")
_log_stream = file
assert hasattr(stream, "write") and hasattr(stream, "flush")
_log_stream = stream
def log_use_syslog(use = True, ident = None, logopt = 0,
facility = syslog.LOG_USER):
"Enable or disable the use of syslog for logging messages."
global _log_use_syslog, _log_syslog_opts
if not use:
if _log_use_syslog:
syslog.closelog()
_log_use_syslog = False
_log_syslog_opts = (ident, logopt, facility)
_log_use_syslog = use
_init_log()
def _init_log():
if not _log_use_syslog:
syslog.closelog()
return
(ident, logopt, facility) = _log_syslog_opts
if not ident:
#ident = os.path.basename(sys.argv[0])
ident = "nemu"
syslog.openlog("%s[%d]" % (ident, os.getpid()), logopt, facility)
_log_syslog_opts = (ident, logopt, facility)
_log_use_syslog = True
info("Syslog logging started")
def logger(priority, message):
"Print a log message in syslog, console or both."
global _log_use_syslog, _log_stream
if _log_use_syslog:
if os.getpid() != _log_pid:
# Re-init logging
log_use_syslog(_log_use_syslog, *_log_syslog_opts)
_init_log() # Need to tell syslog the new PID.
syslog.syslog(priority, message)
return
if priority > _log_level:
......@@ -180,14 +193,13 @@ def info(message):
def debug(message):
logger(LOG_DEBUG, message)
# Used to print extra info in nested exceptions
def _custom_hook(t, v, tb): # pragma: no cover
if hasattr(v, "child_traceback"):
def _custom_hook(tipe, value, traceback): # pragma: no cover
"""Custom exception hook, to print nested exceptions information."""
if hasattr(value, "child_traceback"):
sys.stderr.write("Nested exception, original traceback " +
"(most recent call last):\n")
sys.stderr.write(v.child_traceback + ("-" * 70) + "\n")
sys.__excepthook__(t, v, tb)
sys.stderr.write(value.child_traceback + ("-" * 70) + "\n")
sys.__excepthook__(tipe, value, traceback)
# XXX: somebody kill me, I deserve it :)
sys.excepthook = _custom_hook
......@@ -71,7 +71,7 @@ class NSInterface(Interface):
# Disable auto-configuration
# you wish: need to take into account the nonetns mode; plus not
# touching some pre-existing ifaces
#node.system([sysctl_path, '-w', 'net.ipv6.conf.%s.autoconf=0' %
#node.system([SYSCTL_PATH, '-w', 'net.ipv6.conf.%s.autoconf=0' %
#self.name])
node._add_interface(self)
......
......@@ -299,7 +299,7 @@ def get_if_data():
In each dictionary, values are interface objects.
"""
ipdata = backticks([ip_path, "-o", "link", "list"])
ipdata = backticks([IP_PATH, "-o", "link", "list"])
byidx = {}
bynam = {}
......@@ -348,7 +348,7 @@ def create_if_pair(if1, if2):
if iface[i].mtu:
cmd[i] += ["mtu", str(iface[i].mtu)]
cmd = [ip_path, "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1]
cmd = [IP_PATH, "link", "add"] + cmd[0] + ["type", "veth", "peer"] + cmd[1]
execute(cmd)
try:
set_if(if1)
......@@ -366,7 +366,7 @@ def create_if_pair(if1, if2):
def del_if(iface):
ifname = _get_if_name(iface)
execute([ip_path, "link", "del", ifname])
execute([IP_PATH, "link", "del", ifname])
def set_if(iface, recover = True):
def do_cmds(cmds, orig_iface):
......@@ -383,7 +383,7 @@ def set_if(iface, recover = True):
# Name goes first
if diff.name:
_ils = [ip_path, "link", "set", "dev"]
_ils = [IP_PATH, "link", "set", "dev"]
cmds = [_ils + [orig_iface.name, "name", diff.name]]
if orig_iface.up:
# iface needs to be down
......@@ -392,7 +392,7 @@ def set_if(iface, recover = True):
do_cmds(cmds, orig_iface)
# I need to use the new name after a name change, duh!
_ils = [ip_path, "link", "set", "dev", diff.name or orig_iface.name]
_ils = [IP_PATH, "link", "set", "dev", diff.name or orig_iface.name]
cmds = []
if diff.lladdr:
if orig_iface.up:
......@@ -417,12 +417,12 @@ def set_if(iface, recover = True):
def change_netns(iface, netns):
ifname = _get_if_name(iface)
execute([ip_path, "link", "set", "dev", ifname, "netns", str(netns)])
execute([IP_PATH, "link", "set", "dev", ifname, "netns", str(netns)])
# Address handling
def get_addr_data():
ipdata = backticks([ip_path, "-o", "addr", "list"])
ipdata = backticks([IP_PATH, "-o", "addr", "list"])
byidx = {}
bynam = {}
......@@ -461,7 +461,7 @@ def add_addr(iface, address):
addresses = get_addr_data()[1][ifname]
assert address not in addresses
cmd = [ip_path, "addr", "add", "dev", ifname, "local",
cmd = [IP_PATH, "addr", "add", "dev", ifname, "local",
"%s/%d" % (address.address, int(address.prefix_len))]
if hasattr(address, "broadcast"):
cmd += ["broadcast", address.broadcast if address.broadcast else "+"]
......@@ -472,7 +472,7 @@ def del_addr(iface, address):
addresses = get_addr_data()[1][ifname]
assert address in addresses
cmd = [ip_path, "addr", "del", "dev", ifname, "local",
cmd = [IP_PATH, "addr", "del", "dev", ifname, "local",
"%s/%d" % (address.address, int(address.prefix_len))]
execute(cmd)
......@@ -546,7 +546,7 @@ def create_bridge(br):
if isinstance(br, str):
br = interface(name = br)
assert br.name
execute([brctl_path, "addbr", br.name])
execute([BRCTL_PATH, "addbr", br.name])
try:
set_if(br)
except:
......@@ -560,7 +560,7 @@ def create_bridge(br):
def del_bridge(br):
brname = _get_if_name(br)
execute([brctl_path, "delbr", brname])
execute([BRCTL_PATH, "delbr", brname])
def set_bridge(br, recover = True):
def saveval(fname, val):
......@@ -599,18 +599,18 @@ def set_bridge(br, recover = True):
def add_bridge_port(br, iface):
ifname = _get_if_name(iface)
brname = _get_if_name(br)
execute([brctl_path, "addif", brname, ifname])
execute([BRCTL_PATH, "addif", brname, ifname])
def del_bridge_port(br, iface):
ifname = _get_if_name(iface)
brname = _get_if_name(br)
execute([brctl_path, "delif", brname, ifname])
execute([BRCTL_PATH, "delif", brname, ifname])
# Routing
def get_all_route_data():
ipdata = backticks([ip_path, "-o", "route", "list"]) # "table", "all"
ipdata += backticks([ip_path, "-o", "-f", "inet6", "route", "list"])
ipdata = backticks([IP_PATH, "-o", "route", "list"]) # "table", "all"
ipdata += backticks([IP_PATH, "-o", "-f", "inet6", "route", "list"])
ifdata = get_if_data()[1]
ret = []
......@@ -655,7 +655,7 @@ def del_route(route):
_add_del_route("del", route)
def _add_del_route(action, route):
cmd = [ip_path, "route", action]
cmd = [IP_PATH, "route", action]
if route.tipe != "unicast":
cmd += [route.tipe]
if route.prefix:
......@@ -671,7 +671,7 @@ def _add_del_route(action, route):
# TC stuff
def get_tc_tree():
tcdata = backticks([tc_path, "qdisc", "show"])
tcdata = backticks([TC_PATH, "qdisc", "show"])
data = {}
for line in tcdata.split("\n"):
......@@ -827,7 +827,7 @@ def clear_tc(iface):
if tcdata[iface.index] == None:
return
# Any other case, we clean
execute([tc_path, "qdisc", "del", "dev", iface.name, "root"])
execute([TC_PATH, "qdisc", "del", "dev", iface.name, "root"])
def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
delay_correlation = None, delay_distribution = None,
......@@ -843,7 +843,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
commands = []
if tcdata[iface.index] == 'foreign':
# Avoid the overhead of calling tc+ip again
commands.append([tc_path, "qdisc", "del", "dev", iface.name, "root"])
commands.append([TC_PATH, "qdisc", "del", "dev", iface.name, "root"])
tcdata[iface.index] = {'qdiscs': []}
has_netem = 'netem' in tcdata[iface.index]['qdiscs']
......@@ -859,19 +859,19 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
else:
# Too much work to do better :)
if has_netem or has_tbf:
commands.append([tc_path, "qdisc", "del", "dev", iface.name,
commands.append([TC_PATH, "qdisc", "del", "dev", iface.name,
"root"])
cmd = "add"
if bandwidth:
rate = "%dbit" % int(bandwidth)
mtu = ifdata[iface.index].mtu
burst = max(mtu, int(bandwidth) / hz)
burst = max(mtu, int(bandwidth) / HZ)
limit = burst * 2 # FIXME?
handle = "1:"
if cmd == "change":
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["tbf"])
command = [tc_path, "qdisc", cmd, "dev", iface.name, "root", "handle",
command = [TC_PATH, "qdisc", cmd, "dev", iface.name, "root", "handle",
handle, "tbf", "rate", rate, "limit", str(limit), "burst",
str(burst)]
commands.append(command)
......@@ -880,7 +880,7 @@ def set_tc(iface, bandwidth = None, delay = None, delay_jitter = None,
handle = "2:"
if cmd == "change":
handle = "%d:" % int(tcdata[iface.index]["qdiscs"]["netem"])
command = [tc_path, "qdisc", cmd, "dev", iface.name, "handle", handle]
command = [TC_PATH, "qdisc", cmd, "dev", iface.name, "handle", handle]
if bandwidth:
parent = "1:"
if cmd == "change":
......
......@@ -203,8 +203,8 @@ def _start_child(nonetns):
# create new name space
unshare.unshare(unshare.CLONE_NEWNET)
# Enable packet forwarding
execute([sysctl_path, '-w', 'net.ipv4.ip_forward=1'])
execute([sysctl_path, '-w', 'net.ipv6.conf.default.forwarding=1'])
execute([SYSCTL_PATH, '-w', 'net.ipv4.ip_forward=1'])
execute([SYSCTL_PATH, '-w', 'net.ipv6.conf.default.forwarding=1'])
srv.run()
except BaseException, e:
s = "Slave node aborting: %s\n" % str(e)
......
......@@ -364,7 +364,7 @@ class Server(object):
os.close(fd)
# stupid xauth format: needs the 'hostname' for local
# connections
execute([xauth_path, "-f", xauth, "add",
execute([XAUTH_PATH, "-f", xauth, "add",
"%s/unix:%d" % (socket.gethostname(), display),
protoname, hexkey])
if user:
......@@ -506,7 +506,7 @@ class Server(object):
self.reply(200, "Done.")
def do_X11_SET(self, cmdname, protoname, hexkey):
if not xauth_path:
if not XAUTH_PATH:
self.reply(500, "Impossible to forward X: xauth not present")
return
skt, port = None, None
......@@ -788,9 +788,9 @@ class Client(object):
raise RuntimeError("Impossible to forward X: DISPLAY variable not "
"set or invalid")
xauthdpy, sock, addr = xinfo
if not xauth_path:
if not XAUTH_PATH:
raise RuntimeError("Impossible to forward X: xauth not present")
auth = backticks([xauth_path, "list", xauthdpy])
auth = backticks([XAUTH_PATH, "list", xauthdpy])
match = re.match(r"\S+\s+(\S+)\s+(\S+)\n", auth)
if not match:
raise RuntimeError("Impossible to forward X: invalid DISPLAY")
......
......@@ -230,9 +230,9 @@ class TestGlobal(unittest.TestCase):
class TestX11(unittest.TestCase):
@test_util.skipUnless("DISPLAY" in os.environ, "Test requires working X11")
@test_util.skipUnless(nemu.environ.xdpyinfo_path, "Test requires xdpyinfo")
@test_util.skipUnless(nemu.environ.XDPYINFO_PATH, "Test requires xdpyinfo")
def test_run_xdpyinfo(self):
xdpy = nemu.environ.xdpyinfo_path
xdpy = nemu.environ.XDPYINFO_PATH
info = nemu.environ.backticks([xdpy])
# remove first line, contains the display name
info = info.partition("\n")[2]
......@@ -243,9 +243,9 @@ class TestX11(unittest.TestCase):
@test_util.skipUnless(os.getuid() == 0, "Test requires root privileges")
@test_util.skipUnless("DISPLAY" in os.environ, "Test requires working X11")
@test_util.skipUnless(nemu.environ.xdpyinfo_path, "Test requires xdpyinfo")
@test_util.skipUnless(nemu.environ.XDPYINFO_PATH, "Test requires xdpyinfo")
def test_run_xdpyinfo_netns(self):
xdpy = nemu.environ.xdpyinfo_path
xdpy = nemu.environ.XDPYINFO_PATH
info = nemu.environ.backticks([xdpy])
# remove first line, contains the display name
info = info.partition("\n")[2]
......
......@@ -141,7 +141,7 @@ class TestInterfaces(unittest.TestCase):
self.assertTrue(devs[if0.name]['up'])
# Verify that data is actually read from the kernel
r = node0.system([ip_path, "link", "set", if0.name, "mtu", "1500"])
r = node0.system([IP_PATH, "link", "set", if0.name, "mtu", "1500"])
self.assertEquals(r, 0)
devs = get_devs_netns(node0)
self.assertEquals(devs[if0.name]['mtu'], 1500)
......@@ -188,7 +188,7 @@ class TestWithDummy(unittest.TestCase):
node = nemu.Node()
self.dummyname = "dummy%d" % os.getpid()
self.assertEquals(os.system("%s link add name %s type dummy" %
(ip_path, self.dummyname)), 0)
(IP_PATH, self.dummyname)), 0)
devs = get_devs()
self.assertTrue(self.dummyname in devs)
dummyidx = devs[self.dummyname]['idx']
......@@ -213,7 +213,7 @@ class TestWithDummy(unittest.TestCase):
def tearDown(self):
# oops here
if hasattr(self, 'dummyname'):
os.system("%s link del %s" % (ip_path, self.dummyname))
os.system("%s link del %s" % (IP_PATH, self.dummyname))
# FIXME: Links
......
......@@ -51,7 +51,8 @@ class TestNode(unittest.TestCase):
orig_devs = len(test_util.get_devs())
chld = os.fork()
if chld == 0:
nemu.set_cleanup_hooks(on_exit = True, on_signals = [])
# TODO: not implemented.
#nemu.set_cleanup_hooks(on_exit = True, on_signals = [])
create_stuff()
os._exit(0)
os.waitpid(chld, 0)
......@@ -61,8 +62,9 @@ class TestNode(unittest.TestCase):
orig_devs = len(test_util.get_devs())
chld = os.fork()
if chld == 0:
nemu.set_cleanup_hooks(on_exit = False,
on_signals = [signal.SIGTERM])
# TODO: not implemented.
#nemu.set_cleanup_hooks(on_exit = False,
# on_signals = [signal.SIGTERM])
create_stuff()
while True:
time.sleep(10)
......
......@@ -50,7 +50,7 @@ class TestSwitch(unittest.TestCase):
# Test strange rules handling
os.system(("%s qd add dev %s root prio bands 3 " +
"priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1") %
(nemu.environ.tc_path, i1.control.name))
(nemu.environ.TC_PATH, i1.control.name))
tcdata = nemu.iproute.get_tc_data()[0]
self.assertEquals(tcdata[i1.control.index], "foreign")
l.set_parameters(bandwidth = 13107200) # 100 mbits
......
......@@ -54,11 +54,11 @@ def process_ipcmd(str):
return out
def get_devs():
outdata = backticks([ip_path, "addr", "list"])
outdata = backticks([IP_PATH, "addr", "list"])
return process_ipcmd(outdata)
def get_devs_netns(node):
out = nemu.subprocess_.backticks_raise(node, [ip_path, "addr", "list"])
out = nemu.subprocess_.backticks_raise(node, [IP_PATH, "addr", "list"])
return process_ipcmd(out)
def make_linux_ver(string):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment