From 50d489f38e18577cb7e75605515d4b8d567aaa52 Mon Sep 17 00:00:00 2001 From: Sol Jerome Date: Mon, 4 Apr 2011 08:59:54 -0500 Subject: tools: PY3K + PEP8 fixes Signed-off-by: Sol Jerome --- tools/batchadd.py | 66 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 21 deletions(-) (limited to 'tools/batchadd.py') diff --git a/tools/batchadd.py b/tools/batchadd.py index ce47650a5..e8008b330 100755 --- a/tools/batchadd.py +++ b/tools/batchadd.py @@ -1,23 +1,37 @@ #!/usr/bin/python -import sys, os from datetime import date +import os +import sys + os.environ['DJANGO_SETTINGS_MODULE'] = 'Bcfg2.Server.Hostbase.settings' from Bcfg2.Server.Hostbase.hostbase.models import * from Bcfg2.Server.Hostbase.settings import DEFAULT_MX, PRIORITY import Bcfg2.Server.Hostbase.regex -host_attribs = ['hostname', 'whatami', 'netgroup', 'security_class', 'support', - 'csi', 'printq', 'outbound_smtp', 'primary_user', - 'administrator', 'location', 'expiration_date', 'comments'] +host_attribs = ['administrator', + 'comments', + 'csi', + 'expiration_date', + 'hostname', + 'location', + 'netgroup', + 'outbound_smtp', + 'primary_user', + 'printq', + 'security_class', + 'support', + 'whatami'] + def handle_error(field): if '-f' in sys.argv: return - print "Error: %s is already defined in hostbase" % field + print("Error: %s is already defined in hostbase" % field) if '-s' in sys.argv: sys.exit(1) + def checkformat(values, indices): """Ensures file contains all necessary attributes in order """ filelist = [pair[0] for pair in values] @@ -34,8 +48,8 @@ def checkformat(values, indices): # process rest of host attributes try: next = filelist[1:].index('hostname') - remaining = filelist[13:next+1] - filelist = filelist[next+1:] + remaining = filelist[13:next + 1] + filelist = filelist[next + 1:] except: remaining = filelist[13:] needfields = ['mac_addr', 'hdwr_type', 'ip_addr'] @@ -50,7 +64,7 @@ if __name__ == '__main__': try: fd = open(sys.argv[1], 'r') except (IndexError, IOError): - print "\nUsage: batchadd.py filename\n" + print("\nUsage: batchadd.py filename\n") sys.exit() lines = fd.readlines() @@ -59,18 +73,20 @@ if __name__ == '__main__': if line.lstrip(' ')[0] != '#' and line != '\n'] if info[0][0] == 'mx' and info[1][0] == 'priority': - mx, created = MX.objects.get_or_create(mx=info[0][1], priority=info[1][1]) + mx, created = MX.objects.get_or_create(mx=info[0][1], + priority=info[1][1]) info = info[2:] - else: - mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, priority=PRIORITY) + mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, + priority=PRIORITY) if created: mx.save() - hostindices = [num for num in range(0, len(info)) if info[num][0] == 'hostname'] + hostindices = [num for num in range(0, len(info)) + if info[num][0] == 'hostname'] if not checkformat(info, hostindices): - print "Error: file format" + print("Error: file format") sys.exit() ################# @@ -83,7 +99,8 @@ if __name__ == '__main__': # do something here pass - macindices = [num for num in range(0, len(info)) if info[num][0] == 'mac_addr'] + macindices = [num for num in range(0, len(info)) + if info[num][0] == 'mac_addr'] for mac_addr in macindices: try: host = Interface.objects.get(mac_addr=info[mac_addr][1]) @@ -103,7 +120,9 @@ if __name__ == '__main__': blank.__dict__[pair[0]] = 0 elif pair[0] == 'expiration_date': (year, month, day) = pair[1].split("-") - blank.expiration_date = date(int(year), int(month), int(day)) + blank.expiration_date = date(int(year), + int(month), + int(day)) else: blank.__dict__[pair[0]] = pair[1] blank.status = 'active' @@ -113,7 +132,9 @@ if __name__ == '__main__': while info and info[0][0] != 'hostname': if info[0][0] == 'mac_addr': pair = info.pop(0) - inter = Interface.objects.create(host=blank, mac_addr=pair[1], hdwr_type='eth') + inter = Interface.objects.create(host=blank, + mac_addr=pair[1], + hdwr_type='eth') if not pair[1]: inter.dhcp = False inter.save() @@ -124,13 +145,17 @@ if __name__ == '__main__': elif info[0][0] == 'ip_addr': pair = info.pop(0) ip = IP.objects.create(interface=inter, ip_addr=pair[1]) - hostnamenode = Name(ip=ip, name=blank.hostname, dns_view='global', only=False) + hostnamenode = Name(ip=ip, + name=blank.hostname, + dns_view='global', + only=False) hostnamenode.save() - namenode = Name(ip=ip, name=".".join([newhostname + "-" + inter.hdwr_type, - newdomain]), + namenode = Name(ip=ip, + name=".".join([newhostname + "-" + inter.hdwr_type, + newdomain]), dns_view="global", only=False) namenode.save() - subnetnode = Name(ip=ip, name=newhostname + "-" + + subnetnode = Name(ip=ip, name=newhostname + "-" + ip.ip_addr.split(".")[2] + "." + newdomain, dns_view="global", only=False) subnetnode.save() @@ -141,4 +166,3 @@ if __name__ == '__main__': pair = info.pop(0) cname = CName.objects.create(name=hostnamenode, cname=pair[1]) cname.save() - -- cgit v1.2.3-1-g7c22