--- a/bin/pvl.dns-hosts Mon Dec 16 16:51:38 2013 +0200
+++ b/bin/pvl.dns-hosts Mon Dec 16 17:11:17 2013 +0200
@@ -62,8 +62,8 @@
parser.add_option('--output-hosts', metavar='FILE',
help="Output hosts file")
- parser.add_option('--output-ip', metavar='PREFIX',
- help="Output hosts by ip prefix")
+ parser.add_option('--output-prefix', metavar='PREFIX',
+ help="Select hosts by ip prefix")
# defaults
parser.set_defaults(
@@ -91,7 +91,7 @@
elif rr.type == 'A' :
ip, = rr.data
- yield rr.name, 'ip', ip
+ yield rr.name, 'ip', ipaddr.IPAddress(ip)
if rr.comment :
yield rr.name, 'comment', rr.comment
@@ -378,18 +378,28 @@
return hosts.iteritems()
-def select_hosts_ip (options, hosts, network) :
+def process_export_hosts (options, hosts) :
+ if options.output_prefix :
+ prefix = ipaddr.IPNetwork(options.output_prefix)
+ else :
+ prefix = None
+
for host, fields in hosts :
ip = fields.get('ip')
-
- if not ip :
- continue
+
+ # sort by IP
+ if ip :
+ sort = ip = ip[0]
+ else :
+ # fake, to sort correctly
+ sort = ipaddr.IPAddress(0)
+
+ # select
+ if prefix:
+ if not (ip and ip in prefix) :
+ continue
- ip = ipaddr.IPAddress(ip[0])
-
- if ip in network :
- yield ip, host, fields
-
+ yield sort, host, fields
def export_hosts (options, hosts) :
"""
@@ -398,11 +408,8 @@
file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
- if options.output_ip :
- prefix = ipaddr.IPNetwork(options.output_ip)
-
- # filter + sort
- hosts = [(host, fields) for ip, host, fields in sorted(select_hosts_ip(options, hosts, prefix))]
+ # filter + sort
+ hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
for host, fields in hosts :
for comment in fields.get('comment', ()) :