From bda0b6204a0899550b73ccc56256e029d4255628 Mon Sep 17 00:00:00 2001 From: Harvey Tuch Date: Thu, 7 May 2020 15:37:47 -0400 Subject: [PATCH] build: add support for pip3_import from rules_python. Provide an Envoy build system styled way of loading arbitrary pip3 packages as py_{library,binary} dependencies. Part of https://github.com/envoyproxy/envoy/issues/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 --- WORKSPACE | 4 ++++ bazel/dependency_imports.bzl | 3 +++ bazel/repositories.bzl | 4 ++-- bazel/repositories_extra.bzl | 16 ++++++++++++++++ bazel/repository_locations.bzl | 7 +++++-- tools/config_validation/BUILD | 10 ++++++++++ tools/config_validation/requirements.txt | 1 + .../config_validation/validate_yaml_fragment.py | 3 +++ 8 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 bazel/repositories_extra.bzl create mode 100644 tools/config_validation/BUILD create mode 100644 tools/config_validation/requirements.txt create mode 100644 tools/config_validation/validate_yaml_fragment.py diff --git a/WORKSPACE b/WORKSPACE index ef120bc53d4f..a96cba501302 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -12,6 +12,10 @@ load("//bazel:repositories.bzl", "envoy_dependencies") envoy_dependencies() +load("//bazel:repositories_extra.bzl", "envoy_dependencies_extra") + +envoy_dependencies_extra() + load("//bazel:dependency_imports.bzl", "envoy_dependency_imports") envoy_dependency_imports() diff --git a/bazel/dependency_imports.bzl b/bazel/dependency_imports.bzl index 051923e315a4..2385a98ff25b 100644 --- a/bazel/dependency_imports.bzl +++ b/bazel/dependency_imports.bzl @@ -4,6 +4,7 @@ load("@envoy_build_tools//toolchains:rbe_toolchains_config.bzl", "rbe_toolchains load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies") load("@upb//bazel:repository_defs.bzl", upb_bazel_version_repository = "bazel_version_repository") +load("@config_validation//:requirements.bzl", config_validation_pip_install = "pip_install") # go version for rules_go GO_VERSION = "1.13.5" @@ -38,3 +39,5 @@ def envoy_dependency_imports(go_version = GO_VERSION): sum = "h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=", version = "v0.3.0", ) + + config_validation_pip_install() diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 18230f79d954..73e52e3eb7fe 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -65,9 +65,9 @@ _default_envoy_build_config = repository_rule( }, ) -# Python dependencies. If these become non-trivial, we might be better off using a virtualenv to -# wrap them, but for now we can treat them as first-class Bazel. +# Python dependencies. def _python_deps(): + # TODO(htuch): convert these to pip3_import. _repository_impl( name = "com_github_pallets_markupsafe", build_file = "@envoy//bazel/external:markupsafe.BUILD", diff --git a/bazel/repositories_extra.bzl b/bazel/repositories_extra.bzl new file mode 100644 index 000000000000..277b6b6736c8 --- /dev/null +++ b/bazel/repositories_extra.bzl @@ -0,0 +1,16 @@ +load("@rules_python//python:repositories.bzl", "py_repositories") +load("@rules_python//python:pip.bzl", "pip3_import", "pip_repositories") + +# Python dependencies. +def _python_deps(): + py_repositories() + pip_repositories() + + pip3_import( + name = "config_validation", + requirements = "//tools/config_validation:requirements.txt", + ) + +# Envoy deps that rely on a first stage of dependency loading in envoy_dependencies(). +def envoy_dependencies_extra(): + _python_deps() diff --git a/bazel/repository_locations.bzl b/bazel/repository_locations.bzl index 9561aae50d6f..2a83b77b50f7 100644 --- a/bazel/repository_locations.bzl +++ b/bazel/repository_locations.bzl @@ -365,8 +365,11 @@ DEPENDENCY_REPOSITORIES = dict( use_category = ["build"], ), rules_python = dict( - sha256 = "aa96a691d3a8177f3215b14b0edc9641787abaaa30363a080165d06ab65e1161", - urls = ["https://github.com/bazelbuild/rules_python/releases/download/0.0.1/rules_python-0.0.1.tar.gz"], + sha256 = "76a8fd4e7eca2a3590f816958faa0d83c9b2ce9c32634c5c375bcccf161d3bb5", + strip_prefix = "rules_python-a0fbf98d4e3a232144df4d0d80b577c7a693b570", + # 2020-04-09 + # TODO(htuch): revert back to a point releases when pip3_import appears. + urls = ["https://github.com/bazelbuild/rules_python/archive/a0fbf98d4e3a232144df4d0d80b577c7a693b570.tar.gz"], use_category = ["build"], ), six = dict( diff --git a/tools/config_validation/BUILD b/tools/config_validation/BUILD new file mode 100644 index 000000000000..a6e8b6db72b2 --- /dev/null +++ b/tools/config_validation/BUILD @@ -0,0 +1,10 @@ +licenses(["notice"]) # Apache 2 + +load("@config_validation//:requirements.bzl", "requirement") + +py_binary( + name = "validate_yaml_fragment", + srcs = ["validate_yaml_fragment.py"], + visibility = ["//visibility:public"], + deps = [requirement("PyYAML")], +) diff --git a/tools/config_validation/requirements.txt b/tools/config_validation/requirements.txt new file mode 100644 index 000000000000..7a997b5e44bd --- /dev/null +++ b/tools/config_validation/requirements.txt @@ -0,0 +1 @@ +PyYAML==5.3.1 diff --git a/tools/config_validation/validate_yaml_fragment.py b/tools/config_validation/validate_yaml_fragment.py new file mode 100644 index 000000000000..0cfac273b237 --- /dev/null +++ b/tools/config_validation/validate_yaml_fragment.py @@ -0,0 +1,3 @@ +import yaml + +print('YAML version is %s' % yaml.__version__)