Section: base
Priority: extra
Maintainer: SIPB Xen Project <sipb-xen@mit.edu>
-Build-Depends: cdbs (>= 0.4.23-1.1), debhelper (>= 4.1.0), python-json (>= 3.4-2), python-yaml (>= 3.05-1andersk1)
+Build-Depends: cdbs (>= 0.4.23-1.1), debhelper (>= 4.1.0)
Standards-Version: 3.7.2
Package: sipb-xen-base
Architecture: all
-Depends: ${misc:Depends},
+Depends: ${misc:Depends}, python-json (>= 3.4-2), python-yaml (>= 3.05)
Description: Base configuration required for all SIPB xen servers
This package includes apt configuration, .k5login and other files that
should be synchronized among all our servers.
#!/usr/bin/env python
"""
-invirt-getconf [-f FILE] KEY prints the configuration the option named KEY from
-the invirt configuration file FILE. Keys are dot-separated paths into the YAML
+invirt-getconf loads an invirt configuration file (either the original YAML
+source or the faster-to-load JSON cache) and prints the configuration option
+with the given name (key). Keys are dot-separated paths into the YAML
configuration tree. List indexes (0-based) are also treated as path
components.
(Due to this path language, certain restrictions are placed on the keys used in
-the YAML configuration, e.g. they cannot contain dots.)
+the YAML configuration; e.g., they cannot contain dots.)
Examples:
except IndexError: raise invirt_exception(
'%s: index %s out of range' % (progress, component))
print conf
- except (invirt_exception, OSError), ex:
+ except invirt_exception, ex:
print >> stderr, ex
return 1
--- /dev/null
+import unittest
+
+class struct(object):
+ 'A simple namespace object.'
+ def __init__(self, d = {}):
+ 'd is the dictionary to update my __dict__ with.'
+ self.__dict__.update(d)
+
+def dicts2struct(x):
+ """
+ Given a tree of lists/dicts, perform a deep traversal to transform all the
+ dicts to structs.
+ """
+ if type(x) == dict:
+ return struct((k, dicts2struct(v)) for k,v in x.iteritems())
+ elif type(x) == list:
+ return [dicts2struct(v) for v in x]
+ else:
+ return x
+
+def wrap(rsrc, func):
+ "Utility to that emulates with Python 2.5's `with closing(rsrc)`."
+ try: return func(rsrc)
+ finally: rsrc.close()
+
+class common_tests(unittest.TestCase):
+ def test_dicts2structs(self):
+ dicts = {
+ 'atom': 0,
+ 'dict': { 'atom': 'atom', 'list': [1,2,3] },
+ 'list': [ 'atom', {'key': 'value'} ]
+ }
+ structs = dicts2struct(dicts)
+ self.assertEqual(structs.atom, dicts['atom'])
+ self.assertEqual(structs.dict.atom, dicts['dict']['atom'])
+ self.assertEqual(structs.dict.list, dicts['dict']['list'])
+ self.assertEqual(structs.list[0], dicts['list'][0])
+ self.assertEqual(structs.list[1].key, dicts['list'][1]['key'])
+
+if __name__ == '__main__':
+ unittest.main()
import json, yaml
+from invirt.common import *
from os import error, makedirs
from os.path import dirname, getmtime
try: default_loader = yaml.CSafeLoader
except: default_loader = yaml.SafeLoader
-def wrap(rsrc, func):
- "Utility to that emulates with Python 2.5's `with closing(rsrc)`."
- try: return func(rsrc)
- finally: rsrc.close()
-
def load(src_path = default_src_path,
cache_path = default_cache_path,
force_refresh = False):
except OSError: do_refresh = True
else: do_refresh = src_mtime > cache_mtime
+ if not do_refresh:
+ # try reading from the cache first
+ try: cfg = wrap(file(cache_path), lambda f: json.read(f.read()))
+ except: do_refresh = True
+
if do_refresh:
# reload the source and regenerate the cache
cfg = wrap(file(src_path), lambda f: yaml.load(f, default_loader))
- wrap(file(cache_path, 'w'), lambda f: f.write(json.write(cfg)))
- else:
- cfg = wrap(file(cache_path), lambda f: json.read(f.read()))
+ try: wrap(file(cache_path, 'w'), lambda f: f.write(json.write(cfg)))
+ except: pass # silent failure
return cfg
+dicts = load()
+structs = dicts2struct(dicts)
+
# vim:et:sw=4:ts=4