diff --git a/bin/send2syslog.py b/bin/send2syslog.py index 972d5a8d71..5c4b50b534 100755 --- a/bin/send2syslog.py +++ b/bin/send2syslog.py @@ -14,16 +14,15 @@ # License for the specific language governing permissions and limitations # under the License. -import os -import sys -import signal -import string -import re import json -import time import logging from logging.handlers import SysLogHandler -from optparse import OptionParser, OptionGroup +from optparse import OptionParser +import os +import re +import signal +import sys +import time # Add syslog levels to logging module. @@ -96,7 +95,7 @@ main_logger.setLevel(logging.NOTSET) class WatchedFile: - """ WatchedFile(filename) => Object that read lines from file if exist. """ + """WatchedFile(filename) => Object that read lines from file if exist.""" def __init__(self, name): self.name = name @@ -117,7 +116,7 @@ class WatchedFile: self.close() def readLines(self): - """Return list of last append lines from file if exist. """ + """Return list of last append lines from file if exist.""" self._checkRewrite() if not self.fo: @@ -134,7 +133,7 @@ class WatchedFile: class WatchedGroup: - """ Can send data from group of specified files to specified servers. """ + """Can send data from group of specified files to specified servers.""" def __init__(self, servers, files, name): self.servers = servers @@ -172,7 +171,7 @@ class WatchedGroup: self.watchedfiles.append(WatchedFile(name)) def send(self): - """ Send append data from files to servers. """ + """Send append data from files to servers.""" for watchedfile in self.watchedfiles: for line in watchedfile.readLines(): @@ -209,7 +208,7 @@ class WatchedGroup: def sig_handler(signum, frame): - """ Send all new data when signal arrived. """ + """Send all new data when signal arrived.""" if not sending_in_progress: send_all() @@ -219,16 +218,14 @@ def sig_handler(signum, frame): def send_all(): - """ Send any updates. """ + """Send any updates.""" - sending_in_progress = 1 for group in watchlist: group.send() - sending_in_progress = 0 def main_loop(): - """ Periodicaly call sendlogs() for each group in watchlist. """ + """Periodicaly call sendlogs() for each group in watchlist.""" signal.signal(signal.SIGINT, sig_handler) signal.signal(signal.SIGTERM, sig_handler) @@ -241,13 +238,13 @@ def main_loop(): class Config: - """ Collection of config generation methods. + """Collection of config generation methods. Usage: config = Config.getConfig() """ @classmethod def getConfig(cls): - """ Generate config from command line arguments and config file. """ + """Generate config from command line arguments and config file.""" # example_config = { # "daemon": True, @@ -285,7 +282,7 @@ class Config: fo = open(cmdline.config_file, 'r') parsed_config = json.load(fo) if cmdline.debug: - print parsed_config + print(parsed_config) except IOError: # Raised if IO operations failed. main_logger.error("Can not read config file %s\n" % cmdline.config_file) @@ -321,7 +318,7 @@ class Config: @staticmethod def _getHostname(): - """ Generate hostname by BOOTIF kernel option or use os.uname().""" + """Generate hostname by BOOTIF kernel option or use os.uname().""" with open('/proc/cmdline') as fo: cpu_cmdline = fo.readline().strip() @@ -333,7 +330,7 @@ class Config: @staticmethod def cmdlineParse(): - """ Parse command line config options. """ + """Parse command line config options.""" parser = OptionParser() parser.add_option("-c", "--config", dest="config_file", metavar="FILE", @@ -383,7 +380,7 @@ class Config: @staticmethod def _checkType(value, value_type, value_name='', msg=None): - """ Check correctness of type of value and exit if not. """ + """Check correctness of type of value and exit if not.""" if not isinstance(value, value_type): message = msg or "Value %r in config have type %r but"\ @@ -394,7 +391,7 @@ class Config: @classmethod def configValidate(cls, config): - """ Validate types and names of data items in config. """ + """Validate types and names of data items in config.""" cls._checkType(config, dict, msg='Config must be a dict.') for key in ("daemon", "run_once", "debug"): diff --git a/dhcp-checker/dhcp_checker/api.py b/dhcp-checker/dhcp_checker/api.py index ce3b7472ae..63bea1c9ce 100644 --- a/dhcp-checker/dhcp_checker/api.py +++ b/dhcp-checker/dhcp_checker/api.py @@ -73,7 +73,7 @@ def check_dhcp(ifaces, timeout=5, repeat=2): else len(ifaces_filtered)) pool = multiprocessing.Pool(concurrency_limit) return itertools.chain(*pool.map(check_dhcp_on_eth, ( - (iface, timeout) for iface in ifaces_filtered*repeat))) + (iface, timeout) for iface in ifaces_filtered * repeat))) def check_dhcp_with_vlans(config, timeout=5, repeat=2): diff --git a/dhcp-checker/dhcp_checker/tests/unit/test_utils.py b/dhcp-checker/dhcp_checker/tests/unit/test_utils.py index 42de930842..948324904d 100644 --- a/dhcp-checker/dhcp_checker/tests/unit/test_utils.py +++ b/dhcp-checker/dhcp_checker/tests/unit/test_utils.py @@ -12,8 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. +from mock import call +from mock import patch import os -from mock import patch, call import unittest from scapy import all as scapy @@ -136,7 +137,7 @@ class TestMultiprocMap(unittest.TestCase): class TestIfaceStateHelper(unittest.TestCase): def test_iface_is_up(self, command, iface_state): - iface_value = iter(('UP',)*3) + iface_value = iter(('UP',) * 3) iface_state.side_effect = lambda *args, **kwargs: next(iface_value) with utils.IfaceState('eth1') as iface: self.assertEqual(iface, 'eth1') @@ -155,7 +156,7 @@ class TestIfaceStateHelper(unittest.TestCase): call('ifconfig', 'eth1', 'down')]) def test_iface_cant_ifup(self, command, iface_state): - iface_value = iter(('DOWN',)*10) + iface_value = iter(('DOWN',) * 10) iface_state.side_effect = lambda *args, **kwargs: next(iface_value) def test_raises(): diff --git a/docs/common_conf.py b/docs/common_conf.py index aeeb6a18e3..8955f01754 100644 --- a/docs/common_conf.py +++ b/docs/common_conf.py @@ -1,15 +1,18 @@ -# Add any Sphinx extension module names here, as strings. They can be extensions +# Add any Sphinx extension module names here, as strings. +# They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions += ['sphinx.ext.inheritance_diagram', 'sphinxcontrib.blockdiag', 'sphinxcontrib.actdiag', 'sphinxcontrib.seqdiag', 'sphinxcontrib.nwdiag'] - +extensions += ['sphinx.ext.inheritance_diagram', 'sphinxcontrib.blockdiag', + 'sphinxcontrib.actdiag', 'sphinxcontrib.seqdiag', + 'sphinxcontrib.nwdiag'] + # The encoding of source files. source_encoding = 'utf-8-sig' #source_encoding = 'shift_jis' - + # The language for content autogenerated by Sphinx. language = 'en' #language = 'ja' - + # The theme to use for HTML and HTML Help pages. #html_theme = 'default' #html_theme = 'sphinxdoc' @@ -18,12 +21,12 @@ language = 'en' #html_theme = 'traditional' #html_theme = 'nature' #html_theme = 'haiku' - + # If this is not the empty string, a 'Last updated on:' timestamp # is inserted at every page bottom, using the given strftime() format. # Default is '%b %d, %Y' (or a locale-dependent equivalent). html_last_updated_fmt = '%Y/%m/%d' - + # Enable Antialiasing blockdiag_antialias = True acttdiag_antialias = True @@ -31,10 +34,10 @@ seqdiag_antialias = True nwdiag_antialias = True extensions += ['rst2pdf.pdfbuilder'] -pdf_documents = [ +pdf_documents = [ (master_doc, project, project, copyright), ] -pdf_stylesheets = ['sphinx','kerning','a4','en'] +pdf_stylesheets = ['sphinx', 'kerning', 'a4', 'en'] pdf_language = "en" # Mode for literal blocks wider than the frame. Can be # overflow, shrink or truncate diff --git a/docs/conf.py b/docs/conf.py index 642c45e6f9..dd8ce23005 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -11,7 +11,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import os +import sys sys.path.insert(0, os.path.join(os.path.abspath('.'), "..", "nailgun")) autodoc_default_flags = ['members'] @@ -35,7 +36,7 @@ extensions = [ 'sphinxcontrib.plantuml', 'nailgun.autoapidoc' ] -plantuml = ['java','-jar','plantuml.jar'] +plantuml = ['java', '-jar', 'plantuml.jar'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -236,10 +237,10 @@ man_pages = [ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) -texinfo_documents = [ - ('index', 'scaffold', u'scaffold Documentation', - u'Mike Scherbakov', 'scaffold', 'One line description of project.', - 'Miscellaneous'), +texinfo_documents = [( + 'index', 'scaffold', u'scaffold Documentation', + u'Mike Scherbakov', 'scaffold', 'One line description of project.', + 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. diff --git a/netcheck.py b/netcheck.py index 6a66890250..4ff62f9067 100644 --- a/netcheck.py +++ b/netcheck.py @@ -14,25 +14,31 @@ #!/usr/bin/env python -import random -import logging import itertools +import logging +import random logging.basicConfig() logger = logging.getLogger() + class Vertex(object): def __init__(self, node, interface): self.node = node self.interface = interface + def __str__(self): return "" % (self.node, self.interface) + def __repr__(self): return self.__str__() + def __eq__(self, other): return self.node == other.node and self.interface == other.interface + def __ne__(self, other): return self.node != other.node or self.interface != other.interface + def __hash__(self): return hash(str(self)) @@ -40,20 +46,27 @@ class Vertex(object): class Arc(object): def __init__(self, vertex_a, vertex_b): self.arc = (vertex_a, vertex_b) + def __str__(self): return "" % (self.arc,) + def __repr__(self): return self.__str__() + def __getitem__(self, i): return self.arc[i] + def __eq__(self, other): l = map(lambda x, y: x == y, self.arc, other.arc) return bool(filter(lambda x: x, l)) + def __ne__(self, other): l = map(lambda x, y: x != y, self.arc, other.arc) return bool(filter(lambda x: x, l)) + def __hash__(self): return hash(str(self)) + def invert(self): return Arc(self.arc[1], self.arc[0]) @@ -62,7 +75,8 @@ class NetChecker(object): def __init__(self, nodes, arcs): self.nodes = nodes self.arcs = arcs - logger.debug("Init: got %d nodes and %d arcs", len(nodes), len(self.arcs)) + logger.debug("Init: got %d nodes and %d arcs", + len(nodes), len(self.arcs)) @staticmethod def _invert_arc(arc): @@ -84,7 +98,7 @@ class NetChecker(object): return "%s.%s" % (str(node), str(interface)) def get_topos(self): - """ Main method to collect all possible altermatives of + """Main method to collect all possible altermatives of interconnection. """ topos = [] @@ -207,7 +221,7 @@ class NetChecker(object): for t in topos: logger.debug("_uniq_topos: now testing: %s" % t) if not isincluded(t, [i for i in topos if id(i) != id(t)]): - copy.append(t) + copy.append(t) return copy @@ -229,9 +243,6 @@ class ClassbasedNetChecker(NetChecker): return Vertex(node, interface) - - - def generateFullMesh(nodes, interfaces, Klass, stability=1.0): A = [] vertices = itertools.product(nodes, interfaces, nodes, interfaces) @@ -245,6 +256,7 @@ def generateFullMesh(nodes, interfaces, Klass, stability=1.0): logger.debug("generateArcs: %d arcs generated", len(A)) return A + def generateMesh(nodes1, ifaces1, nodes2, ifaces2, Klass, stability=1.0): A = [] vertices = itertools.product(nodes1, ifaces1, nodes2, ifaces2) @@ -261,38 +273,36 @@ def generateMesh(nodes1, ifaces1, nodes2, ifaces2, Klass, stability=1.0): def printChoice(choice, step=4): def printlist(l, indent=0, step=2): - print '%s[' % (' ' * indent) + print('%s[' % (' ' * indent)) for i in l: if type(i) is dict: - print '%s-' % (' ' * indent) + print('%s-' % (' ' * indent)) printdict(i, indent + step, step) elif type(i) in (list, tuple): printlist(i, indent + step, step) else: - print '%s- %s' % (' ' * indent, str(i)) - print '%s]' % (' ' * indent) + print('%s- %s' % (' ' * indent, str(i))) + print('%s]' % (' ' * indent)) + def printdict(d, indent=0, step=2): for k, v in d.iteritems(): if type(v) is dict: - print '%s%s:' % (' ' * indent, str(k)) + print('%s%s:' % (' ' * indent, str(k))) printdict(v, indent + step, step) elif type(v) in (list, tuple): - print '%s%s:' % (' ' * indent, str(k)) + print('%s%s:' % (' ' * indent, str(k))) printlist(v, indent + step, step) else: - print '%s%s: %s' % (' ' * indent, str(k), str(v)) + print('%s%s: %s' % (' ' * indent, str(k), str(v))) if type(choice) is dict: printdict(choice, step=step) elif type(choice) is list: printlist(choice, step=step) else: - print choice + print(choice) - - - -print "" +print("") nodes = ['s1', 's2', 's3', 's4'] interfaces = ['i0', 'i1', 'i2', 'i3'] @@ -322,10 +332,10 @@ choices = netcheck.get_topos() # print "\n---- Choice number %d: ----\n" % (i + 1) # printChoice(choices[i]) if not choices: - print "No choices found" + print("No choices found") else: - print "%d choices found" % len(choices) -print "" + print("%d choices found" % len(choices)) +print("") #import time #time.sleep(5)