diff options
author | Wayne Warren <waynr+launchpad@sdf.org> | 2016-05-22 22:56:59 -0700 |
---|---|---|
committer | Wayne Warren <waynr+launchpad@sdf.org> | 2016-09-02 08:58:09 -0700 |
commit | e9a295b1fb2efb073e2bb708c7e0a3b53988f9de (patch) | |
tree | 341b471b351051feb1fc5abe068bb51f82b64568 /jenkins_jobs/builder.py | |
parent | e75062d10323ea0014ad27aaff0fb99a9e25a672 (diff) | |
download | python-jenkins-job-builder-e9a295b1fb2efb073e2bb708c7e0a3b53988f9de.tar.gz python-jenkins-job-builder-e9a295b1fb2efb073e2bb708c7e0a3b53988f9de.tar.xz python-jenkins-job-builder-e9a295b1fb2efb073e2bb708c7e0a3b53988f9de.zip |
Move CacheStorage to its own module.
And rename it to JobCache. The word "Cache" already implies "Storage"
so it makes more sense to name it after the things that it
stores...Jobs!
Change-Id: If7a7a56e8f6a3637f62e285eaa7ba989f67002dc
Diffstat (limited to 'jenkins_jobs/builder.py')
-rw-r--r-- | jenkins_jobs/builder.py | 97 |
1 files changed, 3 insertions, 94 deletions
diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index 7f56d313..cf59d570 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -23,13 +23,12 @@ import operator import os from pprint import pformat import re -import tempfile import time import xml.etree.ElementTree as XML -import yaml import jenkins +from jenkins_jobs.cache import JobCache from jenkins_jobs.constants import MAGIC_MANAGE_STRING from jenkins_jobs.parallel import concurrent from jenkins_jobs import utils @@ -43,96 +42,6 @@ logger = logging.getLogger(__name__) _DEFAULT_TIMEOUT = object() -class CacheStorage(object): - # ensure each instance of the class has a reference to the required - # modules so that they are available to be used when the destructor - # is being called since python will not guarantee that it won't have - # removed global module references during teardown. - _logger = logger - _os = os - _tempfile = tempfile - _yaml = yaml - - 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') - if flush or not os.path.isfile(self.cachefilename): - self.data = {} - else: - with io.open(self.cachefilename, 'r', encoding='utf-8') as yfile: - self.data = yaml.load(yfile) - logger.debug("Using cache: '{0}'".format(self.cachefilename)) - - @staticmethod - def get_cache_dir(): - home = os.path.expanduser('~') - if home == '~': - raise OSError('Could not locate home folder') - xdg_cache_home = os.environ.get('XDG_CACHE_HOME') or \ - os.path.join(home, '.cache') - path = os.path.join(xdg_cache_home, 'jenkins_jobs') - if not os.path.isdir(path): - try: - os.makedirs(path) - except OSError as ose: - # it could happen that two jjb instances are running at the - # same time and that the other instance created the directory - # after we made the check, in which case there is no error - if ose.errno != errno.EEXIST: - raise ose - return path - - def set(self, job, md5): - self.data[job] = md5 - - def clear(self): - self.data.clear() - - def is_cached(self, job): - if job in self.data: - return True - return False - - def has_changed(self, job, md5): - if job in self.data and self.data[job] == md5: - return False - return True - - def save(self): - # use self references to required modules in case called via __del__ - # write to tempfile under same directory and then replace to avoid - # issues around corruption such the process be killed - tfile = self._tempfile.NamedTemporaryFile(dir=self.get_cache_dir(), - delete=False) - tfile.write(self._yaml.dump(self.data).encode('utf-8')) - # force contents to be synced on disk before overwriting cachefile - tfile.flush() - self._os.fsync(tfile.fileno()) - tfile.close() - try: - self._os.rename(tfile.name, self.cachefilename) - except OSError: - # On Windows, if dst already exists, OSError will be raised even if - # it is a file. Remove the file first in that case and try again. - self._os.remove(self.cachefilename) - self._os.rename(tfile.name, self.cachefilename) - - self._logger.debug("Cache written out to '%s'" % self.cachefilename) - - def __del__(self): - # check we initialized sufficiently in case called - # due to an exception occurring in the __init__ - if getattr(self, 'data', None) is not None: - try: - self.save() - except Exception as e: - self._logger.error("Failed to write to cache file '%s' on " - "exit: %s" % (self.cachefilename, e)) - - class JenkinsManager(object): def __init__(self, jjb_config): @@ -146,8 +55,8 @@ class JenkinsManager(object): else: self.jenkins = jenkins.Jenkins(url, user, password) - self.cache = CacheStorage(jjb_config.jenkins['url'], - flush=jjb_config.builder['flush_cache']) + self.cache = JobCache(jjb_config.jenkins['url'], + flush=jjb_config.builder['flush_cache']) self._plugins_list = jjb_config.builder['plugins_info'] self._jobs = None |