From 0ad6444039b59296f5097b2a21e5a1e7a6df6555 Mon Sep 17 00:00:00 2001 From: Andrii Kaznadiei Date: Fri, 2 Aug 2024 18:33:42 +0200 Subject: [PATCH] Do not run Python during 'configure_ros2' - Add possibility to provide user-defined `setup.bzl` - Generate 'setup.bzl' as part of `repos_lock.update` --- repos/config/BUILD.bazel | 2 +- repos/config/defs.bzl | 7 ++++--- repos/config/detail/BUILD.bazel | 11 +++++++++-- repos/config/detail/lock_repos.py | 11 ++++++++++- repos/config/detail/ros2_config.bzl | 18 ++++-------------- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/repos/config/BUILD.bazel b/repos/config/BUILD.bazel index a459d77..d4cec41 100644 --- a/repos/config/BUILD.bazel +++ b/repos/config/BUILD.bazel @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -exports_files(glob(["*.lock"])) +exports_files(["bazel.repos"] + glob(["*.lock"])) alias( name = "repos_lock.update", diff --git a/repos/config/defs.bzl b/repos/config/defs.bzl index 7ac331b..fb511fa 100644 --- a/repos/config/defs.bzl +++ b/repos/config/defs.bzl @@ -16,7 +16,7 @@ load("@rules_ros//repos/config/detail:ros2_config.bzl", "ros2_config") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("@rules_ros//repos/config:distros.bzl", "DISTROS") -def _configure_ros2(*, name, distro_src, repo_index_overlays): +def _configure_ros2(*, name, distro_src, repo_index_overlays, setup_file): distro_src_wo_index = {k: v for k, v in distro_src.items() if k != "repo_index"} distro_src_wo_index["build_file_content"] = 'exports_files(["ros2.repos"])' @@ -28,9 +28,10 @@ def _configure_ros2(*, name, distro_src, repo_index_overlays): repo_index_overlays = [ "@rules_ros//repos/config:bazel.repos", ] + repo_index_overlays, + setup_file = setup_file, ) -def configure_ros2(*, name = "ros2_config", repo_index_overlays = [], distro): +def configure_ros2(*, name = "ros2_config", repo_index_overlays = [], setup_file, distro): """ """ if type(distro) == type(""): @@ -43,4 +44,4 @@ def configure_ros2(*, name = "ros2_config", repo_index_overlays = [], distro): distro_src = distro if not type(repo_index_overlays) == type([]): fail("repo_index_overlays needs to be a list of *.repos files") - _configure_ros2(name = name, distro_src = distro_src, repo_index_overlays = repo_index_overlays) + _configure_ros2(name = name, distro_src = distro_src, repo_index_overlays = repo_index_overlays, setup_file = setup_file) diff --git a/repos/config/detail/BUILD.bazel b/repos/config/detail/BUILD.bazel index 21d32d0..f371dbe 100644 --- a/repos/config/detail/BUILD.bazel +++ b/repos/config/detail/BUILD.bazel @@ -11,15 +11,22 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + load("@python_deps//:requirements.bzl", "requirement") load("@ros2_config//:repos_lock_file.bzl", "REPOS_LOCK_FILE") +load("@ros2_config//:repos_setup_file.bzl", "REPOS_SETUP_FILE") +load("@ros2_config//:repos_overlay_files.bzl", "REPOS_OVERLAY_FILES") py_binary( name = "repos_lock.update", srcs = ["lock_repos.py"], main = "lock_repos.py", - data = ["@ros2//:ros2.repos", REPOS_LOCK_FILE], - args = ["$(execpath @ros2//:ros2.repos)", "$(execpath {})".format(REPOS_LOCK_FILE)], + data = ["generate_ros2_config.py", "@ros2//:ros2.repos", REPOS_LOCK_FILE, REPOS_SETUP_FILE] + REPOS_OVERLAY_FILES, + args = [ + "$(execpath @ros2//:ros2.repos)", + "$(execpath {})".format(REPOS_LOCK_FILE), + "$(execpath {})".format(REPOS_SETUP_FILE), + ] + ["$(execpath {})".format(f) for f in REPOS_OVERLAY_FILES], deps = [requirement("pyyaml")], visibility = ["//visibility:public"], ) diff --git a/repos/config/detail/lock_repos.py b/repos/config/detail/lock_repos.py index 6df8145..7f50314 100755 --- a/repos/config/detail/lock_repos.py +++ b/repos/config/detail/lock_repos.py @@ -28,6 +28,8 @@ def main(): parser = argparse.ArgumentParser(description="Generate a lock file for a given repos file.") parser.add_argument('repos', type=str, help='Input YAML file') parser.add_argument('lock', type=str, help='Output YAML file with pinned repos') + parser.add_argument('setup_bzl', type=str, help='TODO') + parser.add_argument('overlays', type=str, nargs='*', help='TODO') parser.add_argument('--tar', action='store_true', help='Use the Github archive download.') args = parser.parse_args() @@ -58,6 +60,13 @@ def main(): ) yaml.dump(repos, lock_file, default_flow_style=False, allow_unicode=True) + with open(args.setup_bzl, "w", encoding='utf8') as setup_bzl: + result = subprocess.run( + ["external/rules_ros/repos/config/detail/generate_ros2_config.py", args.lock, *args.overlays], + stdout=setup_bzl, + encoding='utf8' + ) + def add_attributes(dictionary, additional_attributes): for k,v in additional_attributes.items(): dictionary[k] = v @@ -126,4 +135,4 @@ def fetch_repo_details(url, branch): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/repos/config/detail/ros2_config.bzl b/repos/config/detail/ros2_config.bzl index d514131..a8a6a85 100644 --- a/repos/config/detail/ros2_config.bzl +++ b/repos/config/detail/ros2_config.bzl @@ -25,25 +25,15 @@ _archive_attrs = { for a repo. """, ), - "_generate_ros2_config": attr.label( - default = "generate_ros2_config.py", - ), - "verbose": attr.bool( - default = False, - doc = "Prints the calling sequence to stdout.", + "setup_file": attr.label( + doc = ".bzl file containing repo rules to load every ros2 repository.", ), } def _ros2_config_impl(ctx): - result = ctx.execute( - [ctx.attr._generate_ros2_config, ctx.attr.repo_index] + - ctx.attr.repo_index_overlays, - ) - if result.return_code != 0: - fail(result.stderr) - - ctx.file("setup.bzl", content = result.stdout) ctx.file("repos_lock_file.bzl", content = "REPOS_LOCK_FILE = '{}'".format(ctx.attr.repo_index)) + ctx.file("repos_overlay_files.bzl", content = "REPOS_OVERLAY_FILES = {}".format(["{}".format(l) for l in ctx.attr.repo_index_overlays])) + ctx.file("repos_setup_file.bzl", content = "REPOS_SETUP_FILE = '{}'".format(ctx.attr.setup_file)) ctx.file("WORKSPACE", content = "workspace(name = {}".format(ctx.name), executable = False) ctx.file("BUILD", executable = False)