def read_config(paths):
    for path in paths:
        if os.path.exists(path):
            try:
                return json.loads(open(path).read())
            except Exception:
                raise ConfigException("invalid metadata file: %s" % path)
    raise ConfigException("No metadata found.")
def render_template(template, config):
    if is_executable(template):
        return render_executable(template, config)
    else:
        try:
            return render_moustache(open(template).read(), config)
        except context.KeyNotFoundError as e:
            raise ConfigException(
                "key '%s' from template '%s' does not exist in metadata file."
                % (e.key, template))
        except Exception as e:
            raise ConfigException("could not render moustache template %s" %
                                  template)
def ensure_type(string_value, type_name='default'):
    if type_name not in TYPES:
        raise ValueError("requested validation of unknown type: %s" %
                         type_name)
    if not re.match(TYPES[type_name], string_value):
        raise ConfigException("cannot interpret value '%s' as type %s" %
                              (string_value, type_name))
    return string_value
def strip_hash(h, keys):
    if not keys:
        return h
    for k in keys.split('.'):
        if k in h and isinstance(h[k], dict):
            h = h[k]
        else:
            raise ConfigException(
                "key '%s' does not correspond to a hash in the metadata file" %
                keys)
    return h
def render_executable(path, config):
    p = subprocess.Popen([path],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    stdout, stderr = p.communicate(json.dumps(config))
    p.wait()
    if p.returncode != 0:
        raise ConfigException(
            "config script failed: %s\n\nwith output:\n\n%s" %
            (path, stdout + stderr))
    return stdout
def print_key(config_path, key, type_name, default=None):
    config = read_config(config_path)
    keys = key.split('.')
    for key in keys:
        try:
            config = config[key]
        except KeyError:
            if default is not None:
                print default
                return
            else:
                raise ConfigException('key %s does not exist in %s' %
                                      (key, config_path))
    ensure_type(config, type_name)
    print config
def main(argv=sys.argv):
    opts = parse_opts(argv)
    if opts.print_templates:
        print(opts.templates)
        return 0

    try:
        if opts.templates is None:
            raise ConfigException('missing option --templates')

        if opts.key:
            print_key(opts.metadata, opts.key, opts.type, opts.key_default)
        else:
            install_config(opts.metadata, opts.templates, opts.output,
                           opts.validate, opts.subhash)
            logger.info("success")
    except ConfigException as e:
        logger.error(e)
        return 1
    return 0