Skip to content

Commit

Permalink
Do not run Python during 'configure_ros2'
Browse files Browse the repository at this point in the history
- Add possibility to provide user-defined `setup.bzl`
- Generate 'setup.bzl' as part of `repos_lock.update`
  • Loading branch information
andrii-apexai committed Aug 5, 2024
1 parent 3bf5db7 commit 0ad6444
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion repos/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions repos/config/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"])'

Expand All @@ -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(""):
Expand All @@ -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)
11 changes: 9 additions & 2 deletions repos/config/detail/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
11 changes: 10 additions & 1 deletion repos/config/detail/lock_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -126,4 +135,4 @@ def fetch_repo_details(url, branch):


if __name__ == "__main__":
main()
main()
18 changes: 4 additions & 14 deletions repos/config/detail/ros2_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0ad6444

Please sign in to comment.