--- a/bin/pvl.dns-serial Thu May 16 01:03:29 2013 +0300
+++ b/bin/pvl.dns-serial Thu May 16 01:14:35 2013 +0300
@@ -5,158 +5,51 @@
Update zone serials.
"""
-__version__ = '0.0.1-dev'
+from pvl.dns import __version__
+import pvl.dns.serial
-import optparse
-import codecs
import os.path
-from datetime import datetime
-import logging
-log = logging.getLogger('main')
-
-# command-line options, global state
-options = None
+import pvl.args
+import optparse
+import logging; log = logging.getLogger('main')
def parse_options (argv) :
"""
Parse command-line arguments.
"""
-
+
prog = argv[0]
parser = optparse.OptionParser(
prog = prog,
- usage = '%prog: [options]',
+ usage = '%prog: [options] <serial-path> ...',
version = __version__,
# module docstring
description = __doc__,
)
-
- # logging
- general = optparse.OptionGroup(parser, "General Options")
+
+ # options
+ parser.add_option_group(pvl.args.parser(parser))
- general.add_option('-q', '--quiet', dest='loglevel', action='store_const', const=logging.ERROR, help="Less output")
- general.add_option('-v', '--verbose', dest='loglevel', action='store_const', const=logging.INFO, help="More output")
- general.add_option('-D', '--debug', dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
-
- parser.add_option_group(general)
+ parser.add_option('-n', '--noop', action='store_true',
+ help="Do not actually update serial")
# defaults
parser.set_defaults(
- loglevel = logging.WARN,
- expand = [],
+
)
# parse
options, args = parser.parse_args(argv[1:])
-
- # configure
- logging.basicConfig(
- format = prog + ': %(name)s: %(levelname)s %(funcName)s : %(message)s',
- level = options.loglevel,
- )
+
+ # apply
+ pvl.args.apply(options, prog)
return options, args
-# date fmt to use in serial
-DATE_FMT = '%Y%m%d'
-DATE_LEN = 8
-
-SERIAL_FMT = "{date:8}{count:02}"
-SERIAL_LEN = 10
-
-def format_serial (date, count) :
- return SERIAL_FMT.format(date=date.strftime(DATE_FMT), count=count)
-
-def next_count (value, date, count) :
- """
- Return serial with next count.
- """
-
- count += 1
-
- # check
- if count > 99 :
- serial = str(value + 1)
-
- log.warn("Serial count rollover: %s, %s; fallback -> %s", date, count, serial)
-
- return serial
-
- return format_serial(date, count)
-
-def update_serial (serial) :
- """
- Update new serial number into given file, based on date.
- """
-
- today = datetime.now().date()
-
- # handle
- if not serial :
- # fresh
- log.info("Setting initial serial: %s01", today)
-
- return format_serial(today, 1)
-
- elif len(serial) != SERIAL_LEN :
- log.warn("Invalid serial format: %s", serial)
-
- value = int(serial)
- serial = str(value + 1)
-
- log.info("Incrementing serial: %d -> %s", value, serial)
-
- return serial
-
- else :
- # parse
- value = int(serial)
-
- try :
- date = datetime.strptime(serial[:DATE_LEN], DATE_FMT).date()
- count = int(serial[DATE_LEN:])
-
- except ValueError, e :
- # invalid date/count format?
- log.warn("Unable to parse serial: %s: %s", serial, e)
-
- serial = str(value + 1)
-
- log.info("Incrementing serial: %d -> %s", value, serial)
-
- return serial
-
- log.debug("old serial=%s, value=%d, date=%s, count=%s", serial, value, date, count)
-
- # update
- if date < today :
- log.info("Updating to today: %s -> %s", date, today)
-
- # update date
- return format_serial(today, 1)
-
- elif date == today :
- # keep date, update count
- log.info("Updating today's count: %s, %s", date, count)
-
- # handle count rollover
- return next_count(value, date, count)
-
- elif date > today :
- # keep, update count
- serial = next_count(value, date, count)
-
- log.warn("Serial in future; incrementing count: %s, %s -> %s", date, count, serial)
-
- return serial
-
- else :
- raise Exception("Invalid serial: %s:%s", old_date, old_count)
-
-def process_serial (path) :
+def process_serial (options, path) :
"""
Read old serial, generate new one, and update file.
"""
@@ -164,28 +57,29 @@
# read
if os.path.exists(path) :
serial = open(path).read().strip()
- log.debug("current serial: %s", serial)
+ log.debug("%s: current: %s", path, serial)
else :
- log.warn("Given .serial does not yet exist: %s", path)
+ log.warn("%s: Given .serial does not yet exist", path)
serial = None
# update
- serial = update_serial(serial)
-
- # write
- open(path, 'w').write(serial + '\n')
+ serial = pvl.dns.serial.update_serial(serial)
+
+ if options.noop :
+ log.warn("%s: %s", path, serial)
+ else :
+ # write
+ open(path, 'w').write(serial + '\n')
return serial
def main (argv) :
- global options
-
options, args = parse_options(argv)
# serial files to update
for path in args :
- process_serial(path)
+ process_serial(options, path)
return 0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/dns/__init__.py Thu May 16 01:14:35 2013 +0300
@@ -0,0 +1,2 @@
+
+__version__ = '0.1'
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pvl/dns/serial.py Thu May 16 01:14:35 2013 +0300
@@ -0,0 +1,105 @@
+"""
+ Generating zone serials.
+"""
+
+import datetime
+
+import logging; log = logging.getLogger('pvl.dns.serial')
+
+# date fmt to use in serial
+DATE_FMT = '%Y%m%d'
+DATE_LEN = 8
+
+SERIAL_FMT = "{date:8}{count:02}"
+SERIAL_LEN = 10
+
+def format_serial (date, count) :
+ return SERIAL_FMT.format(date=date.strftime(DATE_FMT), count=count)
+
+def next_count (value, date, count) :
+ """
+ Return serial with next count.
+ """
+
+ count += 1
+
+ # check
+ if count > 99 :
+ serial = str(value + 1)
+
+ log.warn("Serial count rollover: %s, %s; fallback -> %s", date, count, serial)
+
+ return serial
+
+ return format_serial(date, count)
+
+def update_serial (serial) :
+ """
+ Parse given serial number (string), returning an updated serial, based on date.
+ """
+
+ today = datetime.date.today()
+
+ # handle
+ if not serial :
+ # fresh
+ log.info("Setting initial serial: %s01", today)
+
+ return format_serial(today, 1)
+
+ elif len(serial) != SERIAL_LEN :
+ log.warn("Invalid serial format: %s", serial)
+
+ value = int(serial)
+ serial = str(value + 1)
+
+ log.info("Incrementing serial: %d -> %s", value, serial)
+
+ return serial
+
+ else :
+ # parse
+ value = int(serial)
+
+ try :
+ date = datetime.datetime.strptime(serial[:DATE_LEN], DATE_FMT).date()
+ count = int(serial[DATE_LEN:])
+
+ except ValueError, e :
+ # invalid date/count format?
+ log.warn("Unable to parse serial: %s: %s", serial, e)
+
+ serial = str(value + 1)
+
+ log.info("Incrementing serial: %d -> %s", value, serial)
+
+ return serial
+
+ log.debug("old serial=%s, value=%d, date=%s, count=%s", serial, value, date, count)
+
+ # update
+ if date < today :
+ log.info("Updating to today: %s -> %s", date, today)
+
+ # update date
+ return format_serial(today, 1)
+
+ elif date == today :
+ # keep date, update count
+ log.info("Updating today's count: %s, %s", date, count)
+
+ # handle count rollover
+ return next_count(value, date, count)
+
+ elif date > today :
+ # keep, update count
+ serial = next_count(value, date, count)
+
+ log.warn("Serial in future; incrementing count: %s, %s -> %s", date, count, serial)
+
+ return serial
+
+ else :
+ raise Exception("Invalid serial: %s:%s", old_date, old_count)
+
+
--- a/setup.py Thu May 16 01:03:29 2013 +0300
+++ b/setup.py Thu May 16 01:14:35 2013 +0300
@@ -39,12 +39,13 @@
'pvl',
'pvl.web',
'pvl.dhcp',
+ 'pvl.dns',
'pvl.rrd',
'pvl.verkko',
],
# bin
- scripts = globs('bin/pvl.dhcp-*', 'bin/pvl.rrd-*', 'bin/pvl.verkko-*'),
+ scripts = globs('bin/pvl.dhcp-*', 'bin/pvl.rrd-*', 'bin/pvl.verkko-*', 'bin/pvl.dns-*'),
# etc, static
data_files = [