Commit 81747f7d authored by Martín Ferrari's avatar Martín Ferrari

Get rid of yaml, to avoid the extra dependency

parent 96c90e64
python-yaml PyYAML Required packages for using netns
python-unshare =================================
python-passfd
linux-kernel linux-image >= 2.6.35 python-unshare http://pypi.python.org/pypi/python-unshare
python-passfd http://pypi.python.org/pypi/python-passfd
linux-kernel >= 2.6.35 http://kernel.org/
#!/usr/bin/env python #!/usr/bin/env python
# vim:ts=4:sw=4:et:ai:sts=4 # vim:ts=4:sw=4:et:ai:sts=4
try: # pragma: no cover import base64, os, passfd, re, signal, sys, traceback, unshare
from yaml import CLoader as Loader
from yaml import CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import base64, os, passfd, re, signal, sys, traceback, unshare, yaml
import netns.subprocess_, netns.iproute import netns.subprocess_, netns.iproute
try:
from cPickle import loads, dumps
except:
from pickle import loads, dumps
# ============================================================================ # ============================================================================
# Server-side protocol implementation # Server-side protocol implementation
# #
...@@ -174,8 +173,7 @@ class Server(object): ...@@ -174,8 +173,7 @@ class Server(object):
return None return None
elif argstemplate[j] == 'b': elif argstemplate[j] == 'b':
try: try:
if args[i][0] == '=': args[i] = _db64(args[i])
args[i] = base64.b64decode(args[i][1:])
except TypeError: except TypeError:
self.reply(500, "Invalid parameter: not base-64 encoded.") self.reply(500, "Invalid parameter: not base-64 encoded.")
return None return None
...@@ -203,7 +201,8 @@ class Server(object): ...@@ -203,7 +201,8 @@ class Server(object):
(t, v, tb) = sys.exc_info() (t, v, tb) = sys.exc_info()
v.child_traceback = "".join( v.child_traceback = "".join(
traceback.format_exception(t, v, tb)) traceback.format_exception(t, v, tb))
self.reply(550, yaml.dump(v)) self.reply(550, ["# Exception data follows:",
_b64(dumps(v, protocol = 2))])
try: try:
self._rfd.close() self._rfd.close()
self._wfd.close() self._wfd.close()
...@@ -327,8 +326,8 @@ class Server(object): ...@@ -327,8 +326,8 @@ class Server(object):
ifdata = netns.iproute.get_if_data()[0] ifdata = netns.iproute.get_if_data()[0]
else: else:
ifdata = netns.iproute.get_if(ifnr) ifdata = netns.iproute.get_if(ifnr)
self.reply(200, ["# Interface data follows."] + self.reply(200, ["# Interface data follows.",
yaml.dump(ifdata).split("\n")) _b64(dumps(ifdata, protocol = 2))])
def do_IF_SET(self, cmdname, ifnr, *args): def do_IF_SET(self, cmdname, ifnr, *args):
if len(args) % 2: if len(args) % 2:
...@@ -355,8 +354,8 @@ class Server(object): ...@@ -355,8 +354,8 @@ class Server(object):
addrdata = netns.iproute.get_addr_data()[0] addrdata = netns.iproute.get_addr_data()[0]
if ifnr != None: if ifnr != None:
addrdata = addrdata[ifnr] addrdata = addrdata[ifnr]
self.reply(200, ["# Address data follows."] + self.reply(200, ["# Address data follows.",
yaml.dump(addrdata).split("\n")) _b64(dumps(addrdata, protocol = 2))])
def do_ADDR_ADD(self, cmdname, ifnr, address, prefixlen, broadcast = None): def do_ADDR_ADD(self, cmdname, ifnr, address, prefixlen, broadcast = None):
if address.find(":") < 0: # crude, I know if address.find(":") < 0: # crude, I know
...@@ -376,8 +375,8 @@ class Server(object): ...@@ -376,8 +375,8 @@ class Server(object):
def do_ROUT_LIST(self, cmdname): def do_ROUT_LIST(self, cmdname):
rdata = netns.iproute.get_route_data() rdata = netns.iproute.get_route_data()
self.reply(200, ["# Routing data follows."] + self.reply(200, ["# Routing data follows.",
yaml.dump(rdata).split("\n")) _b64(dumps(rdata, protocol = 2))])
def do_ROUT_ADD(self, cmdname, tipe, prefix, prefixlen, nexthop, ifnr, def do_ROUT_ADD(self, cmdname, tipe, prefix, prefixlen, nexthop, ifnr,
metric): metric):
...@@ -442,7 +441,7 @@ class Client(object): ...@@ -442,7 +441,7 @@ class Client(object):
defaults to 2.""" defaults to 2."""
code, text = self._read_reply() code, text = self._read_reply()
if code == 550: # exception if code == 550: # exception
e = yaml.load(text) e = loads(_db64(text.partition("\n")[2]))
raise e raise e
if code / 100 != expected: if code / 100 != expected:
raise RuntimeError("Error from slave: %d %s" % (code, text)) raise RuntimeError("Error from slave: %d %s" % (code, text))
...@@ -562,7 +561,7 @@ class Client(object): ...@@ -562,7 +561,7 @@ class Client(object):
else: else:
self._send_cmd("IF", "LIST") self._send_cmd("IF", "LIST")
data = self._read_and_check_reply() data = self._read_and_check_reply()
return yaml.load(data) return loads(_db64(data.partition("\n")[2]))
def set_if(self, interface): def set_if(self, interface):
cmd = ["IF", "SET", interface.index] cmd = ["IF", "SET", interface.index]
...@@ -588,8 +587,7 @@ class Client(object): ...@@ -588,8 +587,7 @@ class Client(object):
else: else:
self._send_cmd("ADDR", "LIST") self._send_cmd("ADDR", "LIST")
data = self._read_and_check_reply() data = self._read_and_check_reply()
data = data.partition("\n")[2] # ignore first line return loads(_db64(data.partition("\n")[2]))
return yaml.load(data)
def add_addr(self, ifnr, address): def add_addr(self, ifnr, address):
if hasattr(address, "broadcast") and address.broadcast: if hasattr(address, "broadcast") and address.broadcast:
...@@ -607,7 +605,7 @@ class Client(object): ...@@ -607,7 +605,7 @@ class Client(object):
def get_route_data(self): def get_route_data(self):
self._send_cmd("ROUT", "LIST") self._send_cmd("ROUT", "LIST")
data = self._read_and_check_reply() data = self._read_and_check_reply()
return yaml.load(data) return loads(_db64(data.partition("\n")[2]))
def add_route(self, route): def add_route(self, route):
self._add_del_route("ADD", route) self._add_del_route("ADD", route)
...@@ -633,6 +631,11 @@ def _b64(text): ...@@ -633,6 +631,11 @@ def _b64(text):
else: else:
return text return text
def _db64(text):
if not text or text[0] != '=':
return text
return base64.b64decode(text[1:])
def _get_file(fd, mode): def _get_file(fd, mode):
# XXX: In some cases we do not call dup(); maybe this should be consistent? # XXX: In some cases we do not call dup(); maybe this should be consistent?
if hasattr(fd, "readline"): if hasattr(fd, "readline"):
......
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