Skip to content

Commit

Permalink
Merge pull request #4 from afpd/master
Browse files Browse the repository at this point in the history
added truncate
  • Loading branch information
job committed Dec 1, 2017
2 parents 2b5de94 + 91dbeeb commit ddda8c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions aggregate6/aggregate6.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from __future__ import unicode_literals

from builtins import str as text
from ipaddress import ip_network
from ipaddress import ip_network, ip_interface

import aggregate6
import radix
Expand Down Expand Up @@ -111,6 +111,8 @@ def parse_args(args):
""", formatter_class=argparse.RawTextHelpFormatter)
p.add_argument('-v', dest='version', action='store_true',
help="Display aggregate6 version")
p.add_argument('-t', dest='truncate', action='store_true',
help="truncate IP/mask to network/mask")
afi_group = p.add_mutually_exclusive_group()
afi_group.add_argument('-4', dest='ipv4_only', action='store_true',
default=False,
Expand Down Expand Up @@ -139,7 +141,10 @@ def main():
continue
for elem in line.strip().split():
try:
prefix_obj = ip_network(text(elem.strip()))
if args.truncate:
prefix_obj = ip_interface(text(elem.strip())).network
else:
prefix_obj = ip_network(text(elem.strip()))
prefix = text(prefix_obj)
except ValueError:
sys.stderr.write("ERROR: '%s' is not a valid IP network, \
Expand Down
8 changes: 8 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def test_12__use_space_in_stdin(self):
agg_main()
self.assertEqual(sys.stdout.getvalue(), '2001:db8::/32\n')

def test_13_truncate(self):
stub_stdin(self, '2001:db8::1/32 2001:db9::1/32\n10.5.5.5/8\n')
stub_stdouts(self)
with patch.object(sys, 'argv', ["prog.py", "-t"]):
agg_main()
self.assertEqual(sys.stdout.getvalue(), '10.0.0.0/8\n2001:db8::/31\n')



class StringIO(io.StringIO):
"""
Expand Down

0 comments on commit ddda8c9

Please sign in to comment.