Skip to content

Commit 70cce26

Browse files
authored
Refactor and separate concerns of external python package handling code (#953)
1 parent 9022291 commit 70cce26

31 files changed

+181
-119
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See https://pre-commit.com/hooks.html for more hooks
44
repos:
55
- repo: https://github.com/keith/pre-commit-buildifier
6-
rev: 5.1.0.1
6+
rev: 6.0.0
77
hooks:
88
- id: buildifier
99
args: &args

MODULE.bazel

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
module(
22
name = "rules_python",
3-
compatibility_level = 1,
43
version = "0.0.0",
4+
compatibility_level = 1,
55
)
66

77
bazel_dep(name = "platforms", version = "0.0.4")
88

99
internal_deps = use_extension("@rules_python//python:extensions.bzl", "internal_deps")
10-
1110
internal_deps.install()
12-
1311
use_repo(
1412
internal_deps,
1513
"pypi__build",

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
module(
22
name = "example_bzlmod",
3-
compatibility_level = 1,
43
version = "0.0.0",
4+
compatibility_level = 1,
55
)
66

77
bazel_dep(name = "rules_python", version = "0.0.0")
8-
98
local_path_override(
109
module_name = "rules_python",
1110
path = "../..",
1211
)
1312

1413
python = use_extension("@rules_python//python:extensions.bzl", "python")
15-
1614
python.toolchain(
1715
name = "python3_9",
1816
python_version = "3.9",
1917
)
20-
2118
use_repo(python, "python3_9_toolchains")
2219

2320
register_toolchains(
2421
"@python3_9_toolchains//:all",
2522
)
2623

2724
pip = use_extension("@rules_python//python:extensions.bzl", "pip")
28-
2925
pip.parse(
3026
name = "pip",
3127
requirements_lock = "//:requirements_lock.txt",
3228
requirements_windows = "//:requirements_windows.txt",
3329
)
34-
3530
use_repo(pip, "pip")

python/extensions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _pip_impl(module_ctx):
7878
environment = attr.environment,
7979
)
8080

81-
# Keep in sync with python/pip_install/extract_wheels/bazel.py
81+
# Keep in sync with python/pip_install/tools/bazel.py
8282
def _sanitize_name(name):
8383
return name.replace("-", "_").replace(".", "_").lower()
8484

python/pip_install/BUILD.bazel

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
exports_files(["pip_compile.py"])
2-
31
filegroup(
42
name = "distribution",
53
srcs = glob(["*.bzl"]) + [
64
"BUILD.bazel",
7-
"pip_compile.py",
8-
"//python/pip_install/extract_wheels:distribution",
5+
"//python/pip_install/tools/dependency_resolver:distribution",
6+
"//python/pip_install/tools/lib:distribution",
7+
"//python/pip_install/tools/lock_file_generator:distribution",
8+
"//python/pip_install/tools/wheel_installer:distribution",
99
"//python/pip_install/private:distribution",
1010
],
1111
visibility = ["//:__pkg__"],
@@ -22,7 +22,10 @@ filegroup(
2222
filegroup(
2323
name = "py_srcs",
2424
srcs = [
25-
"//python/pip_install/extract_wheels:py_srcs",
25+
"//python/pip_install/tools/dependency_resolver:py_srcs",
26+
"//python/pip_install/tools/lib:py_srcs",
27+
"//python/pip_install/tools/lock_file_generator:py_srcs",
28+
"//python/pip_install/tools/wheel_installer:py_srcs",
2629
],
2730
visibility = ["//python/pip_install/private:__pkg__"],
2831
)

python/pip_install/pip_repository.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def _pip_repository_impl(rctx):
307307
args = [
308308
python_interpreter,
309309
"-m",
310-
"python.pip_install.extract_wheels.parse_requirements_to_bzl",
310+
"python.pip_install.tools.lock_file_generator.lock_file_generator",
311311
"--requirements_lock",
312312
rctx.path(requirements_txt),
313313
"--requirements_lock_label",
@@ -524,7 +524,7 @@ def _whl_library_impl(rctx):
524524
args = [
525525
python_interpreter,
526526
"-m",
527-
"python.pip_install.extract_wheels.wheel_installer",
527+
"python.pip_install.tools.wheel_installer.wheel_installer",
528528
"--requirement",
529529
rctx.attr.requirement,
530530
"--repo",

python/pip_install/private/pip_install_utils.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Utilities for `rules_python` pip rules"""
22

33
_SRCS_TEMPLATE = """\
4-
\"\"\"A generate file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules
4+
\"\"\"A generated file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules
55
6-
This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.install` target. Please
6+
This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.update` target. Please
77
`bazel run` this target to apply any updates. Note that doing so will discard any local modifications.
88
"\"\"
99

python/pip_install/private/srcs.bzl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
"""A generate file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules
1+
"""A generated file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules
22
3-
This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.install` target. Please
3+
This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.update` target. Please
44
`bazel run` this target to apply any updates. Note that doing so will discard any local modifications.
55
"""
66

77
# Each source file is tracked as a target so `pip_repository` rules will know to automatically rebuild if any of the
88
# sources changed.
99
PIP_INSTALL_PY_SRCS = [
10-
"@rules_python//python/pip_install/extract_wheels:__init__.py",
11-
"@rules_python//python/pip_install/extract_wheels:annotation.py",
12-
"@rules_python//python/pip_install/extract_wheels:arguments.py",
13-
"@rules_python//python/pip_install/extract_wheels:bazel.py",
14-
"@rules_python//python/pip_install/extract_wheels:namespace_pkgs.py",
15-
"@rules_python//python/pip_install/extract_wheels:parse_requirements_to_bzl.py",
16-
"@rules_python//python/pip_install/extract_wheels:wheel.py",
17-
"@rules_python//python/pip_install/extract_wheels:wheel_installer.py",
10+
"@rules_python//python/pip_install/tools/dependency_resolver:__init__.py",
11+
"@rules_python//python/pip_install/tools/dependency_resolver:dependency_resolver.py",
12+
"@rules_python//python/pip_install/tools/lib:__init__.py",
13+
"@rules_python//python/pip_install/tools/lib:annotation.py",
14+
"@rules_python//python/pip_install/tools/lib:arguments.py",
15+
"@rules_python//python/pip_install/tools/lib:bazel.py",
16+
"@rules_python//python/pip_install/tools/lock_file_generator:__init__.py",
17+
"@rules_python//python/pip_install/tools/lock_file_generator:lock_file_generator.py",
18+
"@rules_python//python/pip_install/tools/wheel_installer:namespace_pkgs.py",
19+
"@rules_python//python/pip_install/tools/wheel_installer:wheel.py",
20+
"@rules_python//python/pip_install/tools/wheel_installer:wheel_installer.py",
1821
]

python/pip_install/repositories.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ py_library(
8989
srcs = glob(["**/*.py"]),
9090
data = glob(["**/*"], exclude=[
9191
# These entries include those put into user-installed dependencies by
92-
# data_exclude in /python/pip_install/extract_wheels/bazel.py
92+
# data_exclude in /python/pip_install/tools/bazel.py
9393
# to avoid non-determinism following pip install's behavior.
9494
"**/*.py",
9595
"**/*.pyc",

python/pip_install/requirements.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def compile_pip_requirements(
5757

5858
# Use the Label constructor so this is expanded in the context of the file
5959
# where it appears, which is to say, in @rules_python
60-
pip_compile = Label("//python/pip_install:pip_compile.py")
60+
pip_compile = Label("//python/pip_install/tools/dependency_resolver:dependency_resolver.py")
6161

6262
loc = "$(rootpath {})"
6363

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exports_files(["dependency_resolver.py"])
2+
3+
filegroup(
4+
name = "distribution",
5+
srcs = glob(
6+
["*"],
7+
exclude = ["*_test.py"],
8+
),
9+
visibility = ["//python/pip_install:__subpackages__"],
10+
)
11+
12+
filegroup(
13+
name = "py_srcs",
14+
srcs = glob(
15+
include = ["**/*.py"],
16+
exclude = ["**/*_test.py"],
17+
),
18+
visibility = ["//python/pip_install:__subpackages__"],
19+
)

python/pip_install/extract_wheels/BUILD.bazel renamed to python/pip_install/tools/lib/BUILD.bazel

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
2-
load("//python/pip_install:repositories.bzl", "requirement")
1+
load("//python:defs.bzl", "py_library", "py_test")
32
load(":annotations_test_helpers.bzl", "package_annotation", "package_annotations_file")
43

54
py_library(
@@ -8,31 +7,8 @@ py_library(
87
"annotation.py",
98
"arguments.py",
109
"bazel.py",
11-
"namespace_pkgs.py",
12-
"parse_requirements_to_bzl.py",
13-
"wheel.py",
14-
"wheel_installer.py",
1510
],
16-
deps = [
17-
requirement("installer"),
18-
requirement("setuptools"),
19-
],
20-
)
21-
22-
py_binary(
23-
name = "wheel_installer",
24-
srcs = [
25-
"wheel_installer.py",
26-
],
27-
deps = [":lib"],
28-
)
29-
30-
py_binary(
31-
name = "parse_requirements_to_bzl",
32-
srcs = [
33-
"parse_requirements_to_bzl.py",
34-
],
35-
deps = [":lib"],
11+
visibility = ["//python/pip_install:__subpackages__"],
3612
)
3713

3814
package_annotations_file(
@@ -70,57 +46,18 @@ py_test(
7046
srcs = ["annotations_test.py"],
7147
data = [":mock_annotations"],
7248
env = {"MOCK_ANNOTATIONS": "$(rootpath :mock_annotations)"},
73-
tags = ["unit"],
7449
deps = [
7550
":lib",
7651
"//python/runfiles",
7752
],
7853
)
7954

80-
py_test(
81-
name = "namespace_pkgs_test",
82-
size = "small",
83-
srcs = [
84-
"namespace_pkgs_test.py",
85-
],
86-
tags = ["unit"],
87-
deps = [
88-
":lib",
89-
],
90-
)
91-
92-
py_test(
93-
name = "wheel_installer_test",
94-
size = "small",
95-
srcs = [
96-
"wheel_installer_test.py",
97-
],
98-
data = ["//examples/wheel:minimal_with_py_package"],
99-
tags = ["unit"],
100-
deps = [
101-
":lib",
102-
],
103-
)
104-
10555
py_test(
10656
name = "arguments_test",
10757
size = "small",
10858
srcs = [
10959
"arguments_test.py",
11060
],
111-
tags = ["unit"],
112-
deps = [
113-
":lib",
114-
],
115-
)
116-
117-
py_test(
118-
name = "parse_requirements_to_bzl_test",
119-
size = "small",
120-
srcs = [
121-
"parse_requirements_to_bzl_test.py",
122-
],
123-
tags = ["unit"],
12461
deps = [
12562
":lib",
12663
],

python/pip_install/tools/lib/__init__.py

Whitespace-only changes.

python/pip_install/extract_wheels/annotations_test.py renamed to python/pip_install/tools/lib/annotations_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import unittest
66
from pathlib import Path
77

8-
from python.pip_install.extract_wheels.annotation import Annotation, AnnotationsMap
8+
from python.pip_install.tools.lib.annotation import Annotation, AnnotationsMap
99
from python.runfiles import runfiles
1010

1111

python/pip_install/extract_wheels/annotations_test_helpers.bzl renamed to python/pip_install/tools/lib/annotations_test_helpers.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Helper macros and rules for testing the `annotations` module of `extract_wheels`"""
1+
"""Helper macros and rules for testing the `annotations` module of `tools`"""
22

33
load("//python:pip.bzl", _package_annotation = "package_annotation")
44

python/pip_install/extract_wheels/arguments_test.py renamed to python/pip_install/tools/lib/arguments_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import unittest
44

5-
from python.pip_install.extract_wheels import arguments
5+
from python.pip_install.tools.lib import arguments
66

77

88
class ArgumentsTestCase(unittest.TestCase):
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
load("//python:defs.bzl", "py_binary", "py_library", "py_test")
2+
load("//python/pip_install:repositories.bzl", "requirement")
3+
4+
py_library(
5+
name = "lib",
6+
srcs = [
7+
"lock_file_generator.py",
8+
],
9+
deps = [
10+
"//python/pip_install/tools/lib",
11+
requirement("pip"),
12+
],
13+
)
14+
15+
py_binary(
16+
name = "lock_file_generator",
17+
srcs = [
18+
"lock_file_generator.py",
19+
],
20+
deps = [":lib"],
21+
)
22+
23+
py_test(
24+
name = "lock_file_generator_test",
25+
size = "small",
26+
srcs = [
27+
"lock_file_generator_test.py",
28+
],
29+
deps = [
30+
":lib",
31+
],
32+
)
33+
34+
filegroup(
35+
name = "distribution",
36+
srcs = glob(
37+
["*"],
38+
exclude = ["*_test.py"],
39+
),
40+
visibility = ["//python/pip_install:__subpackages__"],
41+
)
42+
43+
filegroup(
44+
name = "py_srcs",
45+
srcs = glob(
46+
include = ["**/*.py"],
47+
exclude = ["**/*_test.py"],
48+
),
49+
visibility = ["//python/pip_install:__subpackages__"],
50+
)

python/pip_install/tools/lock_file_generator/__init__.py

Whitespace-only changes.

python/pip_install/extract_wheels/parse_requirements_to_bzl.py renamed to python/pip_install/tools/lock_file_generator/lock_file_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from pip._internal.req.req_install import InstallRequirement
1818

19-
from python.pip_install.extract_wheels import annotation, arguments, bazel
19+
from python.pip_install.tools.lib import annotation, arguments, bazel
2020

2121

2222
def parse_install_requirements(

0 commit comments

Comments
 (0)