Exemplo n.º 1
0
def read_template_manifest(folder, checkout=False):
    """Extract template metadata from the manifest file.

    :param folder: path where to find the template manifest file
    :param checkout: checkout the template folder from local repo
    """
    manifest_path = folder / TEMPLATE_MANIFEST
    try:
        manifest = yaml.safe_load(manifest_path.read_text())
    except FileNotFoundError as e:
        raise errors.InvalidTemplateError(
            'There is no manifest file "{0}"'.format(TEMPLATE_MANIFEST)
        ) from e
    validate_template_manifest(manifest)

    if checkout:
        git_repo = git.Git(str(folder))
        template_folders = [template['folder'] for template in manifest]
        if len(template_folders) < 1:
            raise errors.InvalidTemplateError(
                'Cannot find any valid template in manifest file'
            )
        for template_folder in template_folders:
            template_path = folder / template_folder
            try:
                git_repo.checkout(template_folder)
            except git.exc.GitCommandError as e:
                raise errors.InvalidTemplateError(
                    'Cannot checkout the folder "{0}"'.format(template_folder)
                ) from e
            validate_template(template_path)

    return manifest
Exemplo n.º 2
0
def validate_template(template_path):
    """Validate a local template.

    :param template_path: path of the template to validate
    """
    # TODO: implement a better check
    required_folders = [RENKU_HOME]
    required_files = ["{0}/renku.ini".format(RENKU_HOME), "Dockerfile"]
    for folder in required_folders:
        if not Path(template_path, folder).is_dir():
            raise errors.InvalidTemplateError("Folder {0} is required for the template to be valid".format(folder))
    for file in required_files:
        if not Path(template_path, file).is_file():
            raise errors.InvalidTemplateError("File {0} is required for the template to be valid".format(file))
    return True
Exemplo n.º 3
0
def validate_template_manifest(manifest):
    """Validate manifest content.

    :param manifest: manifest file content
    """
    if not isinstance(manifest, list):
        raise errors.InvalidTemplateError(
            ("The repository doesn't contain a valid", '"{0}" file'.format(TEMPLATE_MANIFEST))
        )
    for template in manifest:
        if not isinstance(template, dict) or "name" not in template:
            raise errors.InvalidTemplateError((f'Every template listed in "{TEMPLATE_MANIFEST}"', " must have a name"))
        for attribute in ["folder", "description"]:
            if attribute not in template:
                raise errors.InvalidTemplateError(
                    ('Template "{0}" doesn\'t have a {1} attribute'.format(template["name"], attribute))
                )
    return True
Exemplo n.º 4
0
def validate_template_manifest(manifest):
    """Validate manifet content.

    :param manifest: manifest file content
    """
    if not isinstance(manifest, list):
        raise errors.InvalidTemplateError((
            'The repository doesn\'t contain a valid',
            '"{0}" file'.format(TEMPLATE_MANIFEST)
        ))
    for template in manifest:
        if not isinstance(template, dict) or 'name' not in template:
            raise errors.InvalidTemplateError((
                'Every template listed in "{0}"',
                ' must have a name'.format(TEMPLATE_MANIFEST)
            ))
        for attribute in ['folder', 'description']:
            if attribute not in template:
                raise errors.InvalidTemplateError((
                    'Template "{0}" doesn\'t have a {1} attribute'.format(
                        template['name'], attribute
                    )
                ))
    return True