bin/pvl.hosts-import
changeset 288 2f2f92e4c58e
parent 285 e18aaec99e54
child 301 b41902b0b9cf
equal deleted inserted replaced
287:968130f21270 288:2f2f92e4c58e
   419     for host, field, value in import_hosts :
   419     for host, field, value in import_hosts :
   420         hosts[host][field].append(value)
   420         hosts[host][field].append(value)
   421     
   421     
   422     return hosts.iteritems()
   422     return hosts.iteritems()
   423 
   423 
   424 def process_export_hosts (options, hosts) :
   424 def check_hosts (options, hosts) :
       
   425     by_name = dict(hosts)
       
   426 
       
   427     for host, fields in hosts :
       
   428         if set(fields) == set(['alias']) :
       
   429             log.warn("%s: nonexistant alias target: %s", host, ' '.join(fields['alias']))
       
   430 
       
   431 def sort_export_hosts (options, hosts) :
   425     if options.output_prefix :
   432     if options.output_prefix :
   426         prefix = ipaddr.IPNetwork(options.output_prefix)
   433         prefix = ipaddr.IPNetwork(options.output_prefix)
   427     else :
   434     else :
   428         prefix = None
   435         prefix = None
   429 
   436 
   452     FMT = u"\t{field:15} = {value}"
   459     FMT = u"\t{field:15} = {value}"
   453 
   460 
   454     yield u"[{domain}]".format(domain=options.hosts_domain)
   461     yield u"[{domain}]".format(domain=options.hosts_domain)
   455 
   462 
   456     # filter + sort
   463     # filter + sort
   457     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   464     hosts = [(host, fields) for sort, host, fields in sorted(sort_export_hosts(options, hosts))]
   458 
   465 
   459     for host, fields in hosts :
   466     for host, fields in hosts :
   460         for comment in fields.get('comment-host', ()):
   467         for comment in fields.get('comment-host', ()):
   461             yield u"# {comment}".format(comment=comment)
   468             yield u"# {comment}".format(comment=comment)
   462 
   469 
   507     if args :
   514     if args :
   508         # direct from file
   515         # direct from file
   509         hosts = pvl.args.apply_files(args, 'r', options.input_charset)
   516         hosts = pvl.args.apply_files(args, 'r', options.input_charset)
   510     else :
   517     else :
   511         # import
   518         # import
   512         hosts = import_hosts(options)
   519         hosts = list(import_hosts(options))
   513    
   520    
       
   521     # verify
       
   522     check_hosts(options, hosts)
       
   523 
   514     # output
   524     # output
   515     if options.output_hosts :
   525     if options.output_hosts :
   516         apply_hosts_export(options, hosts)
   526         apply_hosts_export(options, hosts)
   517 
   527 
   518 if __name__ == '__main__':
   528 if __name__ == '__main__':