Example #1
0
def _get_specs(app_settings_dir):
    """
    Load all  specs files from base settings directory.

    :param app_settings_dir: path to the base directory holding the
        application's settings. App can be provisioner\installer\tester
        and the path would be: settings/<app_name>/
    :return: dict: All spec files merged into a single dict.
    """
    if not os.path.exists(app_settings_dir):
        raise exceptions.IRFileNotFoundException(app_settings_dir)

    # Collect all app's spec
    spec_files = []
    for root, _, files in os.walk(app_settings_dir):
        spec_files.extend([os.path.join(root, a_file) for a_file in files
                           if a_file.endswith(SPEC_EXTENSION)])

    res = {}
    for spec_file in spec_files:
        # TODO (aopincar): print spec_file in debug mode
        with open(spec_file) as fd:
            spec = yaml.load(fd, Loader=yamlordereddictloader.Loader)
        # TODO (aopincar): preserve OrderedDict when merging?!?
        utils.dict_merge(res, spec)

    return res
Example #2
0
def load(settings_file, update_placeholders=True):
    """
    Loads and returns the content of a given YAML file as a dictionary

    :param settings_file: String represents a path to a YAML file
    :param update_placeholders: Whether to update Placeholder list or not in
    case of Placeholder tag is in the given YAML file
    :return: Dictionary containing the content the given YAML file
    """
    LOG.debug("Loading setting file: %s" % settings_file)
    if not os.path.exists(settings_file):
        raise exceptions.IRFileNotFoundException(settings_file)

    try:
        with open(settings_file) as f_obj:
            loaded_yml = yaml.load(f_obj)

        # Handling case of empty file
        if loaded_yml is None:
            raise exceptions.IREmptySettingsFile(settings_file)

        if update_placeholders:
            for placeholder in Placeholder.placeholders_list[::-1]:
                if placeholder.file_path is None:
                    placeholder.file_path = settings_file
                else:
                    break

        return loaded_yml

    except yaml.constructor.ConstructorError as e:
        raise exceptions.IRYAMLConstructorError(e, settings_file)
Example #3
0
def load_yaml(filename, *search_paths):
    """Find YAML file. search default path first.

    :param filename: path to file
    :param search_first: default path to search first
    :returns: dict. loaded YAML file.
    """
    path = None
    searched_files = []
    files_to_search = map(
        lambda search_path: os.path.join(search_path, filename), search_paths)
    # append file name to verify absolute path by default
    files_to_search.append(filename)

    for filename in files_to_search:
        searched_files.append(os.path.abspath(filename))
        if os.path.exists(filename):
            path = os.path.abspath(filename)
            break

    if path is not None:
        print("Loading YAML file: %s" % path)
        return cli.yamls.load(path)
    else:
        raise exceptions.IRFileNotFoundException(
            file_path=searched_files)
Example #4
0
def _get_specs(root_folder, include_subfolders=True):
    """
    Load all  specs files from base settings directory.

    :param root_folder: path to the base directory holding the
        application's settings. App can be provisioner\installer\tester
        and the path would be: settings/<app_name>/
    :param include_subfolders: specifies whether the subfolders of the root
        folder should be also searched for a spec files.
    :return: dict: All spec files merged into a single dict.
    """
    if not os.path.exists(root_folder):
        raise exceptions.IRFileNotFoundException(root_folder)

    # Collect all app's spec
    spec_files = []
    if include_subfolders:
        for root, _, files in os.walk(root_folder):
            spec_files.extend([
                os.path.join(root, a_file) for a_file in files
                if a_file.endswith(SPEC_EXTENSION)
            ])
    else:
        spec_files = glob.glob('./' + root_folder + '/*' + SPEC_EXTENSION)

    res = {}
    for spec_file in spec_files:
        # TODO (aopincar): print spec_file in debug mode
        with open(spec_file) as fd:
            spec = yaml.load(fd, Loader=yamlordereddictloader.Loader)
        # TODO (aopincar): preserve OrderedDict when merging?!?
        utils.dict_merge(res, spec)

    return res
Example #5
0
def load(file_path, update_placeholders=True):
    """
    Loads the yaml file and return it dict representation.
    """
    LOG.debug("Loading setting file: %s" % file_path)
    if not os.path.exists(file_path):
        raise exceptions.IRFileNotFoundException(file_path)

    try:
        res = dict(configure.Configuration.from_file(file_path).configure())
        if update_placeholders:
            placeholders_list = Placeholder.placeholders_list
            for placeholder in placeholders_list[::-1]:
                if placeholder.file_path is None:
                    placeholder.file_path = file_path
                else:
                    break
        return res
    except yaml.constructor.ConstructorError as e:
        raise exceptions.IRYAMLConstructorError(e, file_path)
    except configure.ConfigurationError as confError:
        # fix for the empty yml files
        if confError.message == 'unconfigured':
            return {}
        else:
            raise
Example #6
0
def set_network_template(filename, path):
    """Find network template. search default path first.

    :param filename: template filename
    :param path: default path to search first
    :returns: absolute path to template file
    """
    filename = os.path.join(path, filename) if os.path.exists(
        os.path.join(path, filename)) else filename
    if os.path.exists(os.path.abspath(filename)):
        LOG.debug("Loading Heat template: %s" % os.path.abspath(filename))
        return os.path.abspath(filename)
    raise exceptions.IRFileNotFoundException(
        file_path=os.path.abspath(filename))
Example #7
0
def normalize_file(file_path):
    """Return a normalized absolutized version of a file

    :param file_path: path to file to be normalized
    :return: normalized path of a file
    :raise: IRFileNotFoundException if the file doesn't exist
    """
    file_path = os.path.expanduser(file_path)
    if not os.path.isabs(file_path):
        abspath = os.path.abspath(file_path)
        LOG.debug('Setting the absolute path of "%s" to: "%s"' %
                  (file_path, abspath))
        file_path = abspath

    if not os.path.exists(file_path):
        raise exceptions.IRFileNotFoundException(file_path)

    return file_path