--- a/bin/pvl.dns-hosts Mon Dec 16 11:41:59 2013 +0200
+++ b/bin/pvl.dns-hosts Mon Dec 16 11:48:04 2013 +0200
@@ -68,63 +68,6 @@
return options, args
-ZONE_COMMENTS = (
- re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'),
- re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'),
- re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'),
- re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'),
- re.compile(r'(?P<owner>.+)'),
-)
-
-ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>')
-
-def process_zone_comment (options, hostname, comment) :
- """
- Attempt to parse a host comment field... :D
- """
-
- yield 'comment', comment
-
- for regex in ZONE_COMMENTS :
- match = regex.match(comment)
-
- if match :
- break
- else :
- log.warn("%s: unparsed comment: %s", hostname, comment)
- return
-
- matches = match.groupdict()
- owner = matches.pop('owner', None)
-
- if owner :
- mail_match = ZONE_OWNER_MAIL.match(owner)
-
- if mail_match :
- mail_matches = mail_match.groupdict()
-
- owner = mail_matches['owner']
- yield 'comment-mail', mail_matches['mail']
- else :
- mail_matches = { }
- else :
- mail_matches = { }
-
- yield 'comment-owner', owner
-
- for group, value in matches.iteritems() :
- if value :
- yield 'comment-{group}'.format(group=group), value.strip()
-
- print u"{hostname:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {host}".format(
- hostname = hostname,
- comment = comment,
- group = matches.get('group', ''),
- owner = owner,
- mail = mail_matches.get('mail', ''),
- host = matches.get('host', ''),
- ).encode('utf-8')
-
def process_zone_hosts (options, file) :
"""
Yield host info from zonefile records.
@@ -141,8 +84,7 @@
yield rr.name, 'ip', ip
if rr.comment :
- for field, value in process_zone_comment(options, rr.name, rr.comment) :
- yield rr.name, field, value
+ yield rr.name, 'comment', rr.comment
elif rr.type == 'CNAME' :
host, = rr.data
@@ -247,7 +189,73 @@
for info in process_dhcp_conf(options,
pvl.args.apply_file(options.import_dhcp_hosts)) :
yield info
-
+
+ZONE_COMMENTS = (
+ re.compile(r'(?P<owner>[^/]+)\s*-\s+(?P<host>.+)'),
+ re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[/-]\s+(?P<host>.+)'),
+ re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)\s+[(]\s*(?P<host>.+)[)]'),
+ re.compile(r'(?P<group>.+?)\s*/\s*(?P<owner>.+)'),
+ re.compile(r'(?P<owner>.+)'),
+)
+
+ZONE_OWNER_MAIL = re.compile(r'(?P<owner>.*?)\s*<(?P<mail>.+?)>')
+
+def process_zone_comment (options, hostname, comment) :
+ """
+ Attempt to parse a host comment field... :D
+
+ Yields (field, value) bits
+ """
+
+ for regex in ZONE_COMMENTS :
+ match = regex.match(comment)
+
+ if match :
+ break
+ else :
+ log.warn("%s: unparsed comment: %s", hostname, comment)
+ return
+
+ matches = match.groupdict()
+ owner = matches.pop('owner', None)
+
+ if owner :
+ mail_match = ZONE_OWNER_MAIL.match(owner)
+
+ if mail_match :
+ mail_matches = mail_match.groupdict()
+
+ owner = mail_matches['owner']
+ yield 'mail', mail_matches['mail']
+
+ yield 'owner', owner
+
+ for field, value in matches.iteritems() :
+ if value :
+ yield field, value
+
+
+def process_hosts_comments (options, import_hosts) :
+ """
+ Parse out comments from host imports..
+ """
+
+ for host, field, value in import_hosts :
+ if field != 'comment':
+ yield host, field, value
+ continue
+
+ fields = dict(process_zone_comment(options, host, value))
+
+ print u"{host:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {hostinfo}".format(
+ host = host,
+ comment = value,
+ group = fields.get('group', ''),
+ owner = fields.get('owner', ''),
+ mail = fields.get('mail', ''),
+ hostinfo = fields.get('host', ''),
+ ).encode('utf-8')
+
def process_hosts_import (options, import_hosts) :
"""
Import host definitions from given infos
@@ -270,6 +278,7 @@
else :
# import
import_hosts = apply_hosts_import(options)
+ import_hosts = process_hosts_comments(options, import_hosts)
hosts = process_hosts_import(options, import_hosts)
# output