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
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
# 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()