From 0274803af64ec8655f52c89bfbd377f6eb07f2c4 Mon Sep 17 00:00:00 2001 From: David Caro Date: Wed, 7 Aug 2013 17:22:14 +0200 Subject: [PATCH] Added ignore-cache and flush-cache options - ignore-cache will ignore the cache of the specified jobs and update them no matter what - flush cache will entirely flush the cache before updating, no matter which jobs are specified Change-Id: I345349ccfe296a5a57360366497400a859b98b42 Signed-off-by: David Caro --- jenkins_jobs/builder.py | 26 +++++++++++++------------- jenkins_jobs/cmd.py | 12 +++++++++++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index b1b23e2cb..c70c27ef7 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -201,7 +201,7 @@ class YamlParser(object): def getXMLForJob(self, data): kind = data.get('project-type', 'freestyle') for ep in pkg_resources.iter_entry_points( - group='jenkins_jobs.projects', name=kind): + group='jenkins_jobs.projects', name=kind): Mod = ep.load() mod = Mod(self.registry) xml = mod.root_xml(data) @@ -224,7 +224,7 @@ class ModuleRegistry(object): self.global_config = config for entrypoint in pkg_resources.iter_entry_points( - group='jenkins_jobs.modules'): + group='jenkins_jobs.modules'): Mod = entrypoint.load() mod = Mod(self) self.modules.append(mod) @@ -286,7 +286,8 @@ class ModuleRegistry(object): # Look for a component function defined in an entry point for ep in pkg_resources.iter_entry_points( - group='jenkins_jobs.{0}'.format(component_list_type), name=name): + group='jenkins_jobs.{0}'.format(component_list_type), + name=name): func = ep.load() func(parser, xml_parent, component_data) else: @@ -320,20 +321,18 @@ class XmlJob(object): class CacheStorage(object): - def __init__(self, jenkins_url): + def __init__(self, jenkins_url, flush=False): cache_dir = self.get_cache_dir() # One cache per remote Jenkins URL: host_vary = re.sub('[^A-Za-z0-9\-\~]', '_', jenkins_url) self.cachefilename = os.path.join( cache_dir, 'cache-host-jobs-' + host_vary + '.yml') - try: - yfile = file(self.cachefilename, 'r') - except IOError: + if flush or not os.path.isfile(self.cachefilename): self.data = {} return - self.data = yaml.load(yfile) + with file(self.cachefilename, 'r') as yfile: + self.data = yaml.load(yfile) logger.debug("Using cache: '{0}'".format(self.cachefilename)) - yfile.close() @staticmethod def get_cache_dir(): @@ -393,10 +392,11 @@ class Jenkins(object): class Builder(object): def __init__(self, jenkins_url, jenkins_user, jenkins_password, - config=None): + config=None, ignore_cache=False, flush_cache=False): self.jenkins = Jenkins(jenkins_url, jenkins_user, jenkins_password) - self.cache = CacheStorage(jenkins_url) + self.cache = CacheStorage(jenkins_url, flush=flush_cache) self.global_config = config + self.ignore_cache = ignore_cache def delete_job(self, name): self.jenkins.delete_job(name) @@ -440,11 +440,11 @@ class Builder(object): continue md5 = job.md5() if (self.jenkins.is_job(job.name) - and not self.cache.is_cached(job.name)): + and not self.cache.is_cached(job.name)): old_md5 = self.jenkins.get_job_md5(job.name) self.cache.set(job.name, old_md5) - if self.cache.has_changed(job.name, md5): + if self.cache.has_changed(job.name, md5) or self.ignore_cache: self.jenkins.update_job(job.name, job.output()) self.cache.set(job.name, md5) else: diff --git a/jenkins_jobs/cmd.py b/jenkins_jobs/cmd.py index 72930d5f3..568730982 100755 --- a/jenkins_jobs/cmd.py +++ b/jenkins_jobs/cmd.py @@ -49,6 +49,14 @@ def main(): parser.add_argument('--conf', dest='conf', help='Configuration file') parser.add_argument('-l', '--log_level', dest='log_level', default='info', help="Log level (default: %(default)s)") + parser.add_argument( + '--ignore-cache', action='store_true', + dest='ignore_cache', default=False, + help='Ignore the cache and update the jobs anyhow (that will only ' + 'flush the specified jobs cache)') + parser.add_argument( + '--flush-cache', action='store_true', dest='flush_cache', + default=False, help='Flush all the cache entries before updating') options = parser.parse_args() options.log_level = getattr(logging, options.log_level.upper(), @@ -82,7 +90,9 @@ def main(): builder = jenkins_jobs.builder.Builder(config.get('jenkins', 'url'), config.get('jenkins', 'user'), config.get('jenkins', 'password'), - config) + config, + ignore_cache=options.ignore_cache, + flush_cache=options.flush_cache) if options.command == 'delete': for job in options.name: