Skip to content

Commit 568e139

Browse files
authored
build: add support for pip3_import from rules_python. (envoyproxy#11108)
Provide an Envoy build system styled way of loading arbitrary pip3 packages as py_{library,binary} dependencies. Part of envoyproxy#11085 (to fetch PyYAML). Risk level: Low (build system) Testing: Manual validation that the test utility loads PyYAML at the correct version (different to my system version). Signed-off-by: Harvey Tuch <[email protected]>
1 parent 45726b7 commit 568e139

11 files changed

+73
-8
lines changed

WORKSPACE

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ load("//bazel:repositories.bzl", "envoy_dependencies")
1212

1313
envoy_dependencies()
1414

15+
load("//bazel:repositories_extra.bzl", "envoy_dependencies_extra")
16+
17+
envoy_dependencies_extra()
18+
1519
load("//bazel:dependency_imports.bzl", "envoy_dependency_imports")
1620

1721
envoy_dependency_imports()

bazel/EXTERNAL_DEPS.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ values can change when Github change their tar/gzip libraries breaking
66
builds. Maintainer provided tarballs are more stable and the maintainer
77
can provide the SHA256.
88

9-
# Adding external dependencies to Envoy (native Bazel)
9+
# Adding external dependencies to Envoy (C++)
10+
11+
## Native Bazel
1012

1113
This is the preferred style of adding dependencies that use Bazel for their
1214
build process.
@@ -17,7 +19,7 @@ build process.
1719
`external_deps` attribute.
1820
3. `bazel test //test/...`
1921

20-
# Adding external dependencies to Envoy (external CMake)
22+
## External CMake (preferred)
2123

2224
This is the preferred style of adding dependencies that use CMake for their build system.
2325

@@ -29,7 +31,8 @@ This is the preferred style of adding dependencies that use CMake for their buil
2931
`external_deps` attribute.
3032
4. `bazel test //test/...`
3133

32-
# Adding external dependencies to Envoy (genrule repository)
34+
35+
## genrule repository
3336

3437
This is the newer style of adding dependencies with no upstream Bazel configs.
3538
It wraps the dependency's native build tooling in a Bazel-aware shell script,
@@ -54,6 +57,24 @@ Dependencies between external libraries can use the standard Bazel dependency
5457
resolution logic, using the `$(location)` shell extension to resolve paths
5558
to binaries, libraries, headers, etc.
5659

60+
# Adding external dependencies to Envoy (Python)
61+
62+
Python dependencies should be added via `pip3` and `rules_python`. The process
63+
is:
64+
65+
1. Define a `pip3_import()` pointing at your target `requirements.txt` in
66+
[`bazel/repositories_extra.bzl`](repositories_extra.bzl)
67+
68+
2. Add a `pip_install()` invocation in
69+
[`bazel/dependency_imports.bzl`](dependency_imports.bzl).
70+
71+
3. Add a `requirements("<package name")` in the `BUILD` file that depends on
72+
this package.
73+
74+
You can use [`tools/config_validation/BUILD`](../tools/config_validation/BUILD) as an example
75+
for this flow. See also the [`rules_python`](https://github.com/bazelbuild/rules_python)
76+
documentation for further references.
77+
5778
# Updating an external dependency version
5879

5980
1. Update the corresponding entry in

bazel/dependency_imports.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@envoy_build_tools//toolchains:rbe_toolchains_config.bzl", "rbe_toolchains
44
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
55
load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
66
load("@upb//bazel:repository_defs.bzl", upb_bazel_version_repository = "bazel_version_repository")
7+
load("@config_validation//:requirements.bzl", config_validation_pip_install = "pip_install")
78

89
# go version for rules_go
910
GO_VERSION = "1.13.5"
@@ -38,3 +39,5 @@ def envoy_dependency_imports(go_version = GO_VERSION):
3839
sum = "h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=",
3940
version = "v0.3.0",
4041
)
42+
43+
config_validation_pip_install()

bazel/repositories.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ _default_envoy_build_config = repository_rule(
6868
},
6969
)
7070

71-
# Python dependencies. If these become non-trivial, we might be better off using a virtualenv to
72-
# wrap them, but for now we can treat them as first-class Bazel.
71+
# Python dependencies.
7372
def _python_deps():
73+
# TODO(htuch): convert these to pip3_import.
7474
_repository_impl(
7575
name = "com_github_pallets_markupsafe",
7676
build_file = "@envoy//bazel/external:markupsafe.BUILD",

bazel/repositories_extra.bzl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_python//python:repositories.bzl", "py_repositories")
2+
load("@rules_python//python:pip.bzl", "pip3_import", "pip_repositories")
3+
4+
# Python dependencies.
5+
def _python_deps():
6+
py_repositories()
7+
pip_repositories()
8+
9+
pip3_import(
10+
name = "config_validation",
11+
requirements = "@envoy//tools/config_validation:requirements.txt",
12+
)
13+
14+
# Envoy deps that rely on a first stage of dependency loading in envoy_dependencies().
15+
def envoy_dependencies_extra():
16+
_python_deps()

bazel/repository_locations.bzl

+5-2
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,11 @@ DEPENDENCY_REPOSITORIES = dict(
365365
use_category = ["build"],
366366
),
367367
rules_python = dict(
368-
sha256 = "aa96a691d3a8177f3215b14b0edc9641787abaaa30363a080165d06ab65e1161",
369-
urls = ["https://github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz"],
368+
sha256 = "76a8fd4e7eca2a3590f816958faa0d83c9b2ce9c32634c5c375bcccf161d3bb5",
369+
strip_prefix = "rules_python-a0fbf98d4e3a232144df4d0d80b577c7a693b570",
370+
# 2020-04-09
371+
# TODO(htuch): revert back to a point releases when pip3_import appears.
372+
urls = ["https://github.com/bazelbuild/rules_python/archive/a0fbf98d4e3a232144df4d0d80b577c7a693b570.tar.gz"],
370373
use_category = ["build"],
371374
),
372375
six = dict(

ci/WORKSPACE.filter.example

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ load("@envoy//bazel:repositories.bzl", "envoy_dependencies")
1717

1818
envoy_dependencies()
1919

20+
load("@envoy//bazel:repositories_extra.bzl", "envoy_dependencies_extra")
21+
22+
envoy_dependencies_extra()
23+
2024
load("@envoy//bazel:dependency_imports.bzl", "envoy_dependency_imports")
2125

2226
envoy_dependency_imports()

ci/run_envoy_docker_windows.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44

55
# TODO(sunjayBhatia, wrowe): update this note once we have an RBE toolchain generated for Windows
66
# NOTE: Update this from the latest pushed image here: https://hub.docker.com/r/envoyproxy/envoy-build-windows2019/tags
7-
ENVOY_BUILD_SHA="3cbc11e373dc4e3a523b9273ed010c5e0f197874"
7+
ENVOY_BUILD_SHA="9b7dc527351b9888805377a05e5975d6ef8d6ae1"
88

99
[[ -z "${IMAGE_NAME}" ]] && IMAGE_NAME="envoyproxy/envoy-build-windows2019"
1010
# The IMAGE_ID defaults to the CI hash but can be set to an arbitrary image ID (found with 'docker

tools/config_validation/BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
licenses(["notice"]) # Apache 2
2+
3+
load("@config_validation//:requirements.bzl", "requirement")
4+
5+
py_binary(
6+
name = "validate_yaml_fragment",
7+
srcs = ["validate_yaml_fragment.py"],
8+
visibility = ["//visibility:public"],
9+
deps = [requirement("PyYAML")],
10+
)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyYAML==5.3.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import yaml
2+
3+
print('YAML version is %s' % yaml.__version__)

0 commit comments

Comments
 (0)