cleanup for core.misc and core.netns

This commit is contained in:
bharnden 2018-10-11 16:01:17 -07:00
parent 05a5574155
commit eb04e0a79f
7 changed files with 29 additions and 29 deletions

View file

@ -155,7 +155,8 @@ class EventLoop(object):
schedule = True
break
event = heapq.heappop(self.queue)
assert event.time <= now
if event.time > now:
raise ValueError("invalid event time: %s > %s", event.time, now)
event.run()
with self.lock:
@ -170,11 +171,13 @@ class EventLoop(object):
:return: nothing
"""
with self.lock:
assert self.running
if not self.running:
raise ValueError("scheduling event while not running")
if not self.queue:
return
delay = self.queue[0].time - time.time()
assert self.timer is None
if self.timer:
raise ValueError("timer was already set")
self.timer = Timer(delay, self.__run_events)
self.timer.daemon = True
self.timer.start()

View file

@ -31,7 +31,7 @@ class MacAddress(object):
:return: string representation
:rtype: str
"""
return ":".join(map(lambda x: "%02x" % ord(x), self.addr))
return ":".join("%02x" % ord(x) for x in self.addr)
def to_link_local(self):
"""
@ -61,7 +61,7 @@ class MacAddress(object):
:return: mac address class
:rtype: MacAddress
"""
addr = "".join(map(lambda x: chr(int(x, 16)), s.split(":")))
addr = "".join(chr(int(x, 16)) for x in s.split(":"))
return cls(addr)
@classmethod
@ -154,14 +154,14 @@ class IpAddress(object):
logger.exception("error during addition")
return NotImplemented
tmp = map(lambda x: ord(x), self.addr)
tmp = [ord(x) for x in self.addr]
for i in xrange(len(tmp) - 1, -1, -1):
x = tmp[i] + carry
tmp[i] = x & 0xff
carry = x >> 8
if carry == 0:
break
addr = "".join(map(lambda x: chr(x), tmp))
addr = "".join(chr(x) for x in tmp)
return self.__class__(self.af, addr)
def __sub__(self, other):
@ -200,8 +200,8 @@ class IpAddress(object):
:return: integer value
:rtype: int
"""
bin = socket.inet_pton(AF_INET, s)
return struct.unpack("!I", bin)[0]
value = socket.inet_pton(AF_INET, s)
return struct.unpack("!I", value)[0]
class IpPrefix(object):

View file

@ -293,10 +293,10 @@ def file_demunge(pathname, header):
start = None
end = None
for i in range(len(lines)):
if lines[i] == "# BEGIN %s\n" % header:
for i, line in enumerate(lines):
if line == "# BEGIN %s\n" % header:
start = i
elif lines[i] == "# END %s\n" % header:
elif line == "# END %s\n" % header:
end = i + 1
if start is None or end is None:

View file

@ -211,9 +211,7 @@ class PtpNet(LxBrNet):
if len(self._netif) != 2:
return all_links
if1, if2 = self._netif.items()
if1 = if1[1]
if2 = if2[1]
if1, if2 = self._netif.values()
unidirectional = 0
if if1.getparams() != if2.getparams():
@ -224,7 +222,7 @@ class PtpNet(LxBrNet):
interface1_ip6 = None
interface1_ip6_mask = None
for address in if1.addrlist:
ip, sep, mask = address.partition("/")
ip, _sep, mask = address.partition("/")
mask = int(mask)
if ipaddress.is_ipv4_address(ip):
family = AF_INET
@ -242,7 +240,7 @@ class PtpNet(LxBrNet):
interface2_ip6 = None
interface2_ip6_mask = None
for address in if2.addrlist:
ip, sep, mask = address.partition("/")
ip, _sep, mask = address.partition("/")
mask = int(mask)
if ipaddress.is_ipv4_address(ip):
family = AF_INET

View file

@ -449,9 +449,7 @@ class OvsPtpNet(OvsNet):
if len(self._netif) != 2:
return all_links
if1, if2 = self._netif.items()
if1 = if1[1]
if2 = if2[1]
if1, if2 = self._netif.values()
unidirectional = 0
if if1.getparams() != if2.getparams():
@ -462,7 +460,7 @@ class OvsPtpNet(OvsNet):
interface1_ip6 = None
interface1_ip6_mask = None
for address in if1.addrlist:
ip, sep, mask = address.partition("/")
ip, _sep, mask = address.partition("/")
mask = int(mask)
if ipaddress.is_ipv4_address(ip):
family = AF_INET
@ -480,7 +478,7 @@ class OvsPtpNet(OvsNet):
interface2_ip6 = None
interface2_ip6_mask = None
for address in if2.addrlist:
ip, sep, mask = address.partition("/")
ip, _sep, mask = address.partition("/")
mask = int(mask)
if ipaddress.is_ipv4_address(ip):
family = AF_INET

View file

@ -140,11 +140,9 @@ class EbtablesQueue(object):
while self.doupdateloop:
with self.updatelock:
for wlan in self.updates:
"""
Check if wlan is from a previously closed session. Because of the
rate limiting scheme employed here, this may happen if a new session
is started soon after closing a previous session.
"""
# Check if wlan is from a previously closed session. Because of the
# rate limiting scheme employed here, this may happen if a new session
# is started soon after closing a previous session.
# TODO: if these are WlanNodes, this will never throw an exception
try:
wlan.session

View file

@ -364,7 +364,7 @@ class SimpleLxcNode(PyCoreNode):
if self.up:
self.check_cmd([constants.IP_BIN, "addr", "del", str(addr), "dev", self.ifname(ifindex)])
def delalladdr(self, ifindex, address_types=valid_address_types):
def delalladdr(self, ifindex, address_types=None):
"""
Delete all addresses from an interface.
@ -373,6 +373,9 @@ class SimpleLxcNode(PyCoreNode):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
if not address_types:
address_types = self.valid_address_types
interface_name = self.ifname(ifindex)
addresses = self.client.getaddr(interface_name, rescan=True)
@ -570,7 +573,7 @@ class LxcNode(SimpleLxcNode):
:rtype: file
"""
hostfilename = self.hostfilename(filename)
dirname, basename = os.path.split(hostfilename)
dirname, _basename = os.path.split(hostfilename)
if not os.path.isdir(dirname):
os.makedirs(dirname, mode=0755)
return open(hostfilename, mode)