Ejemplo n.º 1
0
def list(
    filter: Optional[str] = typer.Option(
        None, help="Optional string to use as filter when looking for package names"
    )
) -> None:
    """List packages in the monorepo"""
    projects = Table(title="Projects")
    projects.add_column("Name")
    projects.add_column("Directory")
    projects.add_column("Description")
    for project in repo.get_packages():
        projects.add_row(
            project.pyproject.name,
            str(project.root.relative_to(Path.cwd())),
            project.pyproject.description,
        )
    console.print(projects)
Ejemplo n.º 2
0
def install_deps(become: bool = typer.Option(
    False,
    "-b",
    "--become",
    help="perform installation as root user using sudo")) -> None:
    """Install release dependencies"""
    deps = [
        "semantic-release",
        "@semantic-release/commit-analyzer",
        "@semantic-release/changelog",
        "@semantic-release/exec",
        "conventional-changelog-conventionalcommits",
    ]
    cmd = "npm i -g " + " ".join(deps)
    if become:
        cmd = "sudo " + cmd
    console.print(f"Running command: {quote(style_str(cmd, 'bold'))}")
    run(cmd)
Ejemplo n.º 3
0
def ensure_config() -> Iterator[pathlib.Path]:
    """A context manager to make sure a semantic release config is present in the directory."""
    # The configuration must be located in the root directory of the project
    config = pathlib.Path.cwd() / "release.config.js"
    is_default = False
    # If it does not exist we copy the default config
    if not config.exists():
        is_default = True
        shutil.copy2(
            pathlib.Path(__file__).parent.parent / "defaults" /
            "release.config.js",
            config,
        )
    # We print the config as debug
    try:
        console.print("Using config: ", config.read_text())
        # Yield the config so that it is returned by the context manager
        yield config
    finally:
        # Only remove the file is the default
        if is_default:
            config.unlink(missing_ok=True)
Ejemplo n.º 4
0
def run_prof(
    script: Optional[str] = typer.Argument(None, help="Script to run"),
    module: Optional[str] = typer.Option(None,
                                         "-m",
                                         "--module",
                                         help="Module to run"),
    output: str = typer.Option(None, "-o", "--output", help="Output file"),
) -> None:
    out_opt = f" -o {output}" if output else ""
    if module:
        rest = " -m " + module
    elif script:
        rest = " " + quote(script)
        script = script
    else:
        console.print(
            "Wrong usage: Either [bold]-m[/bold]/[bold]--module[/bold] option or a [bold]positional argument[/bold] must be present",
            style="red",
        )
        raise typer.Exit(1)
    cmd = f"python -m cProfile {out_opt} {rest}"
    console.print(f"[bold blue]Profiling[/bold blue] with command: {cmd}")
    run(cmd)
Ejemplo n.º 5
0
def build(
    ctx: Context,
    name: Optional[str] = Argument(None, help="Name of images to build"),
    builder_file: Optional[str] = Option(
        None,
        "--builder-file",
        "-b",
        help="builder.yml file to use. By default files are searched accross repo",
    ),
    *,
    context: Optional[Path] = Option(
        None, "--context", "-c", help="Docker context to consider when performing build"
    ),
    file: Optional[Path] = Option(
        None,
        "--dockerfile",
        "--file",
        "-f",
        help="Custom Dockerfile to use when building image",
    ),
    tags: Optional[List[str]] = Option(
        None, "-t", "--tags", help="Custom tags to give to produced image"
    ),
    labels: Optional[List[str]] = Option(
        None, "-l", "--label", help="Additional labels to give to the produced image"
    ),
    platforms: Optional[List[str]] = Option(
        None, "-p", "--platform", help="Platforms to build the image for"
    ),
    add_hosts: Optional[List[str]] = Option(
        None,
        "-h",
        "--add-host",
        help="Add known hosts entries into the generated docker image",
    ),
    push: bool = Option(False, "--push", help="Push the image to registry after build"),
    load: bool = Option(
        False, "--load", help="Load the image into local docker engine after build"
    ),
    dump: bool = Option(
        False,
        "--dump",
        help="Dump the image filesystem into the current directory after build",
    ),
    build_args: Optional[List[str]] = Option(
        None, "--build-args", help="Additional build arguments"
    ),
    builder: Optional[str] = Option(
        None,
        "--builder",
        help="Custom builder to use (see --builder option for 'docker buildx build' command)",
    ),
    cache: bool = Option(
        True, "--cache", help="Cache generated layers to speed up build"
    ),
    cache_from: Optional[str] = Option(
        None,
        "--cache-from",
        help="Reuse cache from given location. Can be a remote docker image",
    ),
    cache_to: Optional[str] = Option(
        None,
        "--cache-to",
        help="Store intermediate layer and produced cache into given destination",
    ),
    network: Optional[str] = Option(
        None, "--network", help="Use a specific network mode during build"
    ),
    output: Optional[str] = Option(
        None, "--output", help="Custom output for 'docker buildx build' command"
    ),
    progress: str = Option("auto", "--progress", help="Progress display mode"),
    pull: bool = Option(False, "--pull", help="Always pull images before build"),
    secrets: Optional[List[str]] = Option(
        None, "--secret", help="Secrets to mount during build"
    ),
    # Don't know what those two options are for
    allow: Optional[List[str]] = None,
    ssh: Optional[str] = None,
    target: Optional[str] = None,
) -> None:
    """Build a docker image."""
    if builder_file:
        try:
            images = [Image.from_file(builder_file)]
        except ValidationError as err:
            console.print(err, style="red")
            exit(1)
    elif name:
        catalog = Catalog.from_directory(Path.cwd())
        try:
            images = [next(image for image in catalog.images if image.name == name)]
        except StopIteration:
            console.print(
                f"Build config for image {style_str(name, 'bold')} does not exist",
                style="red",
            )
            exit(1)
    else:
        try:
            images = Catalog.from_directory(Path.cwd()).images
        except ValidationError as err:
            console.print(err, style="red")
            exit(1)

    _add_hosts = map_string_to_dict(add_hosts)
    _labels = map_string_to_dict(labels or [])
    names = [tag for tag in tags] if tags else []

    is_key = True
    kwargs = {}
    for extra_arg in ctx.args:
        if is_key:
            key = extra_arg
            if key.startswith("--"):
                key = key[2:]
            elif key.startswith("-"):
                key = key[1:]
            if "=" in extra_arg:
                key, value = key.split("=")
                key = key.replace("-", "_").upper()
                kwargs[key] = value
                is_key = True
                continue
            else:
                is_key = False
                continue
        key = key.replace("-", "_").upper()
        kwargs[key] = extra_arg
        is_key = True
    _user_build_args = map_string_to_dict(build_args or [])
    _user_build_args = {**_user_build_args, **kwargs}

    if dump:
        _output = {"type": "local", "dest": "."}
    elif output:
        _output = map_string_to_dict(output)
    else:
        _output = {}

    if progress.lower() in ("0", "false", "no", "n"):
        _progress: Union[bool, str] = False
    else:
        _progress = progress

    # TODO: Is there some order ?
    for image in images:
        _build_args = {
            arg.name.upper(): arg.default
            for arg in image.build.build_args
            if arg.default
        }

        _build_args.update(
            {key.upper(): value for key, value in _user_build_args.items()}
        )
        _names = names or [image.get_name()]

        logger.debug(f"Building image {_names[0]} with build arguments: {_build_args}")
        if len(_names) > 1:
            for name in _names[1:]:
                logger.debug(f"Using additional tag: {name}")

        build_context = BuildContext(
            context_path=context or image.build.context,
            add_hosts=_add_hosts or image.build.add_hosts,
            allow=list(allow or []),
            build_args=_build_args,
            builder=builder,
            cache=cache,
            cache_from=cache_from,
            cache_to=cache_to,
            file=file or image.build.file,
            labels={**image.labels, **_labels},
            load=load,
            network=network,
            output=_output,
            platforms=list(platforms or []) or image.platforms,
            progress=_progress,
            pull=pull,
            push=push,
            secrets=secrets,
            ssh=ssh,
            tags=_names,
            target=target,
        )
        docker.buildx.build(**build_context.dict())
Ejemplo n.º 6
0
def _callback(value: bool) -> None:
    if value:
        version = get_version("kapla-cli-core")
        console.print(version)
        raise typer.Exit(0)