#!/usr/bin/env python # Computers # # Douglas Thrift # # $Id$ from __init__ import lock from ccscslab.main.models import Computer, OperatingSystem, Role from ccscslab.main import utils from django.db import transaction import Ft.Xml.Domlette import optparse import os import re import socket @transaction.commit_on_success def main(): parser = optparse.OptionParser() parser.add_option('-D', '--debug', action = 'store_true', dest = 'debug') options = parser.parse_args()[0] lock(options.debug) hosts = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'hosts')) _file = re.compile(r'^[a-z]+\.xml$') computer_ids = [] for file in os.listdir(hosts): if _file.match(file) is not None: with open(os.path.join(hosts, file), 'rb') as file: host = Ft.Xml.Domlette.ValidatingReader.parseStream(file, 'file://' + hosts + '/').xpath('//host')[0] name = host.xpath('string(name)') ip_address = socket.gethostbyname(utils.full_name(name)) sword = host.xpath('string(sword/name)') article = host.xpath('string(sword/uri)') operating_system, inserted = OperatingSystem.objects.get_or_create(name = host.xpath('string(os)')) if operating_system.name == 'Arch Linux': operating_system.image = 'arch_linux.png' operating_system.url = 'http://www.archlinux.org/' elif operating_system.name == 'Darwin': operating_system.image = 'darwin.png' operating_system.url = 'http://developer.apple.com/darwin/' elif operating_system.name == 'Debian GNU/kFreeBSD': operating_system.image = 'debian_kfreebsd.png' operating_system.url = 'http://www.debian.org/ports/kfreebsd-gnu/' elif operating_system.name == 'Debian GNU/Linux': operating_system.image = 'debian.png' operating_system.url = 'http://www.debian.org/' elif operating_system.name == 'DragonFly BSD': operating_system.image = 'dragonfly.png' operating_system.url = 'http://www.dragonflybsd.org/' elif operating_system.name == 'FreeBSD': operating_system.image = 'freebsd.png' operating_system.url = 'http://www.freebsd.org/' elif operating_system.name == 'Gentoo Linux': operating_system.image = 'gentoo_linux.png' operating_system.url = 'http://www.gentoo.org/' elif operating_system.name == 'GNU Hurd': operating_system.image = 'gnu_hurd.png' operating_system.url = 'http://www.gnu.org/software/hurd/hurd.html' elif operating_system.name == 'Mac OS X': operating_system.image = 'mac_os_x.png' operating_system.url = 'http://www.apple.com/macosx/' elif operating_system.name == 'NetBSD': operating_system.image = 'netbsd.png' operating_system.url = 'http://www.netbsd.org/' elif operating_system.name == 'OpenBSD': operating_system.image = 'openbsd.png' operating_system.url = 'http://www.openbsd.org/' elif operating_system.name == 'Solaris': operating_system.image = 'solaris.png' operating_system.url = 'http://www.sun.com/software/solaris/' elif operating_system.name == 'Ubuntu Linux': operating_system.image = 'ubuntu_linux.png' operating_system.url = 'http://www.ubuntu.com/' elif operating_system.name == 'Windows 2003 Server': operating_system.image = 'windows_xp.png' operating_system.url = 'http://www.microsoft.com/windowsserver2003/default.mspx' elif operating_system.name == 'Windows XP Professional': operating_system.image = 'windows_xp.png' operating_system.url = 'http://www.microsoft.com/windowsxp/default.mspx' elif operating_system.name == 'Windows XP Professional x64': operating_system.image = 'windows_xp.png' operating_system.url = 'http://www.microsoft.com/windowsxp/64bit/default.mspx' elif operating_system.name == 'Windows 7 Professional': operating_system.image = 'windows_xp.png' operating_system.url = 'http://www.microsoft.com/windows/windows-7/' operating_system.save() processor = host.xpath('string(box/proc)') if options.debug: print (name + '\n ' + ip_address + '\n ' + sword + '\n ' + article + '\n ' + unicode(operating_system) + '\n ' + ('inserted' if inserted else 'updated') + '\n ' + processor) computer, inserted = Computer.objects.get_or_create(name = name) computer.ip_address = ip_address computer.operating_system = operating_system computer.sword = sword computer.article = article computer.processor = processor motd = os.path.join(hosts,name + '.motd') if os.path.exists(motd): with open(motd, 'rb') as motd: computer.motd = utils.htmlify(motd.read().decode('utf8')[:-1]) if options.debug: print ' motd' computer.save() if options.debug: print ' ' + ('inserted' if inserted else 'updated') roles = [] for role in host.xpath('role'): full_name = role.xpath('string(.)') name = full_name.lower().replace(' ', '-') if options.debug: print ' ' + name + '\n ' + full_name role, inserted = Role.objects.get_or_create(name = name, full_name = full_name) if options.debug: print ' ' + ('inserted' if inserted else 'updated') roles.append(role) computer.roles = roles computer.save() computer_ids.append(computer.id) Computer.objects.exclude(id__in = computer_ids).delete() if __name__ == '__main__': main()