diff options
author | Rob Austein <sra@hactrn.net> | 2012-11-15 22:13:53 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2012-11-15 22:13:53 +0000 |
commit | 756fdbe0d7eda1e98663c62bb3e57f2c18e88ee1 (patch) | |
tree | c0af81a5987e9859d12a0323d0d240f58d8b2f36 /rpkid/rpki/gui/app | |
parent | 713507be1695d8f2f278ab925d58defc58eff2aa (diff) | |
parent | 11f3b8df179a16ebe1446dab620522ac97e3c327 (diff) |
Merge tk274 performance work back to trunk. Closes #274.
svn path=/trunk/; revision=4878
Diffstat (limited to 'rpkid/rpki/gui/app')
-rw-r--r-- | rpkid/rpki/gui/app/forms.py | 10 | ||||
-rwxr-xr-x | rpkid/rpki/gui/app/range_list.py | 90 | ||||
-rw-r--r-- | rpkid/rpki/gui/app/views.py | 2 |
3 files changed, 53 insertions, 49 deletions
diff --git a/rpkid/rpki/gui/app/forms.py b/rpkid/rpki/gui/app/forms.py index 99a5cc5e..2dfbb6a5 100644 --- a/rpkid/rpki/gui/app/forms.py +++ b/rpkid/rpki/gui/app/forms.py @@ -23,7 +23,7 @@ from rpki.resource_set import (resource_range_as, resource_range_ipv4, from rpki.gui.app import models from rpki.exceptions import BadIPResource from rpki.gui.app.glue import str_to_resource_range -import rpki.ipaddrs +from rpki.POW import IPAddress class AddConfForm(forms.Form): @@ -190,11 +190,7 @@ class ROARequest(forms.Form): """ prefix = self.cleaned_data.get('prefix') if '/' not in prefix: - p = rpki.ipaddrs.parse(prefix) - - # rpki.ipaddrs.parse doesn't return a v?addr object, so can't - # introspect p.bits - bits = 32 if ':' not in prefix else 64 + p = IPAddress(prefix) # determine the first nonzero bit starting from the lsb and # subtract from the address size to find the closest classful @@ -203,7 +199,7 @@ class ROARequest(forms.Form): while (p != 0) and (p & 1) == 0: prefixlen = prefixlen + 1 p = p >> 1 - mask = bits - (8 * (prefixlen / 8)) + mask = p.bits - (8 * (prefixlen / 8)) prefix = prefix + '/' + str(mask) return str_to_resource_range(prefix) diff --git a/rpkid/rpki/gui/app/range_list.py b/rpkid/rpki/gui/app/range_list.py index fcfcfc24..21fd1f29 100755 --- a/rpkid/rpki/gui/app/range_list.py +++ b/rpkid/rpki/gui/app/range_list.py @@ -17,6 +17,7 @@ __version__ = '$Id$' import bisect import unittest + class RangeList(list): """A sorted list of ranges, which automatically merges adjacent ranges. @@ -36,35 +37,39 @@ class RangeList(list): # upper bound j = bisect.bisect_right(keys, v.max, lo=i) - # if the max value for the previous item is greater than v.min, include the previous item in the range to replace - # and use its min value. also include the previous item if the max value is 1 less than the min value for the - # inserted item - if i > 0 and self[i-1].max >= v.min - 1: + # if the max value for the previous item is greater than v.min, include + # the previous item in the range to replace and use its min value. + # also include the previous item if the max value is 1 less than the + # min value for the inserted item + if i > 0 and self[i - 1].max >= v.min - 1: i = i - 1 vmin = self[i].min else: vmin = v.min - # if the max value for the previous item is greater than the max value for the new item, use the previous item's max - if j > 0 and self[j-1].max > v.max: - vmax = self[j-1].max + # if the max value for the previous item is greater than the max value + # for the new item, use the previous item's max + if j > 0 and self[j - 1].max > v.max: + vmax = self[j - 1].max else: vmax = v.max - # if the max value for the new item is 1 less than the min value for the next item, combine into a single item - if j < len(self) and vmax+1 == self[j].min: + # if the max value for the new item is 1 less than the min value for + # the next item, combine into a single item + if j < len(self) and vmax + 1 == self[j].min: vmax = self[j].max - j = j+1 + j = j + 1 # replace the range with a new object covering the entire range - self[i:j] = [v.__class__(min=vmin, max=vmax)] + self[i:j] = [v.__class__(vmin, vmax)] def extend(self, args): for x in args: self.append(x) def difference(self, other): - """Return a RangeList object which contains ranges in this object which are not in "other".""" + """Return a RangeList object which contains ranges in this object which + are not in "other".""" it = iter(other) try: @@ -85,27 +90,30 @@ class RangeList(list): try: while xmin <= x.max: if xmin < cur.min: - r.append(x.__class__(min=V(xmin), - max=V(min(x.max,cur.min-1)))) - xmin = cur.max+1 + r.append(x.__class__(V(xmin), + V(min(x.max, cur.min - 1)))) + xmin = cur.max + 1 elif xmin == cur.min: - xmin = cur.max+1 - else: # xmin > cur.min + xmin = cur.max + 1 + else: # xmin > cur.min if xmin <= cur.max: - xmin = cur.max+1 - else: # xmin > cur.max + xmin = cur.max + 1 + else: # xmin > cur.max cur = it.next() except StopIteration: - r.append(x.__class__(min=V(xmin), max=x.max)) + r.append(x.__class__(V(xmin), x.max)) return r + class TestRangeList(unittest.TestCase): class MinMax(object): - def __init__(self, min, max): - self.min = min - self.max = max + datum_type = int + + def __init__(self, range_min, range_max): + self.min = range_min + self.max = range_max def __str__(self): return '(%d, %d)' % (self.min, self.max) @@ -117,12 +125,12 @@ class TestRangeList(unittest.TestCase): return self.min == other.min and self.max == other.max def setUp(self): - self.v1 = TestRangeList.MinMax(1,2) - self.v2 = TestRangeList.MinMax(4,5) - self.v3 = TestRangeList.MinMax(7,8) - self.v4 = TestRangeList.MinMax(3,4) - self.v5 = TestRangeList.MinMax(2,3) - self.v6 = TestRangeList.MinMax(1,10) + self.v1 = TestRangeList.MinMax(1, 2) + self.v2 = TestRangeList.MinMax(4, 5) + self.v3 = TestRangeList.MinMax(7, 8) + self.v4 = TestRangeList.MinMax(3, 4) + self.v5 = TestRangeList.MinMax(2, 3) + self.v6 = TestRangeList.MinMax(1, 10) def test_empty_append(self): s = RangeList() @@ -161,14 +169,14 @@ class TestRangeList(unittest.TestCase): s.append(self.v1) s.append(self.v5) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,3)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 3)) def test_combine_range(self): s = RangeList() s.append(self.v1) s.append(self.v4) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,4)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 4)) def test_append_subset(self): s = RangeList() @@ -189,7 +197,7 @@ class TestRangeList(unittest.TestCase): s.append(self.v4) s.append(self.v1) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,4)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 4)) def test_append_aggregate(self): s = RangeList() @@ -213,31 +221,31 @@ class TestRangeList(unittest.TestCase): def test_diff_middle(self): s1 = RangeList([self.v6]) s2 = RangeList([self.v3]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,6), TestRangeList.MinMax(9, 10)])) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1, 6), TestRangeList.MinMax(9, 10)])) def test_diff_overlap(self): s1 = RangeList([self.v2]) s2 = RangeList([self.v4]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(5,5)])) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(5, 5)])) def test_diff_overlap2(self): s1 = RangeList([self.v2]) s2 = RangeList([self.v4]) - self.assertEqual(s2.difference(s1), RangeList([TestRangeList.MinMax(3,3)])) + self.assertEqual(s2.difference(s1), RangeList([TestRangeList.MinMax(3, 3)])) def test_diff_multi(self): - s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(4,5)]) - s2 = RangeList([TestRangeList.MinMax(4,4)]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(5,5)])) + s1 = RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(4, 5)]) + s2 = RangeList([TestRangeList.MinMax(4, 4)]) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(5, 5)])) def test_diff_multi_overlap(self): - s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(3,4)]) - s2 = RangeList([TestRangeList.MinMax(2,3)]) + s1 = RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(3, 4)]) + s2 = RangeList([TestRangeList.MinMax(2, 3)]) self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,1), TestRangeList.MinMax(4,4)])) def test_diff_multi_overlap2(self): s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(3,4), TestRangeList.MinMax(6,7)]) - s2 = RangeList([TestRangeList.MinMax(2,3), TestRangeList.MinMax(6,6)]) + s2 = RangeList([TestRangeList.MinMax(2, 3), TestRangeList.MinMax(6, 6)]) self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,1), TestRangeList.MinMax(4,4), TestRangeList.MinMax(7,7)])) if __name__ == '__main__': diff --git a/rpkid/rpki/gui/app/views.py b/rpkid/rpki/gui/app/views.py index f35447a9..7969159c 100644 --- a/rpkid/rpki/gui/app/views.py +++ b/rpkid/rpki/gui/app/views.py @@ -433,7 +433,7 @@ def child_edit(request, pk): if request.method == 'POST': form = form_class(request.POST, request.FILES) if form.is_valid(): - child.valid_until = sundial.datetime.fromdatetime(form.cleaned_data.get('valid_until')) + child.valid_until = sundial.datetime.from_datetime(form.cleaned_data.get('valid_until')) child.save() # remove AS & prefixes that are not selected in the form models.ChildASN.objects.filter(child=child).exclude(pk__in=form.cleaned_data.get('as_ranges')).delete() |