Skip to content

Commit

Permalink
[pydrake] Remove some gratuitous uses of python repo constants
Browse files Browse the repository at this point in the history
Configuring the python version in a repository rule goes against best
practices for supporting toolchains (and thus, bzlmod). This commit
takes a bite out of the problem; future commits that adjust pybind
rules will finish the job.
  • Loading branch information
jwnimmer-tri committed Dec 21, 2024
1 parent 6848228 commit b9ed548
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 60 deletions.
27 changes: 2 additions & 25 deletions bindings/pydrake/multibody/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH")
load("//bindings/pydrake:pydrake.bzl", "add_lint_tests_pydrake")
load("//tools/install:install.bzl", "install", "install_files")
load("//tools/jupyter:jupyter_py.bzl", "drake_jupyter_py_binary")
Expand All @@ -18,10 +17,6 @@ load(
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
"//tools/workspace:cmake_configure_file.bzl",
"cmake_configure_file",
)

package(default_visibility = [
"//bindings/pydrake:__subpackages__",
Expand Down Expand Up @@ -298,24 +293,6 @@ drake_jupyter_py_binary(
],
)

cmake_configure_file(
name = "generate_run_installed_mesh_to_model",
src = "run_installed_mesh_to_model.py.in",
out = "run_installed_mesh_to_model.py",
defines = [
"PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
],
)

cmake_configure_file(
name = "generate_run_installed_fix_inertia",
src = "run_installed_fix_inertia.py.in",
out = "run_installed_fix_inertia.py",
defines = [
"PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
],
)

install_files(
name = "install_wrapper_scripts",
dest = "bin",
Expand Down Expand Up @@ -566,7 +543,7 @@ drake_py_unittest(

add_lint_tests_pydrake(
python_lint_extra_srcs = [
"run_installed_mesh_to_model.py.in",
"run_installed_fix_inertia.py.in",
"run_installed_mesh_to_model.py",
"run_installed_fix_inertia.py",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def main():
# Ensure that we can import pydrake, accommodating symlinks.
prefix_dir = dirname(dirname(realpath(__file__)))
assert isdir(join(prefix_dir, "bin")), f"Bad location: {prefix_dir}"
site_dir = join(prefix_dir, "@PYTHON_SITE_PACKAGES_RELPATH@")
version = f"{sys.version_info.major}.{sys.version_info.minor}"
site_dir = join(prefix_dir, f"lib/python{version}/site-packages")
sys.path.insert(0, site_dir)

# Execute the imported main.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def main():
# Ensure that we can import pydrake, accommodating symlinks.
prefix_dir = dirname(dirname(realpath(__file__)))
assert isdir(join(prefix_dir, "bin")), f"Bad location: {prefix_dir}"
site_dir = join(prefix_dir, "@PYTHON_SITE_PACKAGES_RELPATH@")
version = f"{sys.version_info.major}.{sys.version_info.minor}"
site_dir = join(prefix_dir, f"lib/python{version}/site-packages")
sys.path.insert(0, site_dir)

# Execute the imported main.
Expand Down
27 changes: 2 additions & 25 deletions bindings/pydrake/visualization/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH")
load("//bindings/pydrake:pydrake.bzl", "add_lint_tests_pydrake")
load("//tools/install:install.bzl", "install", "install_files")
load(
Expand All @@ -13,10 +12,6 @@ load(
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
"//tools/workspace:cmake_configure_file.bzl",
"cmake_configure_file",
)

package(default_visibility = [
"//bindings/pydrake:__subpackages__",
Expand Down Expand Up @@ -91,24 +86,6 @@ drake_py_library(
deps = [":visualization"],
)

cmake_configure_file(
name = "generate_run_installed_meldis",
src = "run_installed_meldis.py.in",
out = "run_installed_meldis.py",
defines = [
"PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
],
)

cmake_configure_file(
name = "generate_run_installed_model_visualizer",
src = "run_installed_model_visualizer.py.in",
out = "run_installed_model_visualizer.py",
defines = [
"PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
],
)

install_files(
name = "install_wrapper_scripts",
dest = "bin",
Expand Down Expand Up @@ -256,8 +233,8 @@ drake_py_unittest(

add_lint_tests_pydrake(
python_lint_extra_srcs = [
"run_installed_meldis.py.in",
"run_installed_model_visualizer.py.in",
"run_installed_meldis.py",
"run_installed_model_visualizer.py",
"test/visualization_install_tests.py",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def main():
# Ensure that we can import pydrake, accommodating symlinks.
prefix_dir = dirname(dirname(realpath(__file__)))
assert isdir(join(prefix_dir, "bin")), f"Bad location: {prefix_dir}"
site_dir = join(prefix_dir, "@PYTHON_SITE_PACKAGES_RELPATH@")
version = f"{sys.version_info.major}.{sys.version_info.minor}"
site_dir = join(prefix_dir, f"lib/python{version}/site-packages")
sys.path.insert(0, site_dir)

# Execute the imported main.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def main():
# Ensure that we can import pydrake, accommodating symlinks.
prefix_dir = dirname(dirname(realpath(__file__)))
assert isdir(join(prefix_dir, "bin")), f"Bad location: {prefix_dir}"
site_dir = join(prefix_dir, "@PYTHON_SITE_PACKAGES_RELPATH@")
version = f"{sys.version_info.major}.{sys.version_info.minor}"
site_dir = join(prefix_dir, f"lib/python{version}/site-packages")
sys.stdout.flush()
sys.path.insert(0, site_dir)

# Execute the imported main.
Expand Down
6 changes: 0 additions & 6 deletions tools/workspace/stable_baselines3_internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
load("@python//:version.bzl", "PYTHON_VERSION")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_unittest")

drake_py_unittest(
name = "stable_baselines3_internal_test",
# DrakeGym is only supported for Python >= 3.10.
tags = ["manual"] if PYTHON_VERSION in [
"3.8",
"3.9",
] else [],
deps = ["@stable_baselines3_internal//:stable_baselines3"],
)

Expand Down

0 comments on commit b9ed548

Please sign in to comment.