Skip to content

Commit

Permalink
Test rules_python with local package
Browse files Browse the repository at this point in the history
Signed-off-by: Amaury Pouly <[email protected]>
  • Loading branch information
pamaury committed Oct 2, 2024
1 parent 6d8b407 commit 3c7e56f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bazel_skylib_workspace()

# Python Toolchain + PIP Dependencies
load("//third_party/python:repos.bzl", "python_repos")
python_repos()
python_repos("/home/pamaury/project/rules_python")
load("//third_party/python:deps.bzl", "python_deps")
python_deps()
load("//third_party/python:pip.bzl", "pip_deps")
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ dependencies = [
# and bug fixes. We fix the version for improved stability and manually update
# if necessary.
"chipwhisperer@https://github.com/newaetech/chipwhisperer-minimal/archive/2643131b71e528791446ee1bab7359120288f4ab.zip",

# Test
"hello @ file:///home/pamaury/project/opentitan/util/hello"
]

[tool.setuptools]
# This is actually not a python project, we just use pyproject.toml to manage dependencies.
py-modules = []

[tool.uv.workspace]
members = ["util/hello/hello"]
1 change: 1 addition & 0 deletions python-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ gitdb==4.0.10 \
gitpython==3.1.32 \
--hash=sha256:8d9b8cb1e80b9735e8717c9362079d3ce4c6e5ddeebedd0361b228c3a67a62f6 \
--hash=sha256:e3d59b1c2c6ebb9dfa7a184daf3b6dd4914237e7488a1730a6d8f6f5d0b4187f
hello @ file:///home/pamaury/project/opentitan/util/hello
hjson==3.1.0 \
--hash=sha256:55af475a27cf83a7969c808399d7bccdec8fb836a07ddbd574587593b9cdcf75 \
--hash=sha256:65713cdcf13214fb554eb8b4ef803419733f4f5e551047c9b711098ab7186b89
Expand Down
34 changes: 12 additions & 22 deletions rules/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@python3//:defs.bzl", "interpreter")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_skylib//lib:paths.bzl", "paths")

Expand Down Expand Up @@ -30,17 +29,18 @@ def http_archive_or_local(local = None, **kwargs):
def _bare_repository_impl(rctx):
if rctx.attr.local:
workspace_root = str(rctx.path(rctx.attr.workspace).dirname.realpath)
result = rctx.execute(
[
rctx.path(rctx.attr.python_interpreter),
rctx.attr._symlink_tree,
paths.join(workspace_root, rctx.attr.local),
".",
],
quiet = False,
)
if result.return_code != 0:
fail("Error initializing repo for {}".format(rctx.attr.local))
# FIXME use standard bazel symlink
# result = rctx.execute(
# [
# rctx.path(rctx.attr.python_interpreter),
# rctx.attr._symlink_tree,
# paths.join(workspace_root, rctx.attr.local),
# ".",
# ],
# quiet = False,
# )
# if result.return_code != 0:
# fail("Error initializing repo for {}".format(rctx.attr.local))
elif rctx.attr.url:
rctx.download_and_extract(
url = rctx.attr.url,
Expand Down Expand Up @@ -68,16 +68,6 @@ bare_repository = repository_rule(
"additional_files_content": attr.string_dict(
doc = "Additional files to place in the repository (mapping repo filename to strings).",
),
"python_interpreter": attr.label(
default = interpreter,
allow_single_file = True,
doc = "Python interpreter to use.",
),
"_symlink_tree": attr.label(
default = Label("//rules/scripts:symlink_tree.py"),
allow_files = True,
doc = "Script to create symlink trees for the `local` case.",
),
"workspace": attr.label(
default = Label("//:WORKSPACE"),
allow_single_file = True,
Expand Down
9 changes: 5 additions & 4 deletions third_party/python/repos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@//rules:repo.bzl", "http_archive_or_local")

def python_repos():
http_archive(
def python_repos(local = None):
http_archive_or_local(
name = "rules_python",
local = local,
sha256 = "778aaeab3e6cfd56d681c89f5c10d7ad6bf8d2f1a72de9de55b23081b2d31618",
strip_prefix = "rules_python-0.34.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.34.0/rules_python-0.34.0.tar.gz",
)
)
1 change: 1 addition & 0 deletions util/hello/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file is here just to prevent bazel from looking into this repository.
1 change: 1 addition & 0 deletions util/hello/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file is here just to prevent bazel from looking into this repository.
10 changes: 10 additions & 0 deletions util/hello/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3

def main():
print("hello")

def bye():
print("bye")

if __name__ == '__main__':
main()
10 changes: 10 additions & 0 deletions util/hello/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
name = "hello"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.8"
dependencies = []

[project.scripts]
hello = "hello:main"
bye = "hello:bye"

0 comments on commit 3c7e56f

Please sign in to comment.