From: Greg Price Date: Wed, 29 Oct 2008 04:44:44 +0000 (-0400) Subject: factor a bit of code out of the mondo invirt.config.load() X-Git-Tag: 0.0.2~3 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-base.git/commitdiff_plain/1eff7d13e6578fc1e4da25cd3e78c126ebe62ebd factor a bit of code out of the mondo invirt.config.load() svn path=/trunk/packages/invirt-base/; revision=1421 --- diff --git a/python/invirt/config.py b/python/invirt/config.py index 0e906d5..7c2b805 100644 --- a/python/invirt/config.py +++ b/python/invirt/config.py @@ -5,11 +5,22 @@ from invirt.common import * from os import rename from os.path import getmtime from contextlib import closing +import yaml + +try: loader = yaml.CSafeLoader +except: loader = yaml.SafeLoader src_path = '/etc/invirt/master.yaml' cache_path = '/var/lib/invirt/cache.json' lock_path = '/var/lib/invirt/cache.lock' +def load_master(): + with closing(file(src_path)) as f: + return yaml.load(f, loader) + +def get_src_mtime(): + return getmtime(src_path) + def load(force_refresh = False): """ Try loading the configuration from the faster-to-load JSON cache at @@ -25,7 +36,7 @@ def load(force_refresh = False): if force_refresh: do_refresh = True else: - src_mtime = getmtime(src_path) + src_mtime = get_src_mtime() try: cache_mtime = getmtime(cache_path) except OSError: do_refresh = True else: do_refresh = src_mtime + 1 >= cache_mtime @@ -66,21 +77,16 @@ def load(force_refresh = False): # is interleaved). The final atomic rename is to keep this # transactionally isolated from the above cache read. If we fail to # acquire the lock, just try to load the master configuration. - import yaml - try: loader = yaml.CSafeLoader - except: loader = yaml.SafeLoader try: with lock_file(lock_path): - with closing(file(src_path)) as f: - ns.cfg = yaml.load(f, loader) + ns.cfg = load_master() try: with closing(file(cache_path + '.tmp', 'w')) as f: f.write(json.write(ns.cfg)) except: pass # silent failure else: rename(cache_path + '.tmp', cache_path) except IOError: - with closing(file(src_path)) as f: - ns.cfg = yaml.load(f, loader) + ns.cfg = load_master() return ns.cfg dicts = load()