Skip to content

Commit

Permalink
Setup bazel workspace for registry tools (#416)
Browse files Browse the repository at this point in the history
* Setup bazel workspace for registry tools

* Add calls to chdir
  • Loading branch information
jsharpe committed Mar 1, 2023
1 parent b6ded3e commit ddc9cd6
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 3 deletions.
1 change: 1 addition & 0 deletions .bazelrc
@@ -0,0 +1 @@
common --enable_bzlmod
1 change: 1 addition & 0 deletions .bazelversion
@@ -0,0 +1 @@
6.0.0
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
tools/__pycache__
/bazel-*
29 changes: 29 additions & 0 deletions MODULE.bazel
@@ -0,0 +1,29 @@
module(
name = "bazel_central_registry",
compatibility_level = 1,
version = "0.0.0",
)

bazel_dep(name = "rules_python", version = "0.17.3")

python = use_extension("@rules_python//python:extensions.bzl", "python")

python.toolchain(
name = "python3",
python_version = "3.11",
)

use_repo(python, "python3_toolchains")

register_toolchains(
"@python3_toolchains//:all",
)

pip = use_extension("@rules_python//python:extensions.bzl", "pip")

pip.parse(
name = "tools_deps",
requirements_lock = "//tools:requirements_lock.txt",
)

use_repo(pip, "tools_deps")
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,5 +17,5 @@ If you consider it necessary, you can do the following to avoid depending on the

- Clone the BCR repository or mirror the content to your own infrastructure and use the [--registry](https://bazel.build/reference/command-line-reference#flag--registry) option to change the default Bazel registry to your own.
- Host your own mirror for all source archive URLs and add the mirror URL in `./bazel_registry.json`.
You can run the `./tools/print_all_src_urls.py` script to get the list of source URLs to mirror for all Bazel modules checked into the BCR.
You can run `bazel run //tools:print_all_src_urls` to get the list of source URLs to mirror for all Bazel modules checked into the BCR.
For example, `https://foo.com/bar.zip` should be mirrored to `https://<your mirror>/foo.com/bar.zip`.
Empty file added WORKSPACE
Empty file.
76 changes: 76 additions & 0 deletions tools/BUILD
@@ -0,0 +1,76 @@
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@tools_deps//:requirements.bzl", "requirement")

compile_pip_requirements(
name = "requirements",
requirements_in = "requirements.in",
requirements_txt = "requirements_lock.txt",
)

py_binary(
name = "add_module",
srcs = ["add_module.py"],
deps = [
":bcr_validation",
":registry",
],
)

py_binary(
name = "calc_integrity",
srcs = ["calc_integrity.py"],
deps = [
":registry",
requirement("validators"),
],
)

py_binary(
name = "migrate_to_bzlmod",
srcs = ["migrate_to_bzlmod.py"],
deps = [
":registry",
],
)

py_binary(
name = "print_all_src_urls",
srcs = ["print_all_src_urls.py"],
deps = [
":registry",
],
)

py_library(
name = "bcr_validation",
srcs = ["bcr_validation.py"],
deps = [
":registry",
":verify_stable_archives",
],
)

py_library(
name = "verify_stable_archives",
srcs = ["verify_stable_archives.py"],
)

py_library(
name = "registry",
srcs = ["registry.py"],
imports = ["."],
deps = [
requirement("pyyaml"),
],
)

py_test(
name = "version_test",
srcs = [
"version_test.py",
],
deps = [
"registry",
],
)
4 changes: 2 additions & 2 deletions tools/README.md
Expand Up @@ -4,7 +4,7 @@

An interactive script for adding a module to the BCR.
```
$ ./tools/add_module.py
$ bazel run //tools:add_module
INFO: Getting module information from user input...
ACTION: Please enter the module name:
...
Expand Down Expand Up @@ -41,7 +41,7 @@ optional arguments:

Print the list of source archive URLs of all modules in the BCR.
```
$ ./tools/print_all_src_urls.py
$ bazel run //tools:print_all_src_urls
https://github.com/bazelbuild/rules_jvm_external/archive/refs/tags/4.4.2.zip
https://github.com/bazelbuild/rules_jvm_external/archive/refs/tags/4.5.zip
...
Expand Down
4 changes: 4 additions & 0 deletions tools/add_module.py 100755 → 100644
Expand Up @@ -37,6 +37,7 @@
"""

import argparse
import os
import sys
import time

Expand Down Expand Up @@ -213,4 +214,7 @@ def main(argv=None):


if __name__ == "__main__":
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
sys.exit(main())
3 changes: 3 additions & 0 deletions tools/bcr_validation.py 100755 → 100644
Expand Up @@ -334,4 +334,7 @@ def main(argv=None):
return validator.getValidationReturnCode()

if __name__ == "__main__":
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
sys.exit(main())
4 changes: 4 additions & 0 deletions tools/calc_integrity.py 100755 → 100644
Expand Up @@ -15,13 +15,17 @@
# limitations under the License.

import validators
import os
import sys

from registry import read
from registry import download
from registry import integrity

if __name__ == "__main__":
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
if validators.url(sys.argv[1]):
print(integrity(download(sys.argv[1])))
else:
Expand Down
4 changes: 4 additions & 0 deletions tools/print_all_src_urls.py 100755 → 100644
Expand Up @@ -17,6 +17,7 @@
# pylint: disable=line-too-long
# pylint: disable=missing-function-docstring

import os
import sys

from registry import RegistryClient
Expand All @@ -27,4 +28,7 @@ def main():
print(client.get_source(name, version)["url"])

if __name__ == "__main__":
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
sys.exit(main())
Empty file modified tools/registry.py 100755 → 100644
Empty file.
2 changes: 2 additions & 0 deletions tools/requirements.in
@@ -0,0 +1,2 @@
pyyaml
validators
55 changes: 55 additions & 0 deletions tools/requirements_lock.txt
@@ -0,0 +1,55 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# bazel run //tools:requirements.update
#
decorator==5.1.1 \
--hash=sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330 \
--hash=sha256:b8c3f85900b9dc423225913c5aace94729fe1fa9763b38939a95226f02d37186
# via validators
pyyaml==6.0 \
--hash=sha256:01b45c0191e6d66c470b6cf1b9531a771a83c1c4208272ead47a3ae4f2f603bf \
--hash=sha256:0283c35a6a9fbf047493e3a0ce8d79ef5030852c51e9d911a27badfde0605293 \
--hash=sha256:055d937d65826939cb044fc8c9b08889e8c743fdc6a32b33e2390f66013e449b \
--hash=sha256:07751360502caac1c067a8132d150cf3d61339af5691fe9e87803040dbc5db57 \
--hash=sha256:0b4624f379dab24d3725ffde76559cff63d9ec94e1736b556dacdfebe5ab6d4b \
--hash=sha256:0ce82d761c532fe4ec3f87fc45688bdd3a4c1dc5e0b4a19814b9009a29baefd4 \
--hash=sha256:1e4747bc279b4f613a09eb64bba2ba602d8a6664c6ce6396a4d0cd413a50ce07 \
--hash=sha256:213c60cd50106436cc818accf5baa1aba61c0189ff610f64f4a3e8c6726218ba \
--hash=sha256:231710d57adfd809ef5d34183b8ed1eeae3f76459c18fb4a0b373ad56bedcdd9 \
--hash=sha256:277a0ef2981ca40581a47093e9e2d13b3f1fbbeffae064c1d21bfceba2030287 \
--hash=sha256:2cd5df3de48857ed0544b34e2d40e9fac445930039f3cfe4bcc592a1f836d513 \
--hash=sha256:40527857252b61eacd1d9af500c3337ba8deb8fc298940291486c465c8b46ec0 \
--hash=sha256:432557aa2c09802be39460360ddffd48156e30721f5e8d917f01d31694216782 \
--hash=sha256:473f9edb243cb1935ab5a084eb238d842fb8f404ed2193a915d1784b5a6b5fc0 \
--hash=sha256:48c346915c114f5fdb3ead70312bd042a953a8ce5c7106d5bfb1a5254e47da92 \
--hash=sha256:50602afada6d6cbfad699b0c7bb50d5ccffa7e46a3d738092afddc1f9758427f \
--hash=sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2 \
--hash=sha256:77f396e6ef4c73fdc33a9157446466f1cff553d979bd00ecb64385760c6babdc \
--hash=sha256:81957921f441d50af23654aa6c5e5eaf9b06aba7f0a19c18a538dc7ef291c5a1 \
--hash=sha256:819b3830a1543db06c4d4b865e70ded25be52a2e0631ccd2f6a47a2822f2fd7c \
--hash=sha256:897b80890765f037df3403d22bab41627ca8811ae55e9a722fd0392850ec4d86 \
--hash=sha256:98c4d36e99714e55cfbaaee6dd5badbc9a1ec339ebfc3b1f52e293aee6bb71a4 \
--hash=sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c \
--hash=sha256:9fa600030013c4de8165339db93d182b9431076eb98eb40ee068700c9c813e34 \
--hash=sha256:a80a78046a72361de73f8f395f1f1e49f956c6be882eed58505a15f3e430962b \
--hash=sha256:afa17f5bc4d1b10afd4466fd3a44dc0e245382deca5b3c353d8b757f9e3ecb8d \
--hash=sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c \
--hash=sha256:b5b9eccad747aabaaffbc6064800670f0c297e52c12754eb1d976c57e4f74dcb \
--hash=sha256:bfaef573a63ba8923503d27530362590ff4f576c626d86a9fed95822a8255fd7 \
--hash=sha256:c5687b8d43cf58545ade1fe3e055f70eac7a5a1a0bf42824308d868289a95737 \
--hash=sha256:cba8c411ef271aa037d7357a2bc8f9ee8b58b9965831d9e51baf703280dc73d3 \
--hash=sha256:d15a181d1ecd0d4270dc32edb46f7cb7733c7c508857278d3d378d14d606db2d \
--hash=sha256:d4b0ba9512519522b118090257be113b9468d804b19d63c71dbcf4a48fa32358 \
--hash=sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53 \
--hash=sha256:d4eccecf9adf6fbcc6861a38015c2a64f38b9d94838ac1810a9023a0609e1b78 \
--hash=sha256:d67d839ede4ed1b28a4e8909735fc992a923cdb84e618544973d7dfc71540803 \
--hash=sha256:daf496c58a8c52083df09b80c860005194014c3698698d1a57cbcfa182142a3a \
--hash=sha256:dbad0e9d368bb989f4515da330b88a057617d16b6a8245084f1b05400f24609f \
--hash=sha256:e61ceaab6f49fb8bdfaa0f92c4b57bcfbea54c09277b1b4f7ac376bfb7a7c174 \
--hash=sha256:f84fbc98b019fef2ee9a1cb3ce93e3187a6df0b2538a651bfb890254ba9f90b5
# via -r tools/requirements.in
validators==0.20.0 \
--hash=sha256:24148ce4e64100a2d5e267233e23e7afeb55316b47d30faae7eb6e7292bc226a
# via -r tools/requirements.in
Empty file modified tools/version_test.py 100755 → 100644
Empty file.

0 comments on commit ddc9cd6

Please sign in to comment.