|
| 1 | +# |
| 2 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +""" |
| 6 | +Module exposing the format commands. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +import asyncclick as click |
| 13 | +from packaging import version |
| 14 | +from pipelines.airbyte_ci.steps.python_registry import PublishToPythonRegistry |
| 15 | +from pipelines.cli.confirm_prompt import confirm |
| 16 | +from pipelines.cli.dagger_pipeline_command import DaggerPipelineCommand |
| 17 | +from pipelines.consts import DEFAULT_PYTHON_PACKAGE_REGISTRY_URL |
| 18 | +from pipelines.models.contexts.click_pipeline_context import ClickPipelineContext, pass_pipeline_context |
| 19 | +from pipelines.models.contexts.python_registry_publish import PythonRegistryPublishContext |
| 20 | +from pipelines.models.steps import StepStatus |
| 21 | + |
| 22 | + |
| 23 | +async def _has_metadata_yaml(context: PythonRegistryPublishContext) -> bool: |
| 24 | + dir_to_publish = context.get_repo_dir(context.package_path) |
| 25 | + return "metadata.yaml" in await dir_to_publish.entries() |
| 26 | + |
| 27 | + |
| 28 | +def _validate_python_version(_ctx: dict, _param: dict, value: Optional[str]) -> Optional[str]: |
| 29 | + """ |
| 30 | + Check if an given version is valid. |
| 31 | + """ |
| 32 | + if value is None: |
| 33 | + return value |
| 34 | + try: |
| 35 | + version.Version(value) |
| 36 | + return value |
| 37 | + except version.InvalidVersion: |
| 38 | + raise click.BadParameter(f"Version {value} is not a valid version.") |
| 39 | + |
| 40 | + |
| 41 | +@click.command(cls=DaggerPipelineCommand, name="publish", help="Publish a Python package to a registry.") |
| 42 | +@click.option( |
| 43 | + "--python-registry-token", |
| 44 | + help="Access token", |
| 45 | + type=click.STRING, |
| 46 | + required=True, |
| 47 | + envvar="PYTHON_REGISTRY_TOKEN", |
| 48 | +) |
| 49 | +@click.option( |
| 50 | + "--registry-url", |
| 51 | + help="Which registry to publish to. If not set, the default pypi is used. For test pypi, use https://test.pypi.org/legacy/", |
| 52 | + type=click.STRING, |
| 53 | + default=DEFAULT_PYTHON_PACKAGE_REGISTRY_URL, |
| 54 | +) |
| 55 | +@click.option( |
| 56 | + "--publish-name", |
| 57 | + help="The name of the package to publish. If not set, the name will be inferred from the pyproject.toml file of the package.", |
| 58 | + type=click.STRING, |
| 59 | +) |
| 60 | +@click.option( |
| 61 | + "--publish-version", |
| 62 | + help="The version of the package to publish. If not set, the version will be inferred from the pyproject.toml file of the package.", |
| 63 | + type=click.STRING, |
| 64 | + callback=_validate_python_version, |
| 65 | +) |
| 66 | +@pass_pipeline_context |
| 67 | +@click.pass_context |
| 68 | +async def publish( |
| 69 | + ctx: click.Context, |
| 70 | + click_pipeline_context: ClickPipelineContext, |
| 71 | + python_registry_token: str, |
| 72 | + registry_url: str, |
| 73 | + publish_name: Optional[str], |
| 74 | + publish_version: Optional[str], |
| 75 | +) -> bool: |
| 76 | + context = PythonRegistryPublishContext( |
| 77 | + is_local=ctx.obj["is_local"], |
| 78 | + git_branch=ctx.obj["git_branch"], |
| 79 | + git_revision=ctx.obj["git_revision"], |
| 80 | + ci_report_bucket=ctx.obj["ci_report_bucket_name"], |
| 81 | + report_output_prefix=ctx.obj["report_output_prefix"], |
| 82 | + gha_workflow_run_url=ctx.obj.get("gha_workflow_run_url"), |
| 83 | + dagger_logs_url=ctx.obj.get("dagger_logs_url"), |
| 84 | + pipeline_start_timestamp=ctx.obj.get("pipeline_start_timestamp"), |
| 85 | + ci_context=ctx.obj.get("ci_context"), |
| 86 | + ci_gcs_credentials=ctx.obj["ci_gcs_credentials"], |
| 87 | + python_registry_token=python_registry_token, |
| 88 | + registry=registry_url, |
| 89 | + package_path=ctx.obj["package_path"], |
| 90 | + package_name=publish_name, |
| 91 | + version=publish_version, |
| 92 | + ) |
| 93 | + |
| 94 | + dagger_client = await click_pipeline_context.get_dagger_client(pipeline_name=f"Publish {ctx.obj['package_path']} to python registry") |
| 95 | + context.dagger_client = dagger_client |
| 96 | + |
| 97 | + if await _has_metadata_yaml(context): |
| 98 | + confirm( |
| 99 | + "It looks like you are trying to publish a connector. In most cases, the `connectors` command group should be used instead. Do you want to continue?", |
| 100 | + abort=True, |
| 101 | + ) |
| 102 | + |
| 103 | + publish_result = await PublishToPythonRegistry(context).run() |
| 104 | + |
| 105 | + return publish_result.status is StepStatus.SUCCESS |
0 commit comments