Skip to content

Commit

Permalink
install: Ensure drake lcmtypes are exposed in repository (RobotLocomo…
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI authored and jwnimmer-tri committed Jan 14, 2020
1 parent e17e1a6 commit 3cfc130
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
16 changes: 15 additions & 1 deletion tools/install/bazel/drake.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package(default_visibility = ["//:__subpackages__"])

_DRAKE_RUNFILES = MANIFEST["runfiles"]["drake"]

_PYTHON_SITE_PACKAGES_RELPATH = MANIFEST["python_site_packages_relpath"]

_DRAKE_ROOT_PACKAGE_RUNFILES = [x for x in _DRAKE_RUNFILES if "/" not in x]

_EXPECTED_DRAKE_RUNFILES_PACKAGES = [
Expand Down Expand Up @@ -55,7 +57,16 @@ filegroup(
]),
)

_IMPORT = ".lib/python3.6/site-packages"
_IMPORT = "." + _PYTHON_SITE_PACKAGES_RELPATH

# N.B. This is not a standalone Python library.
# TODO(eric.cousineau): Expose this as an alias
# `@drake//lcmtypes:lcmtypes_drake_py` when it can only depend on specific
# parts of the runfiles (not all of pydrake).
py_library(
name = ".lcmtypes_drake_py",
srcs = glob(["*.py"]),
)

py_library(
name = ".pydrake",
Expand All @@ -68,6 +79,9 @@ py_library(
":.all_runfiles",
":.drake_shared_library",
],
deps = [
":.lcmtypes_drake_py",
],
imports = [
_IMPORT,
],
Expand Down
21 changes: 14 additions & 7 deletions tools/install/bazel/generate_installed_files_manifest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# vi: set ft=python :

load("@drake//tools/install:install.bzl", "InstallInfo")
load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH")

def _impl(ctx):
known_non_runfiles = [
Expand All @@ -13,18 +14,24 @@ def _impl(ctx):
"setup/requirements.txt",
]
drake_runfiles = []
drake_prologue = "share/drake/"
lcmtypes_drake_py_files = []
lcmtypes_drake_py_prologue = PYTHON_SITE_PACKAGES_RELPATH + "/drake/"
for dest in ctx.attr.target[InstallInfo].installed_files:
prologue = "share/drake/"
if not dest.startswith(prologue):
continue
drake_relative_path = dest[len(prologue):]
if drake_relative_path in known_non_runfiles:
continue
drake_runfiles.append(drake_relative_path)
if dest.startswith(drake_prologue):
relative_path = dest[len(drake_prologue):]
if relative_path in known_non_runfiles:
continue
drake_runfiles.append(relative_path)
elif dest.startswith(lcmtypes_drake_py_prologue):
relative_path = dest[len(lcmtypes_drake_py_prologue):]
lcmtypes_drake_py_files.append(relative_path)
content = {
"runfiles": {
"drake": sorted(drake_runfiles),
},
"lcmtypes_drake_py": sorted(lcmtypes_drake_py_files),
"python_site_packages_relpath": PYTHON_SITE_PACKAGES_RELPATH,
}
ctx.actions.write(
output = ctx.outputs.out,
Expand Down
10 changes: 10 additions & 0 deletions tools/install/bazel/repo_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def _drake_impl(repo_ctx):
for relpath in _MANIFEST["runfiles"]["drake"]:
repo_ctx.symlink(str(share_drake) + "/" + relpath, relpath)

# Symlink all drake LCM types to this repository's root package, since it
# should be named `drake` (see bazelbuild/bazel#3998).
if repo_ctx.name != "drake":
print("WARNING: Drake LCM types will not be importable via `drake` " +
"if this repository is not named `drake`.")
python_site_packages_relpath = _MANIFEST["python_site_packages_relpath"]
drake_lcmtypes_package = "." + python_site_packages_relpath + "/drake"
for relpath in _MANIFEST["lcmtypes_drake_py"]:
repo_ctx.symlink(drake_lcmtypes_package + "/" + relpath, relpath)

# Emit the manifest for later loading.
manifest_bzl = "MANIFEST = " + struct(**_MANIFEST).to_json()
repo_ctx.file(".manifest.bzl", content = manifest_bzl, executable = False)
Expand Down
12 changes: 12 additions & 0 deletions tools/install/bazel/test/drake_bazel_installed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def main():
deps = ["@drake//bindings/pydrake"],
)
py_test(
name = "import_all_test",
srcs = ["import_all_test.py"],
size = "small",
deps = ["@drake//bindings/pydrake"],
)
# A stub for unit testing; not required for end users.
filegroup(name = "dummy_filegroup")
""")
Expand All @@ -46,6 +53,11 @@ def main():
from pydrake.common import FindResourceOrThrow, set_log_level
set_log_level("trace")
FindResourceOrThrow("drake/examples/pendulum/Pendulum.urdf")
""")

with open(join(scratch_dir, "import_all_test.py"), "w") as f:
f.write(f"""
import pydrake.all
""")

# Check that a full `bazel test` passes.
Expand Down

0 comments on commit 3cfc130

Please sign in to comment.