Skip to content

Commit

Permalink
build: allow specifying local version label
Browse files Browse the repository at this point in the history
Co-authored-by: Bartosz Sokorski <[email protected]>
  • Loading branch information
abn and Secrus committed Feb 29, 2024
1 parent 342f2a9 commit 104dc1c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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

0 comments on commit 104dc1c

Please sign in to comment.