8000 feat(py_wheel): Normalize name and version by vonschultz · Pull Request #1331 · bazel-contrib/rules_python · GitHub
[go: up one dir, main page]

Skip to content

feat(py_wheel): Normalize name and version #1331

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

Merged
merged 32 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6855cd6
feat(py_wheel)!: Normalize name and version
vonschultz Jul 20, 2023
f6695f7
Handle placeholders in wheelmaker.py
vonschultz Aug 8, 2023
0705d10
refactor: move normalization into a separate file
aignas Aug 27, 2023
d014bc6
refactor: move helper nested defs to be private defs
aignas Aug 27, 2023
1c79b26
refactor: move version and contexts to a self struct
aignas Aug 27, 2023
a9d06f0
refactor: create a normalize function
aignas Aug 27, 2023
df2f124
refactor: move open_context to be a method on the normalizer struct
aignas Aug 27, 2023
1355341
refactor: move more predicates outside
aignas Aug 27, 2023
0a93c81
refactor: move close_context to self.close_context
aignas Aug 27, 2023
9ed69be
refactor: move accept to self.accept
aignas Aug 27, 2023
7f80890
refactor: move all of the methods to be functions instead
aignas Aug 27, 2023
f4be867
refactor: restrict usage of contexts to the internal parser functions
aignas Aug 27, 2023
efc66d4
refactor: move accept back to be a regular function
aignas Aug 27, 2023
d2f6b74
refactor: rename context -> ctx
aignas Aug 27, 2023
03f6348
refactor: rename tail -> context
aignas Aug 27, 2023
dca427c
refactor: drop parameters from open_context
aignas Aug 27, 2023
fcfff02
refactor: rename pop -> pop_context
aignas Aug 27, 2023
ac27a62
refactor: rename version -> input in the parser
aignas Aug 27, 2023
872a648
refactor: rename pop_context -> discard
aignas Aug 27, 2023
4237439
refactor: rename close_context -> accept
aignas Aug 27, 2023
1076fd7
refactor: use parser.discard and parser.accept methods
aignas Aug 27, 2023
6816bd4
Merge pull request #1 from aignas/pep440-suggestions
vonschultz Aug 28, 2023
2fb9457
Make pep440 file accessible by docs targets
vonschultz Aug 28, 2023
2728e9f
Fix branch with missing return in accept_epoch
vonschultz Aug 28, 2023
2e57719
Documentation fixes according to buildifier-lint
vonschultz Aug 28, 2023
cd3f905
Put load of normalize_pep440 on a single line
vonschultz Aug 28, 2023
b2756dc
Add feature flags normalizing version or name
vonschultz Oct 3, 2023
943be1b
Add feature flags to documentation
vonschultz Oct 3, 2023
74e8702
Merge remote-tracking branch 'origin/main' into pep440
vonschultz Oct 3, 2023
88c549b
Update "incompatible_normalize_name" doc string
vonschultz Oct 4, 2023
ea41790
Update feature flag description in documentation
vonschultz Oct 4, 2023
08d555b
Add an entry to the change log
vonschultz Oct 5, 2023
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ A brief description of the categories of changes:
authentication against private HTTP hosts serving Python toolchain binaries.
* `//python:packaging_bzl` added, a `bzl_library` for the Starlark
files `//python:packaging.bzl` requires.
* (py_wheel) Added the `incompatible_normalize_name` feature flag to
normalize the package distribution name according to latest Python
packaging standards. Defaults to `False` for the time being.
* (py_wheel) Added the `incompatible_normalize_version` feature flag
to normalize the package version according to PEP440 standard. This
also adds support for local version specifiers (versions with a `+`
in them), in accordance with PEP440. Defaults to `False` for the
time being.

### Removed

Expand Down
7 changes: 5 additions & 2 deletions docs/packaging.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion examples/wheel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ py_wheel(
testonly = True, # Set this to verify the generated .dist target doesn't break things
# Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
distribution = "example_minimal_library",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
version = "0.0.1",
deps = [
Expand All @@ -76,6 +78,8 @@ py_wheel(
testonly = True,
abi = "$(ABI)",
distribution = "example_minimal_library",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "$(PYTHON_TAG)",
toolchains = ["//examples/wheel:make_variable_tags"],
version = "$(VERSION)",
Expand All @@ -95,6 +99,8 @@ py_wheel(
name = "minimal_with_py_library_with_stamp",
# Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
distribution = " 8000 ;example_minimal_library{BUILD_USER}",
incompatible_normalize_name = False,
incompatible_normalize_version = False,
python_tag = "py3",
stamp = 1,
version = "0.1.{BUILD_TIMESTAMP}",
Expand Down Expand Up @@ -123,6 +129,8 @@ py_wheel(
name = "minimal_with_py_package",
# Package data. We're building "example_minimal_package-0.0.1-py3-none-any.whl"
distribution = "example_minimal_package",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
version = "0.0.1",
deps = [":example_pkg"],
Expand Down Expand Up @@ -156,6 +164,8 @@ py_wheel(
"//examples/wheel:README.md": "README",
},
homepage = "www.example.com",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
license = "Apache 2.0",
project_urls = {
"Bug Tracker": "www.example.com/issues",
Expand All @@ -177,6 +187,8 @@ py_wheel(
entry_points = {
"console_scripts": ["main = foo.bar:baz"],
},
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
strip_path_prefixes = [
"examples",
Expand All @@ -191,6 +203,8 @@ py_wheel(
name = "custom_package_root_multi_prefix",
# Package data. We're building "custom_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl"
distribution = "example_custom_package_root_multi_prefix",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
strip_path_prefixes = [
"examples/wheel/lib",
Expand All @@ -206,6 +220,8 @@ py_wheel(
name = "custom_package_root_multi_prefix_reverse_order",
# Package data. We're building "custom_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl"
distribution = "example_custom_package_root_multi_prefix_reverse_order",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
strip_path_prefixes = [
"examples/wheel",
Expand All @@ -220,6 +236,8 @@ py_wheel(
py_wheel(
name = "python_requires_in_a_package",
distribution = "example_python_requires_in_a_package",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_requires = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
python_tag = "py3",
version = "0.0.1",
Expand All @@ -231,6 +249,8 @@ py_wheel(
py_wheel(
name = "use_rule_with_dir_in_outs",
distribution = "use_rule_with_dir_in_outs",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
version = "0.0.1",
deps = [
Expand All @@ -244,6 +264,8 @@ py_wheel(
name = "python_abi3_binary_wheel",
abi = "abi3",
distribution = "example_python_abi3_binary_wheel",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
# these platform strings must line up with test_python_abi3_binary_wheel() in wheel_test.py
platform = select({
":aarch64-apple-darwin": "macosx_11_0_arm64",
Expand All @@ -258,16 +280,32 @@ py_wheel(
)

py_wheel(
name = "filename_escaping",
name = "legacy_filename_escaping",
# Per https://www.python.org/dev/peps/pep-0427/#escaping-and-unicode
# runs of non-alphanumeric, non-digit symbols should be replaced with a single underscore.
# Unicode non-ascii letters should *not* be replaced with underscore.
distribution = "file~~name-escaping",
incompatible_normalize_name = False,
incompatible_normalize_version = False,
python_tag = "py3",
version = "0.0.1-r7",
deps = [":example_pkg"],
)

py_wheel(
name = "filename_escaping",
# Per https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode
# runs of "-", "_" and "." should be replaced with a single underscore.
# Unicode non-ascii letters aren't allowed according to
# https://packaging.python.org/en/latest/specifications/name-normalization/.
distribution = "File--Name-Escaping",
incompatible_normalize_name = True,
incompatible_normalize_version = True,
python_tag = "py3",
version = "v0.0.1.RC1+ubuntu-r7",
deps = [":example_pkg"],
)

py_test(
name = "wheel_test",
srcs = ["wheel_test.py"],
Expand All @@ -277,6 +315,7 @@ py_test(
":custom_package_root_multi_prefix_reverse_order",
":customized",
":filename_escaping",
":legacy_filename_escaping",
":minimal_with_py_library",
":minimal_with_py_library_with_stamp",
":minimal_with_py_package",
Expand Down
52 changes: 45 additions & 7 deletions examples/wheel/wheel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,51 @@ def test_customized_wheel(self):
second = second.main:s""",
)

def test_legacy_filename_escaping(self):
filename = os.path.join(
os.environ['TEST_SRCDIR'],
'rules_python',
'examples',
'wheel',
'file_name_escaping-0.0.1_r7-py3-none-any.whl',
)
with zipfile.ZipFile(filename) as zf:
self.assertEquals(
zf.namelist(),
[
'examples/wheel/lib/data.txt',
'examples/wheel/lib/module_with_data.py',
'examples/wheel/lib/simple_module.py',
'examples/wheel/main.py',
# PEP calls for replacing only in the archive filename.
# Alas setuptools also escapes in the dist-info directory
# name, so let's be compatible.
'file_name_escaping-0.0.1_r7.dist-info/WHEEL',
'file_name_escaping-0.0.1_r7.dist-info/METADATA',
'file_name_escaping-0.0.1_r7.dist-info/RECORD',
],
)
metadata_contents = zf.read(
'file_name_escaping-0.0.1_r7.dist-info/METADATA'
)
self.assertEquals(
metadata_contents,
b"""\
Metadata-Version: 2.1
Name: file~~name-escaping
Version: 0.0.1-r7

UNKNOWN
""",
)

def test_filename_escaping(self):
filename = os.path.join(
os.environ["TEST_SRCDIR"],
"rules_python",
"examples",
"wheel",
"file_name_escaping-0.0.1_r7-py3-none-any.whl",
"file_name_escaping-0.0.1rc1+ubuntu.r7-py3-none-any.whl",
)
with zipfile.ZipFile(filename) as zf:
self.assertEqual(
Expand All @@ -172,20 +210,20 @@ def test_filename_escaping(self):
# PEP calls for replacing only in the archive filename.
# Alas setuptools also escapes in the dist-info directory
# name, so let's be compatible.
"file_name_escaping-0.0.1_r7.dist-info/WHEEL",
"file_name_escaping-0.0.1_r7.dist-info/METADATA",
"file_name_escaping-0.0.1_r7.dist-info/RECORD",
"file_name_escaping-0.0.1rc1+ubuntu.r7.dist-info/WHEEL",
"file_name_escaping-0.0.1rc1+ubuntu.r7.dist-info/METADATA",
"file_name_escaping-0.0.1rc1+ubuntu.r7.dist-info/RECORD",
],
)
metadata_contents = zf.read(
"file_name_escaping-0.0.1_r7.dist-info/METADATA"
"file_name_escaping-0.0.1rc1+ubuntu.r7.dist-info/METADATA"
)
self.assertEqual(
metadata_contents,
b"""\
Metadata-Version: 2.1
Name: file~~name-escaping
Version: 0.0.1-r7
Name: File--Name-Escaping
Version: 0.0.1rc1+ubuntu.r7

UNKNOWN
""",
Expand Down
1 change: 1 addition & 0 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bzl_library(
":py_binary_bzl",
"//python/private:py_package.bzl",
"//python/private:py_wheel_bzl",
"//python/private:py_wheel_normalize_pep440.bzl",
"//python/private:stamp_bzl",
"//python/private:util_bzl",
],
Expand Down
1 change: 1 addition & 0 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ exports_files(
"coverage.patch",
"py_package.bzl",
"py_wheel.bzl",
"py_wheel_normalize_pep440.bzl",
"reexports.bzl",
"stamp.bzl",
"util.bzl",
Expand Down
Loading
0