diff options
author | 2009-11-18 11:08:22 +0100 | |
---|---|---|
committer | 2009-11-18 11:08:22 +0100 | |
commit | a828ed38772ff8b8bbe866bd03d0cfe554680b4f (patch) | |
tree | 798799c22586b79656e9512a9ff7ab8443873b9d | |
parent | Fix various bugs (diff) | |
download | kernel-check-a828ed38772ff8b8bbe866bd03d0cfe554680b4f.tar.gz kernel-check-a828ed38772ff8b8bbe866bd03d0cfe554680b4f.tar.bz2 kernel-check-a828ed38772ff8b8bbe866bd03d0cfe554680b4f.zip |
Refactoring all parameters in cron.py
-rwxr-xr-x | tools/cron.py | 128 |
1 files changed, 71 insertions, 57 deletions
diff --git a/tools/cron.py b/tools/cron.py index f5d7f91..9c44de1 100755 --- a/tools/cron.py +++ b/tools/cron.py @@ -21,6 +21,22 @@ class CronError(Exception): def __init__(self, value): self.value = value +CONST = { + 'minyear' : 2002, + 'maxyear' : 2012, + 'nvdurl' : 'http://nvd.nist.gov/', + 'bzurl' : 'https://bugs.gentoo.org/', + 'state' : ['NEW', 'ASSIGNED', 'REOPENED', + 'RESOLVED', 'VERIFIED', 'CLOSED'], + 'resolut' : ['FIXED', 'LATER', 'TEST-REQUEST', 'UPSTREAM', '---'], + 'bugorder' : ['bugid', 'reporter', 'reported', + 'status', 'arch', 'affected'], + 'cveorder' : ['cve', 'published', 'desc', 'severity', + 'vector', 'score', 'refs'], + 'filepath' : os.path.dirname(os.path.realpath(__file__)), + 'portdir' : portage.settings['PORTDIR'] +} + NOCVE = { 'cve' : 'GENERIC-MAP-NOMATCH', 'published' : '0000-00-00', @@ -35,41 +51,32 @@ NOCVE = { 'refs' : et.Element('refs') } -DELAY = 0.2 -SKIP = False -MINYEAR = 2002 -MAXYEAR = 2012 -NVDURL = 'http://nvd.nist.gov/' -BZURL = 'https://bugs.gentoo.org/' -STATE = ['NEW', 'ASSIGNED', 'REOPENED', 'RESOLVED', 'VERIFIED', 'CLOSED'] -RESOLUTION = ['FIXED', 'LATER', 'TEST-REQUEST', 'UPSTREAM', '---'] -BUGORDER = ['bugid', 'reporter', 'reported', 'status', 'arch', 'affected'] -CVEORDER = ['cve', 'published', 'desc', 'severity', 'vector', 'score', 'refs'] -FILEPATH = os.path.dirname(os.path.realpath(__file__)) -PORTDIR = portage.settings['PORTDIR'] -LOGFILE = os.path.join(FILEPATH, 'cron.log') -DIR = { - 'tmp' : os.path.join(FILEPATH, 'tmp'), - 'out' : os.path.join(PORTDIR, 'metadata', 'kernel'), - 'bug' : os.path.join(FILEPATH, 'tmp', 'bug'), - 'nvd' : os.path.join(FILEPATH, 'tmp', 'nvd') +PARAM = { + 'delay' : 0.2, + 'skip' : False, + 'logfile' : os.path.join(CONST['filepath'], 'cron.log'), + 'tmpdir' : os.path.join(CONST['filepath'], 'tmp'), + 'bugdir' : os.path.join(CONST['filepath'], 'tmp', 'bug'), + 'nvddir' : os.path.join(CONST['filepath'], 'tmp', 'nvd'), + 'outdir' : os.path.join(CONST['portdir'], 'metadata', 'kernel') } + REGEX = { - 'bugzilla' : re.compile(r'(?<=bug.cgi\?id=)\d*'), - 'grp_all' : re.compile(r'(?<=\()[ (]*CVE-(\d{4})' \ - r'([-,(){}|, \d]+)(?=\))'), - 'grp_split' : re.compile(r'(?<=\D)(\d{4})(?=\D|$)'), - 'm_nomatch' : re.compile(r'.*GENERIC-MAP-NOMATCH.*'), - 'wb_match' : re.compile(r'\s*\[\s*([^ +<=>]+)\s*([' \ - r'<=>]{1,2})\s*([^ <=>\]]+' \ - r')\s*(?:([<=>]{1,2})\s*([' \ - r'^ \]]+))?\s*\]\s*(.*)'), - 'wb_version' : re.compile(r'^(?:\d{1,2}\.){0,3}\d{1,2}' \ - r'(?:[-_](?:r|rc)?\d{1,2})*$') + 'bugzilla' : re.compile(r'(?<=bug.cgi\?id=)\d*'), + 'grp_all' : re.compile(r'(?<=\()[ (]*CVE-(\d{4})([-,(){}|, \d]+)(?=\))'), + 'grp_split' : re.compile(r'(?<=\D)(\d{4})(?=\D|$)'), + 'm_nomatch' : re.compile(r'.*GENERIC-MAP-NOMATCH.*'), + 'wb_match' : re.compile(r'\s*\[\s*([^ +<=>]+)\s*([<=>]{1,2})' \ + r'\s*([^ <=>\]]+)\s*(?:([<=>]{1,2})' \ + r'\s*([^ \]]+))?\s*\]\s*(.*)'), + 'wb_vers' : re.compile(r'^(?:\d{1,2}\.){0,3}\d{1,2}' \ + r'(?:[-_](?:r|rc)?\d{1,2})*$') } + CVES = dict() logging.basicConfig(format='[%(asctime)s] %(levelname)-6s : %(message)s', - datefmt='%H:%M:%S', filename=LOGFILE, level=logging.DEBUG) + datefmt='%H:%M:%S', filename=PARAM['logfile'], + level=logging.DEBUG) def main(argv): @@ -77,38 +84,44 @@ def main(argv): logging.info('Running cron') + for item in sorted(PARAM): + logging.info('Parameter %-8s = %s' % + (item, '\'' + str(PARAM[item]) + '\'')) + current_year = datetime.datetime.now().year - if current_year < MINYEAR or current_year > MAXYEAR: - current_year = MAXYEAR + if current_year < CONST['minyear'] or current_year > CONST['maxyear']: + current_year = CONST['maxyear'] - for directory in DIR: - if not os.path.isdir(DIR[directory]): - os.makedirs(DIR[directory]) + for directory in PARAM: + if 'dir' in directory and not os.path.isdir(PARAM[directory]): + os.makedirs(PARAM[directory]) logging.info('Receiving the latest xml file from the nvd') - receive_file(DIR['nvd'], [NVDURL, 'download/'],'nvdcve-recent.xml') + receive_file(PARAM['nvddir'], [CONST['nvdurl'], + 'download/'],'nvdcve-recent.xml') - if not SKIP: + if not PARAM['skip']: logging.info('Receiving earlier xml files from the nvd') - for year in xrange(MINYEAR, current_year + 1): - receive_file(DIR['nvd'], [NVDURL, 'download/'], + for year in xrange(CONST['minyear'], current_year + 1): + receive_file(PARAM['nvddir'], [CONST['nvdurl'], 'download/'], 'nvdcve-%s.xml' % str(year)) logging.info('Receiving the kernel vulnerability list from bugzilla') - url = [BZURL, 'buglist.cgi?query_format=advanced&component=Kernel'] + url = [CONST['bzurl'], 'buglist.cgi?query_format=advanced' \ + '&component=Kernel'] - for item in STATE: + for item in CONST['state']: url.append('&bug_status=' + item) - for item in RESOLUTION: + for item in CONST['resolut']: url.append('&resolution=' + item) url.append('#') - receive_file(DIR['tmp'], url, 'bugzilla.xml') + receive_file(PARAM['tmpdir'], url, 'bugzilla.xml') - filename = os.path.join(DIR['tmp'], 'bugzilla.xml') + filename = os.path.join(PARAM['tmpdir'], 'bugzilla.xml') with open(filename, 'r+') as buglist_file: memory_map = mmap.mmap(buglist_file.fileno(), 0) buglist = REGEX['bugzilla'].findall(memory_map.read(-1)) @@ -116,17 +129,17 @@ def main(argv): logging.info('Found %i kernel vulnerabilities' % len(buglist)) logging.info('Creating the nvd dictionary') - nvd_dict = parse_nvd_dict(DIR['nvd']) + nvd_dict = parse_nvd_dict(PARAM['nvddir']) logging.info('Creating the xml files') created_files = 0 for item in buglist: try: - receive_file(DIR['bug'], [BZURL, 'show_bug.cgi?ctype=xml&id='], - item) + receive_file(PARAM['bugdir'], [CONST['bzurl'], + 'show_bug.cgi?ctype=xml&id='], item) - vul = parse_bz_dict(DIR['bug'], item) + vul = parse_bz_dict(PARAM['bugdir'], item) for cve in vul['cvelist']: if cve == NOCVE['cve']: @@ -138,9 +151,9 @@ def main(argv): except KeyError: raise CronError('No Nvd entry: ' + cve) - write_xml_file(DIR['out'], vul) + write_xml_file(PARAM['outdir'], vul) created_files += 1 - time.sleep(DELAY) + time.sleep(PARAM['delay']) except CronError, e: logging.error('[%s] %s' % (item, e.value)) @@ -189,7 +202,7 @@ def parse_nvd_dict(directory): for tree in root: cve = { - 'cve' : tree.get('name'), + 'cve' : tree.get('name'), 'published' : tree.get('published'), 'severity' : tree.get('severity'), 'vector' : tree.get('CVSS_vector'), @@ -207,7 +220,8 @@ def parse_nvd_dict(directory): bugref = et.SubElement(reftree, 'ref') bugref.set('source', 'GENTOO') - bugref.set('url', '%sshow_bug.cgi?id=%s' % (BZURL, cve['cve'])) + bugref.set('url', '%sshow_bug.cgi?id=%s' % (CONST['bzurl'], + cve['cve'])) bugref.text = 'Gentoo %s' % cve['cve'] cve['refs'] = reftree @@ -310,7 +324,7 @@ def interval_from_wb(whiteboard): lower = vers1 upper = vers1 - if not REGEX['wb_version'].match(vers1): + if not REGEX['wb_vers'].match(vers1): return None else: for (char, version) in ((comp1, vers1), (comp2, vers2)): @@ -330,7 +344,7 @@ def interval_from_wb(whiteboard): elif char: return None - if version and not REGEX['wb_version'].match(version): + if version and not REGEX['wb_vers'].match(version): return None interval = { @@ -354,7 +368,7 @@ def write_xml_file(directory, vul): root = et.Element('vulnerability') bugroot = et.SubElement(root, 'bug') - for element in BUGORDER: + for element in CONST['bugorder']: if element == 'affected': affectedroot = et.SubElement(bugroot, 'affected') for item in vul['affected']: @@ -376,14 +390,14 @@ def write_xml_file(directory, vul): for cve in vul['cves']: cveroot = et.SubElement(root, 'cve') if cve == NOCVE['cve']: - for element in CVEORDER: + for element in CONST['cveorder']: if element == 'refs': cveroot.append(NOCVE[element]) else: node = et.SubElement(cveroot, element) node.text = NOCVE[element] else: - for element in CVEORDER: + for element in CONST['cveorder']: if element == 'refs': cveroot.append(cve[element]) else: |