Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: allow specifying local version label #9064

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,22 @@ Note that, at the moment, only pure python wheels are supported.
### Options

* `--format (-f)`: Limit the format to either `wheel` or `sdist`.
* `--local-version (-l)`: Add or replace a local version label to the build.
* `--output (-o)`: Set output directory for build artifacts. Default is `dist`.

{{% note %}}
When using `--local-version`, the identifier must be [PEP 440](https://peps.python.org/pep-0440/#local-version-identifiers)
compliant. This is useful for adding build numbers, platform specificities etc. for private packages.
{{% /note %}}

{{% warning %}}
Local version identifiers SHOULD NOT be used when publishing upstream projects to a public index server, but MAY be
used to identify private builds created directly from the project source.

See [PEP 440](https://peps.python.org/pep-0440/#local-version-identifiers) for more information.
{{% /warning %}}


## publish

This command publishes the package, previously built with the [`build`](#build) command, to the remote repository.
Expand Down
11 changes: 11 additions & 0 deletions src/poetry/console/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class BuildCommand(EnvCommand):

options = [
option("format", "f", "Limit the format to either sdist or wheel.", flag=False),
option(
"local-version",
"l",
"Add or replace a local version label to the build.",
flag=False,
),
option(
"output",
"o",
Expand Down Expand Up @@ -45,6 +51,11 @@ def _build(
else:
raise ValueError(f"Invalid format: {fmt}")

if local_version_label := self.option("local-version"):
self.poetry.package.version = self.poetry.package.version.replace(
local=local_version_label
)

for builder in builders:
builder(self.poetry, executable=executable).build(target_dir)

Expand Down
24 changes: 22 additions & 2 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ def tmp_tester(
return command_tester_factory("build", tmp_poetry)


def get_package_glob(poetry: Poetry) -> str:
return f"{poetry.package.name.replace('-', '_')}-{poetry.package.version}*"
def get_package_glob(poetry: Poetry, local_version: str | None = None) -> str:
version = poetry.package.version

if local_version:
version = version.replace(local=local_version)

return f"{poetry.package.name.replace('-', '_')}-{version}*"


def test_build_format_is_not_valid(tmp_tester: CommandTester) -> None:
Expand All @@ -62,6 +67,21 @@ def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
assert all(archive.exists() for archive in build_artifacts)


def test_build_with_local_version_label(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry
) -> None:
local_version_label = "local-version"
tmp_tester.execute(f"--local-version {local_version_label}")
build_artifacts = tuple(
(tmp_project_path / "dist").glob(
get_package_glob(tmp_poetry, local_version=local_version_label)
)
)

assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)


def test_build_not_possible_in_non_package_mode(
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
Expand Down
Loading