Esempio n. 1
0
def package_adhoc_commit(project: Project, validate: bool = True, yaml_path: Optional[str] = None) -> Dict[str, Any]:
    """
    Create an ad-hoc tarball and commit of the project directory.

    :return: Commit response object from API
    """
    project.refresh_details()
    directory = project.directory
    tarball = None
    try:
        description = ''
        try:
            description = describe_current_commit(directory)
        except (NoGitRepo, NoCommit):
            pass
        except Exception as exc:
            warn(f'Unable to derive Git description: {exc}')

        if description:
            click.echo(f'Packaging {directory} ({description})...')
        else:
            click.echo(f'Packaging {directory}...')

        yaml_path = yaml_path or project.get_yaml_path()
        tarball = package_directory(directory=directory, progress=True, validate=validate, yaml_path=yaml_path)
        return create_adhoc_commit_from_tarball(project=project, tarball=tarball, yaml_path=yaml_path, description=description)
    finally:
        if tarball:
            try:
                os.unlink(tarball)
            except OSError as err:  # pragma: no cover
                warn(f'Unable to remove temporary file: {err}')
Esempio n. 2
0
def update_yaml_from_source(source_path: str, project: Project, yaml_file: Optional[str]) -> bool:
    """Updates valohai.yaml by parsing the source code file for a call to valohai.prepare()

    Call to valohai.prepare() will contain step name, parameters and inputs.
    We use the AST parser to parse those from the Python source code file and
    update (or generate) valohai.yaml accordingly.

    :param source_path: Path of the Python source code file
    :param project: Currently linked Valohai project

    """
    old_config = get_current_config(project, yaml_file)
    new_config = get_updated_config(source_path, project, yaml_file)
    if old_config != new_config:
        project.refresh_details()
        with open(project.get_config_filename(yaml_path=yaml_file), 'w') as out_file:
            out_file.write(config_to_yaml(new_config))
        return True
    return False