diff options
author | Bjoern Tropf <asym@gentoo.org> | 2009-12-07 18:44:13 +0100 |
---|---|---|
committer | Bjoern Tropf <asym@gentoo.org> | 2009-12-07 18:44:13 +0100 |
commit | 55b2a01eb7d611d0f1acafc686e96a1695e363b2 (patch) | |
tree | 85a97dfaefe7939710f2db2fc394a429b788b1ff | |
parent | Start porting kernel-check to python3 (diff) | |
download | kernel-check-55b2a01eb7d611d0f1acafc686e96a1695e363b2.tar.gz kernel-check-55b2a01eb7d611d0f1acafc686e96a1695e363b2.tar.bz2 kernel-check-55b2a01eb7d611d0f1acafc686e96a1695e363b2.zip |
Implement filetable
-rwxr-xr-x | tools/cron.py | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/tools/cron.py b/tools/cron.py index 60fa725..82c9350 100755 --- a/tools/cron.py +++ b/tools/cron.py @@ -7,11 +7,13 @@ from contextlib import closing import xml.etree.cElementTree as et import cStringIO import datetime +import hashlib import logging import mmap import os import portage import re +import shutil import sys import time import urllib @@ -108,6 +110,20 @@ def main(argv): if not os.path.isdir(PARAM[directory]): os.makedirs(PARAM[directory]) + logging.info('Creating a filetable for all xml files') + + filetable = dict() + path = '/usr/portage/metadata/kernel' + + for xmlfile in os.listdir(PARAM['outdir']): + xmlfile = os.path.join(PARAM['outdir'], xmlfile) + + if os.path.isfile(xmlfile): + with open(xmlfile, 'r') as data: + filetable[xmlfile] = hashlib.md5(data.read()).hexdigest() + else: + logging.error('Invalid directory: %s', xmlfile) + logging.info('Receiving the latest xml file from the nvd') receive_file(PARAM['nvddir'], [CONST['nvdurl'], @@ -166,7 +182,7 @@ def main(argv): vul['cves'].append(cve) vul['pending'] = True - write_xml_file(PARAM['outdir'], vul) + write_xml_file(PARAM['outdir'], vul, filetable) created_files += 1 time.sleep(PARAM['delay']) @@ -175,6 +191,10 @@ def main(argv): logging.info('Created %i xml files' % created_files) + for key in filetable.keys(): + os.remove(key) + logging.info('File removed %s' % key) + def receive_file(directory, url, xml_file): 'Generic download function' @@ -376,7 +396,7 @@ def interval_from_wb(whiteboard): return affected -def write_xml_file(directory, vul): +def write_xml_file(directory, vul, filetable): 'Writes a bug file containing all important information for kernel-check' filename = os.path.join(directory, vul['bugid'] + '.xml') @@ -433,11 +453,27 @@ def write_xml_file(directory, vul): node = et.SubElement(cveroot, element) node.text = cve[element] - with open(filename, 'w') as xmlout: + hashfile = os.path.join(PARAM['tmpdir'], 'hash.xml') + xmlout_hash = str() + + with open(hashfile, 'w') as xmlout: __indent__(root) doc = et.ElementTree(root) doc.write(xmlout, encoding='utf-8') + with open(hashfile, 'r') as xmlout: + xmlout_hash = hashlib.md5(xmlout.read()).hexdigest() + + if filename in filetable: + if filetable[filename] != xmlout_hash: + shutil.move(hashfile, filename) + logging.debug('File changed %s' % filename) + + del filetable[filename] + else: + shutil.move(hashfile, filename) + logging.debug('File new %s' % filename) + def __indent__(node, level=0): 'Indents xml layout for printing' |