From b803c686243787bc45a41732d9348b08afaff449 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Wed, 25 Jan 2023 03:55:09 -0700 Subject: [PATCH] [py] Replace devtools genrule with proper generate_devtools rule. (#11392) The existing genrule does not handle directories properly. --- WORKSPACE | 6 ---- py/BUILD.bazel | 22 ++++++-------- py/defs.bzl | 2 ++ py/generate.py | 1 - py/private/generate_devtools.bzl | 50 ++++++++++++++++++++++++++++++++ py/rules_pkg_tree.patch | 16 ---------- 6 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 py/private/generate_devtools.bzl delete mode 100644 py/rules_pkg_tree.patch diff --git a/WORKSPACE b/WORKSPACE index 0f6ad82a166d6..f205efadda63c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -229,12 +229,6 @@ rules_closure_toolchains() http_archive( name = "rules_pkg", - patch_args = [ - "-p1", - ], - patches = [ - "//py:rules_pkg_tree.patch", - ], sha256 = "eea0f59c28a9241156a47d7a8e32db9122f3d50b505fae0f33de6ce4d9b61834", urls = [ "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.8.0/rules_pkg-0.8.0.tar.gz", diff --git a/py/BUILD.bazel b/py/BUILD.bazel index c58ddfe6fe907..f00979c8734bc 100644 --- a/py/BUILD.bazel +++ b/py/BUILD.bazel @@ -4,7 +4,7 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library") load("@rules_python//python:packaging.bzl", "py_package", "py_wheel") load("@py_dev_requirements//:requirements.bzl", "requirement") load("//common:defs.bzl", "copy_file") -load("//py:defs.bzl", "py_test_suite") +load("//py:defs.bzl", "generate_devtools", "py_test_suite") load("//py/private:browsers.bzl", "BROWSERS") load("//py/private:import.bzl", "py_import") load("@rules_python//python:pip.bzl", "compile_pip_requirements") @@ -273,18 +273,14 @@ py_binary( deps = [requirement("inflection")], ) -[genrule( - name = "create-cdp-srcs-" + n, - srcs = [ - "//common/devtools/chromium/" + n + ":browser_protocol", - "//common/devtools/chromium/" + n + ":js_protocol", - ], - outs = ["selenium/webdriver/common/devtools/" + n], - cmd = "$(location :generate) $(location //common/devtools/chromium/" + n + ":browser_protocol) $(location //common/devtools/chromium/" + n + ":js_protocol) $@", - tools = [ - ":generate", - ], -) for n in BROWSER_VERSIONS] +[generate_devtools( + name = "create-cdp-srcs-{}".format(devtools_version), + outdir = "selenium/webdriver/common/devtools/{}".format(devtools_version), + browser_protocol = "//common/devtools/chromium/{}:browser_protocol".format(devtools_version), + generator = ":generate", + js_protocol = "//common/devtools/chromium/{}:js_protocol".format(devtools_version), + protocol_version = devtools_version, +) for devtools_version in BROWSER_VERSIONS] py_test_suite( name = "unit", diff --git a/py/defs.bzl b/py/defs.bzl index df25fa5c1d1a7..8f8b4ff94b5ac 100644 --- a/py/defs.bzl +++ b/py/defs.bzl @@ -1,7 +1,9 @@ +load("//py/private:generate_devtools.bzl", _generate_devtools = "generate_devtools") load("//py/private:import.bzl", _py_import = "py_import") load("//py/private:pytest.bzl", _pytest_test = "pytest_test") load("//py/private:suite.bzl", _py_test_suite = "py_test_suite") +generate_devtools = _generate_devtools pytest_test = _pytest_test py_import = _py_import py_test_suite = _py_test_suite diff --git a/py/generate.py b/py/generate.py index 4ebd69b600649..5d6ea7cb4b0ef 100644 --- a/py/generate.py +++ b/py/generate.py @@ -984,7 +984,6 @@ def main(browser_protocol_path, js_protocol_path, output_path): browser_protocol_path, js_protocol_path, ] - output_path.mkdir(parents=True) # Generate util.py util_path = output_path / "util.py" diff --git a/py/private/generate_devtools.bzl b/py/private/generate_devtools.bzl new file mode 100644 index 0000000000000..5fea436ec40f8 --- /dev/null +++ b/py/private/generate_devtools.bzl @@ -0,0 +1,50 @@ +def _generate_devtools_impl(ctx): + outdir = ctx.actions.declare_directory(ctx.attr.outdir) + + args = ctx.actions.args() + args.add(ctx.file.browser_protocol) + args.add(ctx.file.js_protocol) + args.add(outdir.path) + + ctx.actions.run( + executable = ctx.executable.generator, + progress_message = "Generating {} DevTools Protocol bindings for Python".format(ctx.attr.protocol_version), + arguments = [args], + outputs = [ + outdir, + ], + inputs = [ + ctx.file.browser_protocol, + ctx.file.js_protocol, + ], + use_default_shell_env = True, + ) + + return DefaultInfo( + files = depset([outdir]), + runfiles = ctx.runfiles(files = [outdir]), + ) + +generate_devtools = rule( + implementation = _generate_devtools_impl, + attrs = { + "protocol_version": attr.string( + mandatory = True, + default = "", + ), + "browser_protocol": attr.label( + mandatory = True, + allow_single_file = True, + ), + "js_protocol": attr.label( + mandatory = True, + allow_single_file = True, + ), + "outdir": attr.string(), + "generator": attr.label( + executable = True, + cfg = "exec", + ), + "deps": attr.label_list(), + }, +) diff --git a/py/rules_pkg_tree.patch b/py/rules_pkg_tree.patch deleted file mode 100644 index 5ba6fd32105a9..0000000000000 --- a/py/rules_pkg_tree.patch +++ /dev/null @@ -1,16 +0,0 @@ -diff --git a/pkg/private/tar/build_tar.py b/pkg/private/tar/build_tar.py -index ab16610d..b6b75bd7 100644 ---- a/pkg/private/tar/build_tar.py -+++ b/pkg/private/tar/build_tar.py -@@ -309,7 +309,10 @@ def add_manifest_entry(self, entry_list, file_attributes): - elif entry.entry_type == manifest.ENTRY_IS_EMPTY_FILE: - self.add_empty_file(entry.dest, **attrs) - else: -- self.add_file(entry.src, entry.dest, **attrs) -+ if os.path.isdir(entry.src): -+ self.add_tree(entry.src, entry.dest, **attrs) -+ else: -+ self.add_file(entry.src, entry.dest, **attrs) - - - def main():