Skip to content

Commit 3f8104b

Browse files
abnSecrus
andcommitted
build: allow specifying local version label
Co-authored-by: Bartosz Sokorski <b.sokorski@gmail.com>
1 parent 342f2a9 commit 3f8104b

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

docs/cli.md

+14
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,22 @@ Note that, at the moment, only pure python wheels are supported.
548548
### Options
549549

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

554+
{{% note %}}
555+
When using `--local-version`, the identifier must be [PEP 440](https://peps.python.org/pep-0440/#local-version-identifiers)
556+
compliant. This is useful for adding build numbers, platform specificities etc. for private packages.
557+
{{% /note %}}
558+
559+
{{% warning %}}
560+
Local version identifiers SHOULD NOT be used when publishing upstream projects to a public index server, but MAY be
561+
used to identify private builds created directly from the project source.
562+
563+
See [PEP 440](https://peps.python.org/pep-0440/#local-version-identifiers) for more information.
564+
{{% /warning %}}
565+
566+
553567
## publish
554568

555569
This command publishes the package, previously built with the [`build`](#build) command, to the remote repository.

src/poetry/console/commands/build.py

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class BuildCommand(EnvCommand):
1414

1515
options = [
1616
option("format", "f", "Limit the format to either sdist or wheel.", flag=False),
17+
option(
18+
"local-version",
19+
"l",
20+
"Add or replace a local version label to the build.",
21+
flag=False,
22+
),
1723
option(
1824
"output",
1925
"o",
@@ -45,6 +51,11 @@ def _build(
4551
else:
4652
raise ValueError(f"Invalid format: {fmt}")
4753

54+
if local_version_label := self.option("local-version"):
55+
self.poetry.package.version = self.poetry.package.version.replace(
56+
local=local_version_label
57+
)
58+
4859
for builder in builders:
4960
builder(self.poetry, executable=executable).build(target_dir)
5061

tests/console/commands/test_build.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ def tmp_tester(
4141
return command_tester_factory("build", tmp_poetry)
4242

4343

44-
def get_package_glob(poetry: Poetry) -> str:
45-
return f"{poetry.package.name.replace('-', '_')}-{poetry.package.version}*"
44+
def get_package_glob(poetry: Poetry, local_version: str | None = None) -> str:
45+
version = poetry.package.version
46+
47+
if local_version:
48+
version = version.replace(local=local_version)
49+
50+
return f"{poetry.package.name.replace('-', '_')}-{version}*"
4651

4752

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

6469

70+
def test_build_with_local_version_label(
71+
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry
72+
) -> None:
73+
local_version_label = "local-version"
74+
tmp_tester.execute(f"--local-version {local_version_label}")
75+
build_artifacts = tuple(
76+
(tmp_project_path / "dist").glob(
77+
get_package_glob(tmp_poetry, local_version=local_version_label)
78+
)
79+
)
80+
81+
assert len(build_artifacts) > 0
82+
assert all(archive.exists() for archive in build_artifacts)
83+
84+
6585
def test_build_not_possible_in_non_package_mode(
6686
fixture_dir: FixtureDirGetter,
6787
command_tester_factory: CommandTesterFactory,

0 commit comments

Comments
 (0)