From c32aa49a7d3eb9b7c7f0a2ac043a3fd47bae4395 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 8 Nov 2017 11:59:52 +0100 Subject: [PATCH] Finishing sorting list of domains --- pp_lib/common.py | 36 +++++++++++++++++++++++++++++++----- pp_lib/pdns_list_zones.py | 8 +++++--- test/test_compare_fqdn.py | 9 +++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pp_lib/common.py b/pp_lib/common.py index cfcaa3e..de32918 100644 --- a/pp_lib/common.py +++ b/pp_lib/common.py @@ -21,7 +21,7 @@ import six # Own modules -__version__ = '0.5.1' +__version__ = '0.5.2' LOG = logging.getLogger(__name__) @@ -34,6 +34,9 @@ RE_TO_BOOL_FALSE = re.compile(PAT_TO_BOOL_FALSE) RE_DOT = re.compile(r'\.') RE_DOT_AT_END = re.compile(r'(\.)*$') +RE_DECIMAL = re.compile(r'^\d+$') +RE_IPV4_PTR = re.compile(r'\.in-addr\.arpa\.$', re.IGNORECASE) +RE_IPV6_PTR = re.compile(r'\.ip6\.arpa\.$', re.IGNORECASE) # ============================================================================= @@ -292,6 +295,8 @@ def caller_search_path(): # ============================================================================= def compare_fqdn(x, y): + #LOG.debug("Comparing {!r} <=> {!r}.".format(x, y)) + # First check for None values if x is None and y is None: return 0 @@ -316,6 +321,19 @@ def compare_fqdn(x, y): if xs == ys: return 0 + if RE_IPV4_PTR.search(xs): + if not RE_IPV4_PTR.search(ys): + return -1 + elif RE_IPV4_PTR.search(ys): + if not RE_IPV4_PTR.search(xs): + return 1 + elif RE_IPV6_PTR.search(xs): + if not RE_IPV6_PTR.search(ys): + return -1 + elif RE_IPV6_PTR.search(ys): + if not RE_IPV6_PTR.search(xs): + return 1 + xa = RE_DOT.split(xs) xa.reverse() xa.pop(0) @@ -328,10 +346,18 @@ def compare_fqdn(x, y): while nr_tokens > 0: token_x = xa.pop(0) token_y = ya.pop(0) - if token_x < token_y: - return -1 - elif token_x > token_y: - return 1 + if RE_DECIMAL.match(token_x) and RE_DECIMAL.match(token_y): + num_x = int(token_x) + num_y = int(token_y) + if num_x < num_y: + return -1 + elif num_x > num_y: + return 1 + else: + if token_x < token_y: + return -1 + elif token_x > token_y: + return 1 nr_tokens -= 1 if len(xa): diff --git a/pp_lib/pdns_list_zones.py b/pp_lib/pdns_list_zones.py index ad8f41f..be34b31 100644 --- a/pp_lib/pdns_list_zones.py +++ b/pp_lib/pdns_list_zones.py @@ -14,13 +14,15 @@ import logging import logging.config import textwrap +from functools import cmp_to_key + # Own modules -from .common import pp +from .common import pp, compare_fqdn from .pdns_app import PpPDNSAppError, PpPDNSApplication from .pdns_zone import PdnsApiZone -__version__ = '0.2.1' +__version__ = '0.2.2' LOG = logging.getLogger(__name__) @@ -77,7 +79,7 @@ class PpPDNSListZonesApp(PpPDNSApplication): print(line) print('-' * len(line)) - for zone in zone_list: + for zone in sorted(zone_list, key=lambda x: cmp_to_key(compare_fqdn)(x.name)): print(zone.get_line(len_zone)) # ============================================================================= diff --git a/test/test_compare_fqdn.py b/test/test_compare_fqdn.py index b30dc52..d1bd970 100755 --- a/test/test_compare_fqdn.py +++ b/test/test_compare_fqdn.py @@ -77,6 +77,15 @@ class CompareFqdnTest(AdminToolsTestcase): ('aa.de.', 'a.de.', 1), ('a.a.de.', 'a.de.', 1), ('a.de.', 'b.de', -1), + ('10.in-addr.arpa.', 'a.ai.', -1), + ('10.in-addr.arpa.', 'a.az.', -1), + ('10.in-addr.arpa.', 'f.ip6.arpa.', -1), + ('10.in-addr.arpa.', '11.in-addr.arpa.', -1), + ('200.in-addr.arpa.', '10.in-addr.arpa.', 1), + ('100.in-addr.arpa.', '11.in-addr.arpa.', 1), + ('f.ip6.arpa.', 'a.ai.', -1), + ('f.ip6.arpa.', 'a.az.', -1), + ('pixelpark.ag.', '1.1.10.in-addr.arpa.', 1), ) for case in testcases: -- 2.39.5