Skip to content

Commit 27d0c7b

Browse files
authored
feat: support pip_parse in Gazelle (bazel-contrib#633)
* feat: support pip_parse in Gazelle Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com> * Apply suggestions from code review
1 parent fd88779 commit 27d0c7b

File tree

18 files changed

+147
-69
lines changed

18 files changed

+147
-69
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ user.bazelrc
4747
# These otherwise match patterns above
4848
!go.mod
4949
!BUILD.out
50+
51+
# Python cache
52+
**/__pycache__/

examples/build_file_generation/BUILD

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
33
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
44
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
55
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
6-
load("@rules_python//python:defs.bzl", "py_library")
6+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
77

88
# This rule fetches the metadata for python packages we depend on. That data is
99
# required for the gazelle_python_manifest rule to update our manifest file.
@@ -22,7 +22,8 @@ modules_mapping(
2222
gazelle_python_manifest(
2323
name = "gazelle_python_manifest",
2424
modules_mapping = ":modules_map",
25-
pip_deps_repository_name = "pip",
25+
pip_repository_incremental = True,
26+
pip_repository_name = "pip",
2627
requirements = "//:requirements_lock.txt",
2728
)
2829

@@ -43,4 +44,13 @@ py_library(
4344
name = "build_file_generation",
4445
srcs = ["__init__.py"],
4546
visibility = ["//:__subpackages__"],
47+
deps = ["@pip_requests//:pkg"],
48+
)
49+
50+
py_binary(
51+
name = "build_file_generation_bin",
52+
srcs = ["__main__.py"],
53+
main = "__main__.py",
54+
visibility = ["//:__subpackages__"],
55+
deps = [":build_file_generation"],
4656
)

examples/build_file_generation/WORKSPACE

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ http_archive(
2323
# to include https://github.com/bazelbuild/bazel-gazelle/commit/2834ea4
2424
http_archive(
2525
name = "bazel_gazelle",
26-
sha256 = "0bb8056ab9ed4cbcab5b74348d8530c0e0b939987b0cfe36c1ab53d35a99e4de",
27-
strip_prefix = "bazel-gazelle-2834ea44b3ec6371c924baaf28704730ec9d4559",
26+
sha256 = "fd8d852ebcb770b41c1c933fc3085b4a23e1426a1af4e791d39b67bb8d894eb7",
27+
strip_prefix = "bazel-gazelle-41b542f9b0fefe916a95ca5460458abf916f5fe5",
2828
urls = [
2929
# No release since March, and we need subsequent fixes
30-
"https://github.com/bazelbuild/bazel-gazelle/archive/2834ea44b3ec6371c924baaf28704730ec9d4559.zip",
30+
"https://github.com/bazelbuild/bazel-gazelle/archive/41b542f9b0fefe916a95ca5460458abf916f5fe5.zip",
3131
],
3232
)
3333

@@ -48,13 +48,17 @@ local_repository(
4848
path = "../..",
4949
)
5050

51-
load("@rules_python//python:pip.bzl", "pip_install")
51+
load("@rules_python//python:pip.bzl", "pip_parse")
5252

53-
pip_install(
54-
# Uses the default repository name "pip"
55-
requirements = "//:requirements_lock.txt",
53+
pip_parse(
54+
name = "pip",
55+
requirements_lock = "//:requirements_lock.txt",
5656
)
5757

58+
load("@pip//:requirements.bzl", "install_deps")
59+
60+
install_deps()
61+
5862
# The rules_python gazelle extension has some third-party go dependencies
5963
# which we need to fetch in order to compile it.
6064
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
print("hello")
1+
import requests
2+
3+
def main(url):
4+
r = requests.get(url)
5+
print(r.text)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __init__ import main
2+
3+
4+
if __name__ == "__main__":
5+
main("https://example.com")

examples/build_file_generation/gazelle_python.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,7 @@ manifest:
126126
urllib3.util.timeout: urllib3
127127
urllib3.util.url: urllib3
128128
urllib3.util.wait: urllib3
129-
pip_deps_repository_name: pip
130-
integrity: 575d259c512b4b80f9923d1623d2aae3038654b731a4e088bf268e01138b6411
129+
pip_repository:
130+
name: pip
131+
incremental: true
132+
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8

gazelle/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ gazelle_python_manifest(
5858
modules_mapping = ":modules_map",
5959
# This is what we called our `pip_install` rule, where third-party
6060
# python libraries are loaded in BUILD files.
61-
pip_deps_repository_name = "pip",
61+
pip_repository_name = "pip",
62+
# When using pip_parse instead of pip_install, set the following.
63+
# pip_repository_incremental = True,
6264
# This should point to wherever we declare our python dependencies
6365
# (the same as what we passed to the modules_mapping rule in WORKSPACE)
6466
requirements = "//:requirements_lock.txt",

gazelle/manifest/defs.bzl

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,52 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary")
77
def gazelle_python_manifest(
88
name,
99
requirements,
10-
pip_deps_repository_name,
1110
modules_mapping,
11+
pip_repository_name = "",
12+
pip_repository_incremental = False,
13+
pip_deps_repository_name = "",
1214
manifest = ":gazelle_python.yaml"):
1315
"""A macro for defining the updating and testing targets for the Gazelle manifest file.
1416
1517
Args:
1618
name: the name used as a base for the targets.
1719
requirements: the target for the requirements.txt file.
18-
pip_deps_repository_name: the name of the pip_install repository target.
20+
pip_repository_name: the name of the pip_install or pip_repository target.
21+
pip_repository_incremental: the incremental property of pip_repository.
22+
pip_deps_repository_name: deprecated - the old pip_install target name.
1923
modules_mapping: the target for the generated modules_mapping.json file.
2024
manifest: the target for the Gazelle manifest file.
2125
"""
26+
if pip_deps_repository_name != "":
27+
# buildifier: disable=print
28+
print("DEPRECATED pip_deps_repository_name in //{}:{}. Please use pip_repository_name instead.".format(
29+
native.package_name(),
30+
name,
31+
))
32+
pip_repository_name = pip_deps_repository_name
33+
34+
if pip_repository_name == "":
35+
# This is a temporary check while pip_deps_repository_name exists as deprecated.
36+
fail("pip_repository_name must be set in //{}:{}".format(native.package_name(), name))
37+
2238
update_target = "{}.update".format(name)
2339
update_target_label = "//{}:{}".format(native.package_name(), update_target)
2440

41+
update_args = [
42+
"--requirements",
43+
"$(rootpath {})".format(requirements),
44+
"--pip-repository-name",
45+
pip_repository_name,
46+
"--modules-mapping",
47+
"$(rootpath {})".format(modules_mapping),
48+
"--output",
49+
"$(rootpath {})".format(manifest),
50+
"--update-target",
51+
update_target_label,
52+
]
53+
if pip_repository_incremental:
54+
update_args.append("--pip-repository-incremental")
55+
2556
go_binary(
2657
name = update_target,
2758
embed = ["@rules_python//gazelle/manifest/generate:generate_lib"],
@@ -30,18 +61,7 @@ def gazelle_python_manifest(
3061
modules_mapping,
3162
requirements,
3263
],
33-
args = [
34-
"--requirements",
35-
"$(rootpath {})".format(requirements),
36-
"--pip-deps-repository-name",
37-
pip_deps_repository_name,
38-
"--modules-mapping",
39-
"$(rootpath {})".format(modules_mapping),
40-
"--output",
41-
"$(rootpath {})".format(manifest),
42-
"--update-target",
43-
update_target_label,
44-
],
64+
args = update_args,
4565
visibility = ["//visibility:private"],
4666
tags = ["manual"],
4767
)

gazelle/manifest/generate/generate.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func init() {
2525

2626
func main() {
2727
var requirementsPath string
28-
var pipDepsRepositoryName string
28+
var pipRepositoryName string
29+
var pipRepositoryIncremental bool
2930
var modulesMappingPath string
3031
var outputPath string
3132
var updateTarget string
@@ -35,10 +36,15 @@ func main() {
3536
"",
3637
"The requirements.txt file.")
3738
flag.StringVar(
38-
&pipDepsRepositoryName,
39-
"pip-deps-repository-name",
39+
&pipRepositoryName,
40+
"pip-repository-name",
4041
"",
41-
"The name of the pip_install repository target.")
42+
"The name of the pip_install or pip_repository target.")
43+
flag.BoolVar(
44+
&pipRepositoryIncremental,
45+
"pip-repository-incremental",
46+
false,
47+
"The value for the incremental option in pip_repository.")
4248
flag.StringVar(
4349
&modulesMappingPath,
4450
"modules-mapping",
@@ -80,8 +86,11 @@ func main() {
8086
header := generateHeader(updateTarget)
8187

8288
manifestFile := manifest.NewFile(&manifest.Manifest{
83-
ModulesMapping: modulesMapping,
84-
PipDepsRepositoryName: pipDepsRepositoryName,
89+
ModulesMapping: modulesMapping,
90+
PipRepository: &manifest.PipRepository{
91+
Name: pipRepositoryName,
92+
Incremental: pipRepositoryIncremental,
93+
},
8594
})
8695
if err := writeOutput(outputPath, header, manifestFile, requirementsPath); err != nil {
8796
log.Fatalf("ERROR: %v\n", err)
@@ -142,4 +151,4 @@ func writeOutput(
142151
}
143152

144153
return nil
145-
}
154+
}

gazelle/manifest/manifest.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,18 @@ type Manifest struct {
100100
// wheel name provides these modules.
101101
ModulesMapping map[string]string `yaml:"modules_mapping"`
102102
// PipDepsRepositoryName is the name of the pip_install repository target.
103-
PipDepsRepositoryName string `yaml:"pip_deps_repository_name"`
103+
// DEPRECATED
104+
PipDepsRepositoryName string `yaml:"pip_deps_repository_name,omitempty"`
105+
// PipRepository contains the information for pip_install or pip_repository
106+
// target.
107+
PipRepository *PipRepository `yaml:"pip_repository,omitempty"`
108+
}
109+
110+
type PipRepository struct {
111+
// The name of the pip_install or pip_repository target.
112+
Name string
113+
// The incremental property of pip_repository.
114+
Incremental bool
104115
}
105116

106117
// sha256File calculates the checksum of a given file path.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy