Skip to content

Commit

Permalink
Merge pull request #150 from vivekkumac/pep8
Browse files Browse the repository at this point in the history
autopep8: Adding automated tool to format extra/missing whitespace
  • Loading branch information
rdobson authored Sep 29, 2016
2 parents 69ce059 + 5d00c49 commit d1d4b09
Show file tree
Hide file tree
Showing 36 changed files with 2,043 additions and 1,562 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ install:

script:
- nosetests -c .noserc autocertkit/tests/ acktools/
- tools/checkpep8.sh

after_success:
- coveralls
2 changes: 1 addition & 1 deletion acktools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import subprocess


def make_local_call(call):
"""Function wrapper for making a simple call to shell"""
process = subprocess.Popen(call, stdout=subprocess.PIPE)
Expand All @@ -10,4 +11,3 @@ def make_local_call(call):
return str(stdout).strip()
else:
raise Exception("Error: '%s' '%s'" % (stdout, stderr))

46 changes: 24 additions & 22 deletions acktools/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@
# Copyright (c) Citrix Systems Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.


import sys
import logging
import logging.handlers


def configure_log(name, path, to_stdout=True):
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)

try:
fileh = logging.FileHandler(path)
fileh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%%(asctime)-8s %s: %%(levelname)-8s %%(filename)s:%%(lineno)-10d %%(message)s' % name)
formatter = logging.Formatter(
'%%(asctime)-8s %s: %%(levelname)-8s %%(filename)s:%%(lineno)-10d %%(message)s' % name)
fileh.setFormatter(formatter)
log.addHandler(fileh)
except IOError, e:
Expand All @@ -60,6 +62,7 @@ def configure_log(name, path, to_stdout=True):

return log


def release_log(log):
if not log:
return
Expand All @@ -70,4 +73,3 @@ def release_log(log):
handler.flush()
log.removeHandler(handler)
handler.close()

9 changes: 5 additions & 4 deletions acktools/net/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import random


def generate_mac():
"""
This function will generate a random MAC.
The function generates a MAC with the Xensource, Inc. OUI '00:16:3E'.
Care should be taken to ensure duplicates are not used.
"""
mac = [ 0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
mac = [0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return ':'.join(map(lambda x: "%02x" % x, mac))
56 changes: 30 additions & 26 deletions acktools/net/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@
# Copyright (c) Citrix Systems Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

import acktools
from acktools import utils

ROUTE_CLI = "/sbin/route"


class Route(object):
"""Class for representing a route object"""

Expand Down Expand Up @@ -64,6 +65,7 @@ def get_record(self):
rec[key] = getattr(self, key)
return rec


class RouteTable(object):
"""Class for representing a route table which constitutes
a collection of route objects."""
Expand All @@ -72,7 +74,7 @@ def __init__(self, route_obj_list):
self.routes = route_obj_list

def get_routes(self, dest=None, mask=None, gw=None, iface=None):

matching_routes = []

for route in self.routes:
Expand All @@ -88,7 +90,7 @@ def get_routes(self, dest=None, mask=None, gw=None, iface=None):
# Route clearly matches the required fields
matching_routes.append(route)

return matching_routes
return matching_routes

def get_missing(self, rt):
"""Compare this route table with another passed in"""
Expand All @@ -101,21 +103,23 @@ def get_missing(self, rt):
missing.append(route)
return missing


def get_route_table():
return acktools.make_local_call([ROUTE_CLI,'-n'])
return acktools.make_local_call([ROUTE_CLI, '-n'])


def get_all_routes():
"""Return a list of route objects for all the routing
entries currently found in the kernel"""

output = get_route_table()
lines = output.split('\n')

if lines[0] != "Kernel IP routing table":
raise Exception("Error! Unexpected format: '%s'" % output)

# Join the table lines
route_table = "\n".join(lines[1:])
route_table = "\n".join(lines[1:])

# Parse the table to produce a list of recs
recs = utils.cli_table_to_recs(route_table)
Expand All @@ -128,7 +132,7 @@ def get_all_routes():
rec['Gateway'],
rec['Genmask'],
rec['Iface'],
)
)

route_list.append(route)

Expand Down
1 change: 1 addition & 0 deletions acktools/net/tests/net_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from acktools.net import generate_mac


class MACGeneratorTest(unittest.TestCase):
"""Test util methods for manipulating MACs"""

Expand Down
38 changes: 19 additions & 19 deletions acktools/net/tests/route_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
class RouteObjectTests(unittest.TestCase):

route_rec = {
'dest': '0.0.0.0',
'gw': '10.80.2.1',
'mask': '0.0.0.0',
'iface': 'eth1',
}
'dest': '0.0.0.0',
'gw': '10.80.2.1',
'mask': '0.0.0.0',
'iface': 'eth1',
}

def setUp(self):
self.route_obj = route.Route(**self.route_rec)

def test_get_dest(self):
self.assertEqual(self.route_obj.get_dest(),
self.assertEqual(self.route_obj.get_dest(),
self.route_rec['dest'])

def test_get_gw(self):
Expand All @@ -48,12 +48,12 @@ def validate_key(key):

class RouteTableTests(unittest.TestCase):

route_recs = [
{'dest': '0.0.0.0', 'gw': '10.80.2.1',
'mask': '0.0.0.0', 'iface': 'eth1'},
{'dest': '192.168.0.0', 'gw':'192.168.0.1',
'mask': '255.255.255.0', 'iface':'eth3'}
]
route_recs = [
{'dest': '0.0.0.0', 'gw': '10.80.2.1',
'mask': '0.0.0.0', 'iface': 'eth1'},
{'dest': '192.168.0.0', 'gw': '192.168.0.1',
'mask': '255.255.255.0', 'iface': 'eth3'}
]

def setUp(self):
route_list = []
Expand Down Expand Up @@ -91,9 +91,9 @@ def test_get_route(self):
self.assertEqual(route.get_gw(), rec['gw'])

def test_get_nonexistent_route(self):
routes = self.route_table.get_routes('192.145.2.5','255.255.255.0')
routes = self.route_table.get_routes('192.145.2.5', '255.255.255.0')
self.assertEqual(routes, [])
routes = self.route_table.get_routes('192.168.0.0','255.255.254.0')
routes = self.route_table.get_routes('192.168.0.0', '255.255.254.0')
self.assertEqual(routes, [])

def test_get_missing_routes(self):
Expand All @@ -108,12 +108,13 @@ def test_get_no_missing_routes(self):


ROUTE_TABLE = \
"""Kernel IP routing table
"""Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.80.2.1 0.0.0.0 UG 0 0 0 eth0
10.80.2.0 0.0.0.0 255.255.254.0 U 1 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0"""


class RouteMethodTests(unittest.TestCase):

@mock.patch('acktools.net.route.get_route_table', mock.Mock(return_value=ROUTE_TABLE))
Expand All @@ -126,7 +127,7 @@ def assert_about_obj(route_obj, gw, mask, iface):
self.assertEqual(route_obj.get_gw(), gw)
self.assertEqual(route_obj.get_mask(), mask)
self.assertEqual(route_obj.get_iface(), iface)

for route_obj in routes:
if route_obj.get_dest() == '0.0.0.0':
assert_about_obj(route_obj, '10.80.2.1', '0.0.0.0', 'eth0')
Expand All @@ -135,7 +136,7 @@ def assert_about_obj(route_obj, gw, mask, iface):
elif route_obj.get_dest() == '169.254.0.0':
assert_about_obj(route_obj, '0.0.0.0', '255.255.0.0', 'eth0')
else:
raise Exception("Error: route not in original list! " \
raise Exception("Error: route not in original list! "
"'%s'" % route)

@mock.patch('acktools.net.route.get_route_table', mock.Mock(return_value='Blah'))
Expand All @@ -145,4 +146,3 @@ def test_invalid_routing_table(self):

if __name__ == "__main__":
unittest.main()

16 changes: 8 additions & 8 deletions acktools/tests/acktools_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import subprocess
from mock import Mock


class MockProcess:

returncode = 0
stderr = None

Expand All @@ -16,7 +17,7 @@ def __init__(self, output, err=None):
self.returncode = 1
self.stderr = err

def stderr(self):
def stderr(self):
return self.stderr

def stdout(self):
Expand All @@ -29,18 +30,17 @@ def communicate(self):
class MakeLocalCallTests(unittest.TestCase):

def test_no_exceptions(self):
call = ['ls', '/tmp/']
acktools.make_local_call(call)
call = ['ls', '/tmp/']
acktools.make_local_call(call)

def test_expect_exception(self):
call = ['ls','/tmp']
call = ['ls', '/tmp']
real_popen = subprocess.Popen
setattr(subprocess, 'Popen', lambda *args, \
setattr(subprocess, 'Popen', lambda *args,
**kwargs: MockProcess('No such file!', 'No such file!'))
self.assertRaises(Exception, acktools.make_local_call, call)
setattr(subprocess, 'Popen', real_popen)



if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit d1d4b09

Please sign in to comment.