[svn] commit: r909 - in /experiments/each-zoneload: loadzone.py master.py

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Feb 22 06:33:41 UTC 2010


Author: each
Date: Mon Feb 22 06:33:41 2010
New Revision: 909

Log:
bugfixes, general interface improvement

Modified:
    experiments/each-zoneload/loadzone.py
    experiments/each-zoneload/master.py

Modified: experiments/each-zoneload/loadzone.py
==============================================================================
--- experiments/each-zoneload/loadzone.py (original)
+++ experiments/each-zoneload/loadzone.py Mon Feb 22 06:33:41 2010
@@ -1,5 +1,6 @@
 #! /usr/bin/python3
-import sqlite3, sys, string, re, random
+import sqlite3, sys, string, re, random, getopt
+import master
 
 #########################################################################
 # define exceptions
@@ -79,30 +80,6 @@
         return ''
 
 #########################################################################
-# getRR:
-#   converts a line from a zone file into data suitable for
-#   insertion into the database.
-# input:
-#   line - a line from the zone file. (must be a complete resource
-#          record, as generated by named-compilezone.)
-# returns:
-#   name, ttl, rdclass, rdtype, sigtype, rdata
-#########################################################################
-def getRR(line):
-    global decomment;
-    line = decomment.sub('', line)
-    line = line.split()
-    name = line.pop(0).strip()
-    ttl = int(line.pop(0).strip())
-    rdclass = line.pop(0).strip()
-    rdtype = line.pop(0).strip()
-    rdata = ' '.join(line).strip()
-    sigtype = ''
-    if rdtype == 'RRSIG':
-        sigtype == line[0]
-    return name, ttl, rdclass, rdtype, sigtype, rdata
-
-#########################################################################
 # reverse_name:
 #   reverse the labels of a DNS name.  (for example,
 #   "bind10.isc.org." would become "org.isc.bind10.")
@@ -121,7 +98,7 @@
 # usage: print usage note and exit
 #########################################################################
 def usage():
-    print("Usage: " + sys.argv[0] + " <zone name> <zone file> [database]")
+    print("Usage: %s [-d <database>] <zone> <file>" % sys.argv[0])
     exit(1)
 
 #########################################################################
@@ -129,17 +106,25 @@
 #########################################################################
 def main():
     try:
-        zone = sys.argv[1]
-        if zone[-1] != '.':
-            zone += '.'
-        zonefile = sys.argv[2]
+        opts, args = getopt.getopt(sys.argv[1:], "d:h", ["help"])
+    except getopt.GetoptError as e:
+        print(str(e))
+        usage()
+        exit(2)
+
+    dbfile = '/tmp/zone.sqlite3'
+    for o, a in opts:
+        if o == "-d":
+            dbfile = a
+        elif o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        else:
+            assert False, "unhandled option"
+    try:
+        zone, zonefile = args
     except:
         usage()
-
-    try:
-        dbfile = sys.argv[3]
-    except:
-        dbfile = '/tmp/zone.sqlite3'
 
     try:
         conn, cur = open_db(dbfile)
@@ -147,11 +132,7 @@
         print("Couldn't open database: " + e.args[0]);
         exit(1)
 
-    try:
-        data = open(zonefile).read().splitlines()
-    except:
-        print("Unable to open zone file " + zonefile + ": ", sys.exc_info()[0])
-        exit(1)
+    zonedata = master.parse(zonefile)
 
     old_zone_id = findzone(zone, cur)
 
@@ -159,8 +140,11 @@
     cur.execute("INSERT INTO zones (name, rdclass) VALUES (?, 'IN')", [temp])
     new_zone_id = cur.lastrowid
 
-    for line in data:
-        name, ttl, rdclass, rdtype, sigtype, rdata = getRR(line)
+    for name, ttl, rdclass, rdtype, rdata in zonedata:
+        sigtype = ''
+        if rdtype.lower == 'rrsig':
+            sigtype = rdata.split()[0]
+
         if sigtype:
             cur.execute("""INSERT INTO records
                            (zone_id, name, rname, ttl, rdtype, sigtype, rdata)

Modified: experiments/each-zoneload/master.py
==============================================================================
--- experiments/each-zoneload/master.py (original)
+++ experiments/each-zoneload/master.py Mon Feb 22 06:33:41 2010
@@ -1,5 +1,5 @@
 #!/usr/bin/python3
-import re, string;
+import sys, re, string;
 
 #########################################################################
 # define exceptions
@@ -14,8 +14,6 @@
 # global variables
 #########################################################################
 maxttl = 0x7fffffff;
-defttl = -1
-origin='.'
 defclass = 'IN'
 
 #########################################################################
@@ -231,8 +229,6 @@
 #   a record to parse, and the most recent name found in prior records
 # returns:
 #   empty list if parse failed, else name, ttl, class, type, rdata
-# throws:
-#   MasterFileError
 #########################################################################
 def four(record, curname):
     ret = ''
@@ -241,7 +237,7 @@
         return ret
     if istype(list[3]):
         if isclass(list[2]) and isttl(list[1]) and isname(list[0]):
-            name, ttl, rrclass, rrtype = list[0:3]
+            name, ttl, rrclass, rrtype = list[0:4]
             rdata = ' '.join(list[4:])
             ret = name, ttl, rrclass, rrtype, rdata
     return ret
@@ -253,8 +249,6 @@
 #   a record to parse, and the most recent name found in prior records
 # returns:
 #   empty list if parse failed, else name, ttl, class, type, rdata
-# throws:
-#   MasterFileError
 #########################################################################
 def three(record, curname):
     global defttl, defclass
@@ -262,7 +256,7 @@
     list = record.split()
     if len(list) <= 3:
         return ret
-    if istype(list[2]):
+    if istype(list[2]) and not istype(list[1]):
         if isclass(list[1]) and not isttl(list[0]) and isname(list[0]):
             rrclass = list[1]
             ttl = defttl
@@ -273,10 +267,10 @@
             name = list[0]
         elif curname and isclass(list[1]) and isttl(list[0]):
             rrclass = defclass
-            ttl = parse_ttl(list[1])
+            ttl = parse_ttl(list[0])
             name = curname
         else:
-            raise MasterFileError("Cannot parse RR: " + record)
+            return ret
 
         rrtype = list[2]
         rdata = ' '.join(list[3:])
@@ -285,7 +279,7 @@
 
 #########################################################################
 # two: try parsing on the assumption that the RR type is specified in
-# field 2, and field 1 is name, with ttl and class omitted.
+# field 2, and field 1 is either name or ttl
 # input:
 #   a record to parse, and the most recent name found in prior records
 # returns:
@@ -299,29 +293,43 @@
     list = record.split()
     if len(list) <= 2:
         return ret
-    if istype(list[1]):
-        if isname(list[0]):
+    if istype(list[1]) and not istype(list[0]):
+        if isttl(list[0]):
+            ttl = parse_ttl(list[0])
+            name = curname
+        elif isname(list[0]):
             name = list[0]
+            ttl = defttl
         else:
             raise MasterFileError("Cannot parse RR: " + record)
 
-        ttl = defttl
         rrclass = defclass
         rrtype = list[1]
         rdata = ' '.join(list[2:])
         ret = name, ttl, rrclass, rrtype, rdata
     return ret
 
+
+#########################################################################
+# reset: reset the state of the master file parser; use when parsing
+# more than one file
+#########################################################################
+def reset():
+    global defttl, origin
+    defttl = -1
+    origin='.'
+
 #########################################################################
 # parse: parse a zone master file and return it as an array of
 # tuples
 #########################################################################
 def parse(file):
-    global defttl, defclass
-    data = open(file).read().splitlines()
+    global defttl, origin, defclass
+
     zone = []
     name = ''
 
+    data = open(file).read().splitlines()
     for record in records(data):
         if directive(record):
             continue;
@@ -366,6 +374,9 @@
             if rdata[-1] != '.':
                 rdata += '.' + origin
 
+        if (ttl == -1):
+            raise MasterFileError("No TTL specified; zone rejected")
+
         zone.append((name, ttl, rrclass, rrtype, rdata))
 
     return zone
@@ -376,7 +387,11 @@
 #########################################################################
 def main():
     print ('---------------------')
-    zone = parse('testfile')
+    try:
+        file = sys.argv[1]
+    except:
+        file = 'testfile'
+    zone = parse(file)
     for name, ttl, rrclass, rrtype, rdata in zone:
         print ('name: ' + name)
         print ('ttl: ' + str(ttl))
@@ -385,5 +400,8 @@
         print ('rdata: ' + rdata)
         print ('---------------------')
 
+# initialize
+reset()
+
 if __name__ == "__main__":
     main()




More information about the bind10-changes mailing list