Exemple #1
0
def clean(parts, use_lxd, unprime):
    """Remove a part's assets.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    build_environment = get_build_environment(use_lxd=use_lxd)
    project = get_project(is_managed_host=build_environment.is_managed_host)

    if unprime and not build_environment.is_managed_host:
        raise click.BadOptionUsage("--unprime is not a valid option.")

    if build_environment.is_managed_host or build_environment.is_host:
        step = steps.PRIME if unprime else None
        lifecycle.clean(project, parts, step)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider
        )
        if parts:
            with build_provider_class(project=project, echoer=echo) as instance:
                instance.clean(part_names=parts)
        else:
            build_provider_class(project=project, echoer=echo).clean_project()
            # Clear the prime directory on the host
            lifecycle.clean(project, parts, steps.PRIME)
def clean(parts, use_lxd, destructive_mode, unprime, step):
    """Remove a part's assets.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    # This option is only valid in legacy.
    if step:
        raise click.BadOptionUsage("--step", "no such option: --step")

    build_environment = get_build_environment(
        use_lxd=use_lxd, destructive_mode=destructive_mode)
    project = get_project(is_managed_host=build_environment.is_managed_host)

    if unprime and not build_environment.is_managed_host:
        raise click.BadOptionUsage("--unprime", "no such option: --unprime")

    if build_environment.is_managed_host or build_environment.is_host:
        step = steps.PRIME if unprime else None
        lifecycle.clean(project, parts, step)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        if parts:
            with build_provider_class(project=project,
                                      echoer=echo) as instance:
                instance.clean(part_names=parts)
        else:
            build_provider_class(project=project, echoer=echo).clean_project()
            # Clear the prime directory on the host
            lifecycle.clean(project, parts, steps.PRIME)
Exemple #3
0
def _execute(  # noqa: C901
        step: steps.Step,
        parts: str,
        pack_project: bool = False,
        output: str = None,
        shell: bool = False,
        shell_after: bool = False,
        destructive_mode: bool = False,
        **kwargs) -> "Project":
    # fmt: on
    provider = "host" if destructive_mode else None
    build_environment = env.BuilderEnvironmentConfig(force_provider=provider)
    project = get_project(is_managed_host=build_environment.is_managed_host,
                          **kwargs)

    conduct_project_sanity_check(project)

    if build_environment.is_managed_host or build_environment.is_host:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        echo.info("Launching a VM.")
        with build_provider_class(project=project, echoer=echo) as instance:
            instance.mount_project()
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                else:
                    instance.execute_step(step)
            except Exception:
                if project.debug:
                    instance.shell()
                else:
                    echo.warning(
                        "Run the same command again with --debug to shell into the environment "
                        "if you wish to introspect this failure.")
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    return project
Exemple #4
0
def cleanbuild(*, project, echoer, build_environment, remote='') -> None:
    config = project_loader.load_config(project)
    tar_filename = _create_tar_file(project.info.name)

    if build_environment.is_lxd:
        _deprecated_cleanbuild(project, remote, config, tar_filename)
        return

    build_provider_class = build_providers.get_provider_for('multipass')
    with build_provider_class(project=project, echoer=echoer) as instance:
        instance.provision_project(tar_filename)
        instance.build_project()
        instance.retrieve_snap()
Exemple #5
0
def cleanbuild(
    *, project, project_config, echoer, build_environment, remote=""
) -> None:
    tar_filename = _create_tar_file(project.info.name)

    if build_environment.is_lxd:
        _deprecated_cleanbuild(project, remote, project_config, tar_filename)
        return

    build_provider_class = build_providers.get_provider_for("multipass")
    with build_provider_class(project=project, echoer=echoer) as instance:
        instance.provision_project(tar_filename)
        instance.build_project()
        instance.retrieve_snap()
Exemple #6
0
def clean(ctx, parts, unprime, step, **kwargs):
    """Remove a part's assets.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    # This option is only valid in legacy.
    if step:
        option = "--step" if "--step" in ctx.obj["argv"] else "-s"
        raise click.BadOptionUsage(option, "no such option: {}".format(option))

    build_provider = get_build_provider(**kwargs)
    build_provider_flags = get_build_provider_flags(build_provider, **kwargs)
    apply_host_provider_flags(build_provider_flags)

    is_managed_host = build_provider == "managed-host"

    # Temporary fix to ignore target_arch, silently for clean.
    if "target_arch" in kwargs and build_provider in ["multipass", "lxd"]:
        kwargs.pop("target_arch")

    try:
        project = get_project(is_managed_host=is_managed_host)
    except errors.ProjectNotFoundError:
        # Fresh environment, nothing to clean.
        return

    if unprime and not is_managed_host:
        raise click.BadOptionUsage("--unprime", "no such option: --unprime")

    if build_provider in ["host", "managed-host"]:
        step = steps.PRIME if unprime else None
        lifecycle.clean(project, parts, step)
    else:
        build_provider_class = build_providers.get_provider_for(build_provider)
        if parts:
            with build_provider_class(
                    project=project,
                    echoer=echo,
                    build_provider_flags=build_provider_flags) as instance:
                instance.clean(part_names=parts)
        else:
            build_provider_class(project=project, echoer=echo).clean_project()
            # Clear the prime directory on the host, unless on Windows.
            if sys.platform != "win32":
                lifecycle.clean(project, parts, steps.PRIME)
Exemple #7
0
def clean(parts, step_name):
    """Remove content - cleans downloads, builds or install artifacts.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part --step build
    """
    build_environment = env.BuilderEnvironmentConfig()
    try:
        project = get_project(
            is_managed_host=build_environment.is_managed_host
        )
    except YamlValidationError:
        # We need to be able to clean invalid projects too.
        project = get_project(
            is_managed_host=build_environment.is_managed_host,
            skip_snapcraft_yaml=True
        )

    step = None
    if step_name:
        if step_name == "strip":
            echo.warning(
                "DEPRECATED: Use `prime` instead of `strip` as the step to clean"
            )
            step_name = "prime"
        step = steps.get_step_by_name(step_name)

    if build_environment.is_lxd:
        lxd.Project(project=project, output=None, source=os.path.curdir).clean(
            parts, step
        )
    elif build_environment.is_host:
        lifecycle.clean(project, parts, step)
    else:
        # TODO support for steps.
        if parts or step_name:
            raise errors.SnapcraftEnvironmentError(
                "Build providers are still not feature complete, specifying parts or a step name "
                "is not yet supported.")
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider
        )
        build_provider_class(project=project, echoer=echo).clean_project()
Exemple #8
0
def cleanbuild(*,
               project: Project,
               echoer,
               build_environment,
               remote: str = "") -> str:
    tar_filename = _create_tar_file(project.info.name)

    if build_environment.is_lxd:
        return _deprecated_cleanbuild(project, remote, tar_filename)

    build_provider_class = build_providers.get_provider_for("multipass")
    with build_provider_class(project=project,
                              echoer=echoer,
                              is_ephemeral=True) as instance:
        instance.provision_project(tar_filename)
        instance.build_project()
        instance.retrieve_snap()
        return instance.snap_filename
Exemple #9
0
def clean(ctx, parts, use_lxd, destructive_mode, unprime, step):
    """Remove a part's assets.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    # This option is only valid in legacy.
    if step:
        option = "--step" if "--step" in ctx.obj["argv"] else "-s"
        raise click.BadOptionUsage(option, "no such option: {}".format(option))

    build_environment = get_build_environment(
        use_lxd=use_lxd, destructive_mode=destructive_mode)

    try:
        project = get_project(
            is_managed_host=build_environment.is_managed_host)
    except errors.ProjectNotFoundError:
        # Fresh environment, nothing to clean.
        return

    if unprime and not build_environment.is_managed_host:
        raise click.BadOptionUsage("--unprime", "no such option: --unprime")

    if build_environment.is_managed_host or build_environment.is_host:
        step = steps.PRIME if unprime else None
        lifecycle.clean(project, parts, step)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        if parts:
            with build_provider_class(project=project,
                                      echoer=echo) as instance:
                instance.clean(part_names=parts)
        else:
            build_provider_class(project=project, echoer=echo).clean_project()
            # Clear the prime directory on the host
            lifecycle.clean(project, parts, steps.PRIME)
Exemple #10
0
def clean(parts):
    """Remove a part's assets.

    \b
    Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    build_environment = env.BuilderEnvironmentConfig()
    project = get_project(is_managed_host=build_environment.is_managed_host)

    if build_environment.is_managed_host or build_environment.is_host:
        lifecycle.clean(project, parts)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        build_provider = build_provider_class(project=project, echoer=echo)
        if parts:
            echo.info("Launching a VM.")
            with build_provider_class(project=project,
                                      echoer=echo) as instance:
                instance.clean(part_names=parts)
        else:
            build_provider.clean_project()
Exemple #11
0
def _execute(  # noqa: C901
        step: steps.Step,
        parts: str,
        pack_project: bool = False,
        output: str = None,
        shell: bool = False,
        shell_after: bool = False,
        destructive_mode: bool = False,
        **kwargs) -> "Project":
    _clean_provider_error()
    provider = "host" if destructive_mode else None
    build_environment = env.BuilderEnvironmentConfig(force_provider=provider)
    project = get_project(is_managed_host=build_environment.is_managed_host,
                          **kwargs)

    echo.wrapped("Using {!r}: Project assets will be "
                 "searched for from the {!r} directory.".format(
                     project.info.snapcraft_yaml_file_path,
                     os.path.relpath(project._get_snapcraft_assets_dir(),
                                     project._project_dir),
                 ))

    conduct_project_sanity_check(project)

    if build_environment.is_managed_host or build_environment.is_host:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        try:
            build_provider_class.ensure_provider()
        except build_providers.errors.ProviderNotFound as provider_error:
            if provider_error.prompt_installable:
                click.echo(str(provider_error))
                if click.confirm("Would you like to install it now?"):
                    build_provider_class.setup_provider(echoer=echo)
                else:
                    raise provider_error
            else:
                raise provider_error

        echo.info("Launching a VM.")
        with build_provider_class(project=project, echoer=echo) as instance:
            instance.mount_project()
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                else:
                    instance.execute_step(step)
            except Exception:
                _retrieve_provider_error(instance)
                if project.debug:
                    instance.shell()
                else:
                    echo.warning(
                        "Run the same command again with --debug to shell into the environment "
                        "if you wish to introspect this failure.")
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    return project
Exemple #12
0
def _execute(  # noqa: C901
    step: steps.Step,
    parts: str,
    pack_project: bool = False,
    output: Optional[str] = None,
    shell: bool = False,
    shell_after: bool = False,
    setup_prime_try: bool = False,
    **kwargs,
) -> "Project":
    # Cleanup any previous errors.
    _clean_provider_error()

    build_provider = get_build_provider(**kwargs)
    build_provider_flags = get_build_provider_flags(build_provider, **kwargs)
    apply_host_provider_flags(build_provider_flags)

    is_managed_host = build_provider == "managed-host"

    # Temporary fix to ignore target_arch.
    if kwargs.get("target_arch") is not None and build_provider in ["multipass", "lxd"]:
        echo.warning(
            "Ignoring '--target-arch' flag.  This flag requires --destructive-mode and is unsupported with Multipass and LXD build providers."
        )
        kwargs.pop("target_arch")

    project = get_project(is_managed_host=is_managed_host, **kwargs)
    conduct_project_sanity_check(project, **kwargs)

    project_path = pathlib.Path(project._project_dir)
    if project_path.name in ["build-aux", "snap"]:
        echo.warning(
            f"Snapcraft is running in directory {project_path.name!r}.  If this is the snap assets directory, please run snapcraft from {project_path.parent}."
        )

    if build_provider in ["host", "managed-host"]:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(
                project.prime_dir,
                compression=project._snap_meta.compression,
                output=output,
            )
    else:
        build_provider_class = build_providers.get_provider_for(build_provider)
        try:
            build_provider_class.ensure_provider()
        except build_providers.errors.ProviderNotFound as provider_error:
            if provider_error.prompt_installable:
                if echo.is_tty_connected() and echo.confirm(
                    "Support for {!r} needs to be set up. "
                    "Would you like to do it now?".format(provider_error.provider)
                ):
                    build_provider_class.setup_provider(echoer=echo)
                else:
                    raise provider_error
            else:
                raise provider_error

        with build_provider_class(
            project=project, echoer=echo, build_provider_flags=build_provider_flags
        ) as instance:
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                elif setup_prime_try:
                    instance.expose_prime()
                    instance.execute_step(step)
                else:
                    instance.execute_step(step)
            except Exception:
                _retrieve_provider_error(instance)
                if project.debug:
                    instance.shell()
                else:
                    echo.warning(
                        "Run the same command again with --debug to shell into the environment "
                        "if you wish to introspect this failure."
                    )
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    return project
Exemple #13
0
def _execute(  # noqa: C901
    step: steps.Step,
    parts: str,
    pack_project: bool = False,
    output: Optional[str] = None,
    shell: bool = False,
    shell_after: bool = False,
    setup_prime_try: bool = False,
    **kwargs,
) -> "Project":
    # Cleanup any previous errors.
    _clean_provider_error()

    build_provider = get_build_provider(**kwargs)
    build_provider_flags = get_build_provider_flags(build_provider, **kwargs)
    apply_host_provider_flags(build_provider_flags)

    is_managed_host = build_provider == "managed-host"

    project = get_project(is_managed_host=is_managed_host, **kwargs)
    conduct_project_sanity_check(project, **kwargs)

    if build_provider in ["host", "managed-host"]:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    else:
        build_provider_class = build_providers.get_provider_for(build_provider)
        try:
            build_provider_class.ensure_provider()
        except build_providers.errors.ProviderNotFound as provider_error:
            if provider_error.prompt_installable:
                if echo.is_tty_connected() and echo.confirm(
                        "Support for {!r} needs to be set up. "
                        "Would you like to do that it now?".format(
                            provider_error.provider)):
                    build_provider_class.setup_provider(echoer=echo)
                else:
                    raise provider_error
            else:
                raise provider_error

        with build_provider_class(
                project=project,
                echoer=echo,
                build_provider_flags=build_provider_flags) as instance:
            instance.mount_project()
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                elif setup_prime_try:
                    instance.expose_prime()
                    instance.execute_step(step)
                else:
                    instance.execute_step(step)
            except Exception:
                _retrieve_provider_error(instance)
                if project.debug:
                    instance.shell()
                else:
                    echo.warning(
                        "Run the same command again with --debug to shell into the environment "
                        "if you wish to introspect this failure.")
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    return project
Exemple #14
0
def _execute(  # noqa: C901
    step: steps.Step,
    parts: str,
    pack_project: bool = False,
    output: str = None,
    shell: bool = False,
    shell_after: bool = False,
    **kwargs
) -> "Project":
    # fmt: on
    if sys.platform == "darwin":
        default_provider = "multipass"
    else:
        default_provider = "host"

    build_environment = env.BuilderEnvironmentConfig(default=default_provider)
    project = get_project(is_managed_host=build_environment.is_managed_host, **kwargs)

    conduct_project_sanity_check(project)

    #  When we are ready to pull the trigger we will trigger this when
    # project.info.base is set
    if build_environment.is_multipass:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider
        )
        echo.info("Launching a VM.")
        with build_provider_class(project=project, echoer=echo) as instance:
            instance.mount_project()
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                else:
                    instance.execute_step(step)
            except Exception:
                if project.debug:
                    instance.shell()
                else:
                    echo.warning("Run the same command again with --debug to shell into the environment "
                                 "if you wish to introspect this failure.")
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    elif build_environment.is_managed_host or build_environment.is_host:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    else:
        # containerbuild takes a snapcraft command name, not a step
        lifecycle.containerbuild(command=step.name, project=project, args=parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    return project
Exemple #15
0
def _execute(  # noqa: C901
        step: steps.Step,
        parts: str,
        pack_project: bool = False,
        output: str = None,
        shell: bool = False,
        shell_after: bool = False,
        destructive_mode: bool = False,
        **kwargs) -> "Project":
    # fmt: on
    _clean_provider_error()
    provider = "host" if destructive_mode else None
    build_environment = env.BuilderEnvironmentConfig(force_provider=provider)
    try:
        conduct_build_environment_sanity_check(build_environment.provider)
    except MultipassMissingInstallableError as e:
        click.echo("You need multipass installed to build snaps "
                   "(https://github.com/CanonicalLtd/multipass).")
        if click.confirm("Would you like to install it now?"):
            _install_multipass()
        else:
            raise errors.SnapcraftEnvironmentError(
                "multipass is required to continue.") from e

    project = get_project(is_managed_host=build_environment.is_managed_host,
                          **kwargs)

    conduct_project_sanity_check(project)

    if build_environment.is_managed_host or build_environment.is_host:
        project_config = project_loader.load_config(project)
        lifecycle.execute(step, project_config, parts)
        if pack_project:
            _pack(project.prime_dir, output=output)
    else:
        build_provider_class = build_providers.get_provider_for(
            build_environment.provider)
        echo.info("Launching a VM.")
        with build_provider_class(project=project, echoer=echo) as instance:
            instance.mount_project()
            try:
                if shell:
                    # shell means we want to do everything right up to the previous
                    # step and then go into a shell instead of the requested step.
                    # the "snap" target is a special snowflake that has not made its
                    # way to be a proper step.
                    previous_step = None
                    if pack_project:
                        previous_step = steps.PRIME
                    elif step > steps.PULL:
                        previous_step = step.previous_step()
                    # steps.PULL is the first step, so we would directly shell into it.
                    if previous_step:
                        instance.execute_step(previous_step)
                elif pack_project:
                    instance.pack_project(output=output)
                else:
                    instance.execute_step(step)
            except Exception:
                _retrieve_provider_error(instance)
                if project.debug:
                    instance.shell()
                else:
                    echo.warning(
                        "Run the same command again with --debug to shell into the environment "
                        "if you wish to introspect this failure.")
                    raise
            else:
                if shell or shell_after:
                    instance.shell()
    return project