terom@233: #!/usr/bin/env python terom@233: terom@233: """ terom@233: Update zone serials. terom@233: """ terom@233: terom@234: from pvl.dns import __version__ terom@234: import pvl.dns.serial terom@233: terom@233: import os.path terom@233: terom@234: import pvl.args terom@234: import optparse terom@234: import logging; log = logging.getLogger('main') terom@233: terom@233: def parse_options (argv) : terom@233: """ terom@233: Parse command-line arguments. terom@233: """ terom@234: terom@233: prog = argv[0] terom@233: terom@233: parser = optparse.OptionParser( terom@233: prog = prog, terom@234: usage = '%prog: [options] ...', terom@233: version = __version__, terom@233: terom@233: # module docstring terom@233: description = __doc__, terom@233: ) terom@234: terom@234: # options terom@234: parser.add_option_group(pvl.args.parser(parser)) terom@233: terom@234: parser.add_option('-n', '--noop', action='store_true', terom@234: help="Do not actually update serial") terom@233: terom@233: # defaults terom@233: parser.set_defaults( terom@234: terom@233: ) terom@233: terom@233: # parse terom@233: options, args = parser.parse_args(argv[1:]) terom@234: terom@234: # apply terom@234: pvl.args.apply(options, prog) terom@233: terom@233: return options, args terom@233: terom@234: def process_serial (options, path) : terom@233: """ terom@233: Read old serial, generate new one, and update file. terom@233: """ terom@233: terom@233: # read terom@233: if os.path.exists(path) : terom@233: serial = open(path).read().strip() terom@234: log.debug("%s: current: %s", path, serial) terom@233: terom@233: else : terom@234: log.warn("%s: Given .serial does not yet exist", path) terom@233: serial = None terom@233: terom@233: # update terom@234: serial = pvl.dns.serial.update_serial(serial) terom@234: terom@234: if options.noop : terom@234: log.warn("%s: %s", path, serial) terom@234: else : terom@234: # write terom@234: open(path, 'w').write(serial + '\n') terom@233: terom@233: return serial terom@233: terom@233: def main (argv) : terom@233: options, args = parse_options(argv) terom@233: terom@233: # serial files to update terom@233: for path in args : terom@234: process_serial(options, path) terom@233: terom@233: return 0 terom@233: terom@233: if __name__ == '__main__': terom@233: import sys terom@233: terom@233: sys.exit(main(sys.argv))