diff options
author | Scott Moser <smoser@ubuntu.com> | 2014-01-27 13:05:11 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2014-01-27 13:05:11 -0500 |
commit | 50c744fc4ebc0de5fc7fdfee6a874cb9cc62bba8 (patch) | |
tree | 26818911da3eef64c076a9b2f4071bf75147c090 /cloudinit | |
parent | f405a5283ede1e0af09eb8bf0dbe5046680641b5 (diff) | |
download | vyos-cloud-init-50c744fc4ebc0de5fc7fdfee6a874cb9cc62bba8.tar.gz vyos-cloud-init-50c744fc4ebc0de5fc7fdfee6a874cb9cc62bba8.zip |
add 'pathprefix2dict' utility for use by DataSourceNoCloud
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/util.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index b3332acd..f36e2733 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1889,3 +1889,28 @@ def expand_dotted_devname(dotted): return toks else: return (dotted, None) + + +def pathprefix2dict(base, required=None, optional=None, delim=os.path.sep): + # return a dictionary populated with keys in 'required' and 'optional' + # by reading files in prefix + delim + entry + if required is None: + required = [] + if optional is None: + optional = [] + + missing = [] + ret = {} + for f in required + optional: + try: + ret[f] = load_file(base + delim + f, quiet=False) + except IOError as e: + if e.errno != errno.ENOENT: + raise + if f in required: + missing.append(f) + + if len(missing): + raise ValueError("Missing required files: %s", ','.join(missing)) + + return ret |