Commit 9b515aae authored by ale's avatar ale

add a command-line driver

parent 61efd202
......@@ -9,7 +9,7 @@ setup(
author='ale',
author_email='ale@incal.net',
url='http://git.autistici.org/ale/zonetool',
install_requires=[],
install_requires=['PyYAML'],
test_requires=[],
setup_requires=[],
zip_safe=True,
......@@ -17,7 +17,7 @@ setup(
package_data={},
entry_points={
'console_scripts': [
'zoneutil = zonetool.zone:main',
'zoneutil = zonetool.main:main',
],
},
)
......
import optparse
import os
import sys
import yaml
from zonetool import zone
def main():
parser = optparse.OptionParser(
usage='%prog [<OPTIONS>] <PATH>')
parser.add_option(
'-c', '--config', dest='config', metavar='FILE',
help='Load global configuration from FILE (in YAML format)')
parser.add_option(
'-o', '--output-dir', dest='output_dir',
default='.', metavar='DIR',
help='Output directory (default: current dir)')
parser.add_option(
'-n', '--dry-run', dest='dry_run', action='store_true',
help='Do not actually write zone files')
opts, args = parser.parse_args()
if len(args) != 1:
parser.error('Wrong number of arguments')
config = dict(os.environ)
if opts.config:
with open(opts.config, 'r') as fd:
config.update(yaml.safe_load(fd))
zp = zone.ZoneParser(config)
zp.merge(zone.readyaml(zone.walk(args[0])))
zones = zp.render()
if not zones:
print >>sys.stderr, 'No zones found.'
for zone_name, zone_data in zones.iteritems():
print zone_name
if opts.dry_run:
continue
dst = os.path.join(opts.output_dir, zone_name)
with open(dst, 'w') as fd:
fd.write(zone_data)
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment