Changeset 1421


Ignore:
Timestamp:
Oct 29, 2008, 12:44:44 AM (16 years ago)
Author:
price
Message:

factor a bit of code out of the mondo invirt.config.load()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-base/python/invirt/config.py

    r1420 r1421  
    66from os.path import getmtime
    77from contextlib import closing
     8import yaml
     9
     10try:    loader = yaml.CSafeLoader
     11except: loader = yaml.SafeLoader
    812
    913src_path   = '/etc/invirt/master.yaml'
    1014cache_path = '/var/lib/invirt/cache.json'
    1115lock_path  = '/var/lib/invirt/cache.lock'
     16
     17def load_master():
     18    with closing(file(src_path)) as f:
     19        return yaml.load(f, loader)
     20
     21def get_src_mtime():
     22    return getmtime(src_path)
    1223
    1324def load(force_refresh = False):
     
    2637        do_refresh = True
    2738    else:
    28         src_mtime = getmtime(src_path)
     39        src_mtime = get_src_mtime()
    2940        try:            cache_mtime = getmtime(cache_path)
    3041        except OSError: do_refresh  = True
     
    6778        # transactionally isolated from the above cache read.  If we fail to
    6879        # acquire the lock, just try to load the master configuration.
    69         import yaml
    70         try:    loader = yaml.CSafeLoader
    71         except: loader = yaml.SafeLoader
    7280        try:
    7381            with lock_file(lock_path):
    74                 with closing(file(src_path)) as f:
    75                     ns.cfg = yaml.load(f, loader)
     82                ns.cfg = load_master()
    7683                try:
    7784                    with closing(file(cache_path + '.tmp', 'w')) as f:
     
    8087                else: rename(cache_path + '.tmp', cache_path)
    8188        except IOError:
    82             with closing(file(src_path)) as f:
    83                 ns.cfg = yaml.load(f, loader)
     89            ns.cfg = load_master()
    8490    return ns.cfg
    8591
Note: See TracChangeset for help on using the changeset viewer.