]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Finishing sorting list of domains
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 8 Nov 2017 10:59:52 +0000 (11:59 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 8 Nov 2017 10:59:52 +0000 (11:59 +0100)
pp_lib/common.py
pp_lib/pdns_list_zones.py
test/test_compare_fqdn.py

index cfcaa3e5524f1775e93e75b650209591c0415741..de32918187043478be6a8a41909e59417d5f88de 100644 (file)
@@ -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):
index ad8f41fea4f28e01159cbdd32695c2c172d7361f..be34b3129d096727ff6c34d6ce6f480c5f6d7e54 100644 (file)
@@ -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))
 
 # =============================================================================
index b30dc52aa6423be551be375c513a4eca45b49c3c..d1bd9706454ba94c8225b52ac5e727324f8428e7 100755 (executable)
@@ -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: