#!/usr/bin/python #Written by Adam Robinson (adarobin@umflint.edu) #This program is free software: you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation, either version 2 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program. If not, see . import sys, re, xmlrpclib, os, pgdb from rhn import rpclib from lxml import etree #open the xml file and connect to the spacewalk server and the database xmlfile = open("/root/uln/uln-errata.xml") tree = etree.parse(xmlfile) spacewalk = xmlrpclib.Server("http://spacewalk.hostname/rpc/api", verbose=0) key = spacewalk.auth.login("admin", "password") spacedb = pgdb.connect(dsn="localhost:spaceschema", user="spacewalk", password="password") cursor = spacedb.cursor() ulnchannels = [] spwchannels = [] for channel in spacewalk.channel.listAllChannels(key): spwchannels.append(channel["label"]) for element in tree.iterfind("errata/product"): if element.text not in ulnchannels: ulnchannels.append(element.text) for channel in ulnchannels: if channel in spwchannels: packageList = spacewalk.channel.software.listAllPackages(key, channel) errataList = spacewalk.channel.software.listErrata(key, channel) errataListNames = [] for swErrata in errataList: errataListNames.append(swErrata["advisory_name"]) for errata in tree.findall("errata"): if errata.find("product").text == channel: if errata.find("advisory_name").text not in errataListNames: errataInfo = {} bugList = [] keywords = [] packageIDList = [] cveList = [] errataInfo["synopsis"] = errata.find("synopsis").text errataInfo["advisory_name"] = errata.find("advisory_name").text errataInfo["advisory_release"] = int(errata.find("advisory_release").text) errataInfo["advisory_type"] = errata.find("advisory_type").text errataInfo["product"] = errata.find("product").text errataInfo["errataFrom"] = errata.find("errataFrom").text errataInfo["topic"] = errata.find("topic").text errataInfo["description"] = errata.find("description").text[:4000] if errata.find("references").text is not None: errataInfo["references"] = errata.find("references").text else: errataInfo["references"] = "" if errata.find("notes").text is not None: errataInfo["notes"] = errata.find("notes").text else: errataInfo["notes"] = "" errataInfo["solution"] = errata.find("solution").text[:4000] for errataBug in errata.findall("bugs/bug"): bug = {} bug["id"] = int(errataBug.find("id").text) bug["summary"] = errataBug.find("summary").text if errataBug.find("url").text is not None: bug["url"] = errataBug.find("url").text else: bug["url"] = "" bugList.append(bug) for errataCVEs in errata.findall("CVEs"): for errataCVE in errataCVEs.findall("cve"): cveList.append(errataCVE.text) for package in packageList: for errataPackage in errata.findall("packages/package"): if package["name"] == errataPackage.find("name").text and package["version"] == errataPackage.find("version").text and package["release"] == errataPackage.find("release").text: if package["id"] not in packageIDList: packageIDList.append(package["id"]) if packageIDList != []: print "Attempting to add " + errataInfo["advisory_name"] try: errataCreate = spacewalk.errata.create(key, errataInfo, bugList, keywords, packageIDList, True, [channel]) print errataCreate["id"] print errataCreate["date"] print errataCreate["advisory_type"] print errataCreate["advisory_name"] print errataCreate["advisory_synopsis"] print "--------------------------------------------" updateStruct = {} updateStruct["cves"] = cveList spacewalk.errata.setDetails(key, errataInfo["advisory_name"], updateStruct) if errata.find("date").text is not None: cursor.execute("""update rhnerrata set issue_date = to_timestamp(%s,'YYYY-MM-DD'), update_date = to_timestamp(%s,'YYYY-MM-DD') where advisory_name = %s""", (errata.find("date").text, errata.find("date").text, errata.find("advisory_name").text)) else: cursor.execute("""update rhnerrata set issue_date = timestamptz 'epoch', update_date = timestamptz 'epoch' where advisory_name = %s""", [errata.find("advisory_name").text]) except xmlrpclib.ProtocolError: print "Failed!" spacedb.commit() spacewalk.auth.logout(key)