diff --git a/tools/workspace/drake_models/BUILD.bazel b/tools/workspace/drake_models/BUILD.bazel index 67914ea7e0a0..5618a72c03c0 100644 --- a/tools/workspace/drake_models/BUILD.bazel +++ b/tools/workspace/drake_models/BUILD.bazel @@ -1,3 +1,22 @@ +load("//doc:defs.bzl", "enumerate_filegroup") +load("//tools/skylark:drake_py.bzl", "drake_py_unittest") load("//tools/lint:lint.bzl", "add_lint_tests") +enumerate_filegroup( + name = "inventory.txt", + data = ["@drake_models"], +) + +drake_py_unittest( + name = "parse_test", + data = [ + ":inventory.txt", + "@drake_models", + ], + deps = [ + "//bindings/pydrake", + "@bazel_tools//tools/python/runfiles", + ], +) + add_lint_tests() diff --git a/tools/workspace/drake_models/test/parse_test.py b/tools/workspace/drake_models/test/parse_test.py new file mode 100644 index 000000000000..c326dbdd9d61 --- /dev/null +++ b/tools/workspace/drake_models/test/parse_test.py @@ -0,0 +1,110 @@ +from pathlib import Path +from typing import Iterator +import unittest + +from bazel_tools.tools.python.runfiles import runfiles + +from pydrake.planning import RobotDiagramBuilder + + +class TestDrakeModels(unittest.TestCase): + + @staticmethod + def package_xml() -> Path: + """Returns the path to drake_models/package.xml in runfiles. + """ + manifest = runfiles.Create() + result = Path(manifest.Rlocation("drake_models/package.xml")) + assert result.exists(), result + return result + + @staticmethod + def inventory() -> Iterator[str]: + """Generates the list of all the runfiles of @drake_models. The result + is strings like "drake_models/veggies/pepper.png". + """ + manifest = runfiles.Create() + inventory = Path(manifest.Rlocation( + "drake/tools/workspace/drake_models/inventory.txt")) + with open(inventory, encoding="utf-8") as f: + for line in f.readlines(): + assert line.startswith("drake_models/") + yield line.strip() + + @staticmethod + def get_all_models() -> Iterator[str]: + """Generates the Paths of all top-level model files in @drake_models. + The result is URLs like "package://drake_models/veggies/pepper.sdf". + """ + for path in TestDrakeModels.inventory(): + url = f"package://{path}" + if any([path.endswith(".dmd.yaml"), + path.endswith(".sdf"), + path.endswith(".urdf")]): + yield url + elif path.endswith(".xml"): + # We can't tell a MuJoCo file just by its suffix. + runfiles = TestDrakeModels.package_xml().parent.parent + if "