17
17
import cElementTree
19
from zope.interface import implements
19
21
from canonical.config import config
22
from canonical.launchpad.interfaces.looptuner import ITunableLoop
20
23
from canonical.launchpad.scripts.cveimport import CVEDB_NS, update_one_cve
22
24
from canonical.launchpad.scripts.base import (
23
25
LaunchpadCronScript, LaunchpadScriptFailure)
26
from canonical.launchpad.utilities.looptuner import LoopTuner
29
class CveUpdaterTunableLoop(object):
30
"""An `ITunableLoop` for updating CVEs."""
32
implements(ITunableLoop)
36
def __init__(self, cves, transaction, logger, offset=0):
38
self.transaction = transaction
41
self.total_updated = 0
44
"""See `ITunableLoop`."""
45
return self.offset is None
47
def __call__(self, chunk_size):
48
"""Retrieve a batch of CVEs and update them.
52
chunk_size = int(chunk_size)
54
self.logger.debug("More %d" % chunk_size)
57
end = self.offset + chunk_size
59
self.transaction.begin()
61
cve_batch = self.cves[start:end]
66
update_one_cve(cve, self.logger)
67
self.total_updated += 1
69
self.logger.debug("Committing.")
70
self.transaction.commit()
26
73
class CVEUpdater(LaunchpadCronScript):
27
75
def add_my_options(self):
28
76
"""Parse command line arguments."""
29
self.parser.add_option("-f", "--cvefile", dest="cvefile",
31
help="An XML file containing the CVE database.")
32
self.parser.add_option("-u", "--cveurl", dest="cveurl",
33
default=config.cveupdater.cve_db_url,
34
help="The URL for the gzipped XML CVE database.")
77
self.parser.add_option(
78
"-f", "--cvefile", dest="cvefile", default=None,
79
help="An XML file containing the CVE database.")
80
self.parser.add_option(
81
"-u", "--cveurl", dest="cveurl",
82
default=config.cveupdater.cve_db_url,
83
help="The URL for the gzipped XML CVE database.")
37
86
self.logger.info('Initializing...')
65
115
dom = cElementTree.fromstring(cve_db)
66
116
items = dom.findall(CVEDB_NS + 'item')
67
117
self.logger.info("Updating database...")
70
update_one_cve(item, self.logger)
119
# We use Looptuner to control the ideal number of CVEs
120
# processed in each transaction, during at least 2 seconds.
121
loop = CveUpdaterTunableLoop(items, self.txn, self.logger)
122
loop_tuner = LoopTuner(loop, 2)
73
126
self.logger.info('%d seconds to update database.' % timing.seconds())