-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move/Rename:
goals/setup_py.py
-> `{util_rules,goals}/package_dists…
….py` + `subsystems/setup_py_generation.py` (#18702) In pants v1, there was a [`setup-py` goal](https://v1.pantsbuild.org/options_reference.html#setup-py). In pants v2, that is now part of the more generic [`package` goal](https://www.pantsbuild.org/docs/python-package-goal). So, the name `setup_py` doesn't make sense any more. Also, most of the rules in that file are used in contexts other than the `package` goal. For instance, I'm using them in #18639 in the `export` goal. And importing something from `goals` in `util_rules` feels rather odd. So, I would like to see most of these rules under `util_rules`. This change does the following: - renames: `pants.backends.python.goals.setup_py` to `pants.backends.python.util_rules.package_dists` - adds a minimal: `pants.backends.python.goals.package_dists` - moves `SetupPyGeneration(Subsystem)` to `pants.backends.python.subsystems.setup_py_generation` - adjusts imports to use the new locations as needed.
- Loading branch information
1 parent
37d49b6
commit 690e6a6
Showing
18 changed files
with
144 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
from pants.backend.python.subsystems.setup import PythonSetup | ||
from pants.backend.python.subsystems.setuptools import PythonDistributionFieldSet | ||
from pants.backend.python.util_rules.dists import DistBuildRequest, DistBuildResult | ||
from pants.backend.python.util_rules.package_dists import create_dist_build_request | ||
from pants.backend.python.util_rules.package_dists import rules as package_dists_rules | ||
from pants.core.goals.package import BuiltPackage, BuiltPackageArtifact, PackageFieldSet | ||
from pants.engine.fs import Digest, Snapshot | ||
from pants.engine.rules import Get, collect_rules, rule | ||
from pants.engine.unions import UnionMembership, UnionRule | ||
|
||
|
||
@rule | ||
async def package_python_dist( | ||
field_set: PythonDistributionFieldSet, | ||
python_setup: PythonSetup, | ||
union_membership: UnionMembership, | ||
) -> BuiltPackage: | ||
dist_build_request = await create_dist_build_request( | ||
field_set=field_set, | ||
python_setup=python_setup, | ||
union_membership=union_membership, | ||
# raises an error if both dist_tgt.wheel and dist_tgt.sdist are False | ||
validate_wheel_sdist=True, | ||
) | ||
setup_py_result = await Get(DistBuildResult, DistBuildRequest, dist_build_request) | ||
dist_snapshot = await Get(Snapshot, Digest, setup_py_result.output) | ||
return BuiltPackage( | ||
setup_py_result.output, | ||
tuple(BuiltPackageArtifact(path) for path in dist_snapshot.files), | ||
) | ||
|
||
|
||
def rules(): | ||
return [ | ||
*package_dists_rules(), | ||
*collect_rules(), | ||
UnionRule(PackageFieldSet, PythonDistributionFieldSet), | ||
] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/python/pants/backend/python/subsystems/setup_py_generation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
import enum | ||
|
||
from pants.option.option_types import BoolOption, EnumOption | ||
from pants.option.subsystem import Subsystem | ||
from pants.util.strutil import softwrap | ||
|
||
|
||
@enum.unique | ||
class FirstPartyDependencyVersionScheme(enum.Enum): | ||
EXACT = "exact" # i.e., == | ||
COMPATIBLE = "compatible" # i.e., ~= | ||
ANY = "any" # i.e., no specifier | ||
|
||
|
||
class SetupPyGeneration(Subsystem): | ||
options_scope = "setup-py-generation" | ||
help = "Options to control how setup.py is generated from a `python_distribution` target." | ||
|
||
# Generating setup is the more aggressive thing to do, so we'd prefer that the default | ||
# be False. However that would break widespread existing usage, so we'll make that | ||
# change in a future deprecation cycle. | ||
generate_setup_default = BoolOption( | ||
default=True, | ||
help=softwrap( | ||
""" | ||
The default value for the `generate_setup` field on `python_distribution` targets. | ||
Can be overridden per-target by setting that field explicitly. Set this to False | ||
if you mostly rely on handwritten setup files (setup.py, setup.cfg and similar). | ||
Leave as True if you mostly rely on Pants generating setup files for you. | ||
""" | ||
), | ||
) | ||
|
||
first_party_dependency_version_scheme = EnumOption( | ||
default=FirstPartyDependencyVersionScheme.EXACT, | ||
help=softwrap( | ||
""" | ||
What version to set in `install_requires` when a `python_distribution` depends on | ||
other `python_distribution`s. If `exact`, will use `==`. If `compatible`, will | ||
use `~=`. If `any`, will leave off the version. See | ||
https://www.python.org/dev/peps/pep-0440/#version-specifiers. | ||
""" | ||
), | ||
) | ||
|
||
def first_party_dependency_version(self, version: str) -> str: | ||
"""Return the version string (e.g. '~=4.0') for a first-party dependency. | ||
If the user specified to use "any" version, then this will return an empty string. | ||
""" | ||
scheme = self.first_party_dependency_version_scheme | ||
if scheme == FirstPartyDependencyVersionScheme.ANY: | ||
return "" | ||
specifier = "==" if scheme == FirstPartyDependencyVersionScheme.EXACT else "~=" | ||
return f"{specifier}{version}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.