Skip to content

Commit

Permalink
Port to use bzlmod and hermetic Python
Browse files Browse the repository at this point in the history
Respell WORKSPACE.bazel into MODULE.bazel + WORKSPACE.bzlmod. For now,
this means that our skylark external dependencies (like rules_python)
are not upgraded by tools/upgrade.sh -- there's a TODO instead.

Switch from the Ubuntu Python interpreter to a Bazel one (downloaded
as an external). Since any given version of Blender only works with
one specific version of CPython, this will make it easier to stay in
sync long-term. This also lets us from the unittest_path_cleaner
hack to avoid test pollution from Ubuntu libraries.

Switch to using a pip() helper macro to specify the pip deps for our
programs. The old long-hand spellings don't work with bzlmod.

Switch to rules_python runfiles instead of bazel_tools runfiles.

Add test/BUILD.bazel as a sub-package; writing out all of the test
rules in the top-level BUILD.bazel was too much clutter.
  • Loading branch information
jwnimmer-tri committed May 17, 2024
1 parent 8fda070 commit 478e79c
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 187 deletions.
3 changes: 0 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# SPDX-License-Identifier: BSD-2-Clause

# Don't use bzlmod yet.
common --enable_bzlmod=false

common --symlink_prefix=.bazel/
common --keep_going=yes
build --test_output=errors
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: BSD-2-Clause

/.bazel
/MODULE.bazel.lock
52 changes: 6 additions & 46 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: BSD-2-Clause

load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("//tools:defs.bzl", "bazel_lint_test", "py_lint_test")
load("//tools:defs.bzl", "bazel_lint_test", "pip", "py_lint_test")

exports_files([
"pyproject.toml",
Expand All @@ -14,48 +14,17 @@ py_binary(
srcs = ["server.py"],
visibility = ["//visibility:public"],
deps = [
"@requirements_bpy//:pkg",
"@requirements_flask//:pkg",
],
)

py_library(
name = "unittest_path_cleaner",
srcs = ["test/unittest_path_cleaner.py"],
)

py_test(
name = "server_test",
srcs = ["test/server_test.py"],
data = [
":server",
# This texture file is a dependency for `one_texture_box.blend`.
"test/4_color_texture.png",
"test/depth.png",
"test/label.png",
# TODO(zachfang): Consider generating this image in the test code if
# it's easier to maintain for our future use cases.
"test/one_gltf_one_blend.label.png",
"test/one_rgba_box.gltf",
"test/one_rgba_one_texture_boxes.color.png",
"test/one_rgba_one_texture_boxes.gltf",
"test/one_texture_box.blend",
"test/two_rgba_boxes.color.png",
"test/two_rgba_boxes.gltf",
],
deps = [
":unittest_path_cleaner",
"@test_requirements_numpy//:pkg",
"@test_requirements_pillow//:pkg",
"@test_requirements_requests//:pkg",
pip("bpy"),
pip("flask"),
],
)

bazel_lint_test(
name = "bazel_lint_test",
srcs = [
"BUILD.bazel",
"WORKSPACE.bazel",
"MODULE.bazel",
"WORKSPACE.bzlmod",
],
)

Expand All @@ -64,8 +33,6 @@ py_lint_test(
srcs = [
"bazel",
"server.py",
"test/server_test.py",
"test/unittest_path_cleaner.py",
],
)

Expand All @@ -75,10 +42,3 @@ compile_pip_requirements(
requirements_txt = "requirements.txt",
tags = ["manual"],
)

compile_pip_requirements(
name = "test_requirements",
requirements_in = "test/requirements.in",
requirements_txt = "test/requirements.txt",
tags = ["manual"],
)
51 changes: 51 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SPDX-License-Identifier: BSD-2-Clause

module(name = "drake_blender")

bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_python", version = "0.32.2")

# For the most part, a given version of Blender only supports exactly one
# version of Python, so we'll pin ourselves to exactly that version.
PYTHON_VERSION = "3.10"

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = PYTHON_VERSION)
use_repo(python, "python_versions")

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "requirements",
python_version = PYTHON_VERSION,
requirements_lock = "//:requirements.txt",
)
pip.parse(
hub_name = "test_requirements",
python_version = PYTHON_VERSION,
requirements_lock = "//:test/requirements.txt",
)
pip.parse(
hub_name = "examples_requirements",
python_version = PYTHON_VERSION,
requirements_lock = "//examples:requirements.txt",
)
use_repo(pip, "requirements")
use_repo(pip, "test_requirements")
use_repo(pip, "examples_requirements")

http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

# This is a sample file from https://www.blender.org/download/demo-files/,
# licensed under CC0. Credit goes to Ramil Roosileht for creating it,
# https://twitter.com/limarest_art.
http_file(
name = "color_attribute_painting",
sha256 = "443b213229a4c863b2015beff623a700886c14928707a2fb24a6dd85fd80a207",
urls = [
base + "/demo/sculpt_mode/color_attribute_painting.blend"
for base in [
"https://mirrors.ocf.berkeley.edu/blender",
"https://mirror.clarkson.edu/blender",
]
],
)
82 changes: 0 additions & 82 deletions WORKSPACE.bazel

This file was deleted.

21 changes: 21 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- bazel -*-
# SPDX-License-Identifier: BSD-2-Clause

# This file provides dependencies that are not yet resolve-able via the bzlmod
# module system. TODO(jwnimmer-tri) move this into MODULE.bazel as possible.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("//tools:workspace_versions.bzl", "WORKSPACE_VERSIONS")

[
http_file(
name = name,
executable = True,
sha256 = sha256,
url = "https://github.com/bazelbuild/buildtools/releases/download/v{}/{}".format(
WORKSPACE_VERSIONS["buildifier"]["version"],
name,
),
)
for name, sha256 in WORKSPACE_VERSIONS["buildifier"]["binaries"].items()
]
12 changes: 6 additions & 6 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("//tools:defs.bzl", "bazel_lint_test", "py_lint_test")
load("//tools:defs.bzl", "bazel_lint_test", "pip", "py_lint_test")

exports_files(glob(["**"]))

Expand All @@ -15,10 +15,10 @@ py_binary(
"@color_attribute_painting//file",
],
deps = [
"@bazel_tools//tools/python/runfiles",
"@examples_requirements_drake//:pkg",
"@examples_requirements_opencv_python//:pkg",
"@examples_requirements_tqdm//:pkg",
"@rules_python//python/runfiles",
pip("drake", "[examples]"),
pip("opencv_python", "[examples]"),
pip("tqdm", "[examples]"),
],
)

Expand All @@ -29,7 +29,7 @@ py_test(
":ball_bin",
":test/bpy_use_cycles.py",
],
deps = ["@examples_requirements_drake//:pkg"],
deps = [pip("drake", "[examples]")],
)

bazel_lint_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/ball_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import time
import typing

from bazel_tools.tools.python.runfiles import runfiles
from pydrake.common import configure_logging
from pydrake.common.yaml import yaml_load_typed
from pydrake.multibody.parsing import (
Expand All @@ -44,6 +43,7 @@
PixelType,
)
from pydrake.visualization import VideoWriter
from python import runfiles
import tqdm


Expand Down
52 changes: 52 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# SPDX-License-Identifier: BSD-2-Clause

load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("//tools:defs.bzl", "bazel_lint_test", "pip", "py_lint_test")

py_test(
name = "server_test",
srcs = ["server_test.py"],
data = [
"//:server",
# This texture file is a dependency for `one_texture_box.blend`.
"4_color_texture.png",
"depth.png",
"label.png",
# TODO(zachfang): Consider generating this image in the test code if
# it's easier to maintain for our future use cases.
"one_gltf_one_blend.label.png",
"one_rgba_box.gltf",
"one_rgba_one_texture_boxes.color.png",
"one_rgba_one_texture_boxes.gltf",
"one_texture_box.blend",
"two_rgba_boxes.color.png",
"two_rgba_boxes.gltf",
],
deps = [
pip("numpy", "[test]"),
pip("pillow", "[test]"),
pip("requests", "[test]"),
],
)

bazel_lint_test(
name = "bazel_lint_test",
srcs = [
"BUILD.bazel",
],
)

py_lint_test(
name = "py_lint_test",
srcs = [
"server_test.py",
],
)

compile_pip_requirements(
name = "requirements",
requirements_in = "requirements.in",
requirements_txt = "requirements.txt",
tags = ["manual"],
)
2 changes: 1 addition & 1 deletion test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# bazel run //:test_requirements.update
# bazel run //test:requirements.update
#
black==24.4.2 \
--hash=sha256:257d724c2c9b1660f353b36c802ccece186a30accc7742c176d29c146df6e474 \
Expand Down
6 changes: 0 additions & 6 deletions test/server_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# SPDX-License-Identifier: BSD-2-Clause

# isort: off

import test.unittest_path_cleaner # Disable Ubuntu packages.

# isort: on

from collections import namedtuple
import datetime
import json
Expand Down
6 changes: 0 additions & 6 deletions test/unittest_path_cleaner.py

This file was deleted.

Loading

0 comments on commit 478e79c

Please sign in to comment.