Skip to content

Commit

Permalink
[doc,tools] Adjust imports and libraries in prep for bzlmod
Browse files Browse the repository at this point in the history
Re-spell imports to lose the "drake" part of the module namespace.
The old spelling of imports is dispreferred and will not work with
upcoming bzlmod.

For all Drake Python code, we should be using drake_py_library so that
all Drake-specific settings are always in effect. (Earlier in bzlmod
porting this was critically important; since then, I've changed some
options so that it's less critical but we should anyway follow this
hygiene so that we always have a single point of control for our build
rules.)
  • Loading branch information
jwnimmer-tri committed May 20, 2024
1 parent 2fb7ba9 commit 1426f9c
Show file tree
Hide file tree
Showing 43 changed files with 85 additions and 95 deletions.
2 changes: 1 addition & 1 deletion doc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from python import runfiles
import xml.etree.ElementTree as ET

from drake.doc.defs import check_call, main
from doc.defs import check_call, main


def _build(*, out_dir, temp_dir, quick, modules):
Expand Down
2 changes: 1 addition & 1 deletion doc/doxygen_cxx/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from python import runfiles

from drake.doc.defs import (
from doc.defs import (
check_call,
main,
perl_cleanup_html_output,
Expand Down
2 changes: 1 addition & 1 deletion doc/doxygen_cxx/test/system_doxygen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from textwrap import dedent, indent
import unittest

import drake.doc.doxygen_cxx.system_doxygen as mut
import doc.doxygen_cxx.system_doxygen as mut


class TestSystemDoxygen(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion doc/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import tempfile

from drake.doc.defs import (
from doc.defs import (
check_call,
main,
perl_cleanup_html_output,
Expand Down
8 changes: 4 additions & 4 deletions doc/pydrake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
from os.path import join
import sys

from drake.doc.defs import (
import pydrake._all_everything
from pydrake.common import _MangledName

from doc.defs import (
check_call,
main,
perl_cleanup_html_output,
symlink_input,
verbose,
)

import pydrake._all_everything
from pydrake.common import _MangledName


def _get_submodules(name):
prefix = name + "."
Expand Down
3 changes: 2 additions & 1 deletion doc/pydrake/pydrake_sphinx_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
from sphinx.ext import autodoc
from sphinx.util.nodes import nested_parse_with_titles

from drake.doc.doxygen_cxx.system_doxygen import system_yaml_to_html
from pydrake.common.cpp_template import TemplateBase
from pydrake.common.deprecation import DrakeDeprecationWarning

from doc.doxygen_cxx.system_doxygen import system_yaml_to_html


def rindex(s, sub):
"""Reverse index of a substring."""
Expand Down
2 changes: 1 addition & 1 deletion doc/styleguide/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from os.path import join
import textwrap

from drake.doc.defs import check_call, main, symlink_input
from doc.defs import check_call, main, symlink_input


def _add_title(*, temp_dir, filename, title):
Expand Down
6 changes: 2 additions & 4 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_binary")
load("//tools/skylark:drake_py.bzl", "drake_py_binary", "drake_py_library")
load(
"//tools/skylark:drake_runfiles_binary.bzl",
"drake_runfiles_binary",
)
load("//tools/skylark:py.bzl", "py_binary", "py_library")

package(default_visibility = ["//visibility:public"])

py_library(
drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
visibility = [":__subpackages__"],
deps = ["//:module_py"],
)

alias(
Expand Down
8 changes: 4 additions & 4 deletions tools/install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:drake_py.bzl",
"drake_py_binary",
"drake_py_library",
"drake_py_unittest",
)
load("//tools/skylark:py.bzl", "py_library")

package(default_visibility = ["//visibility:public"])

py_library(
drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
visibility = [":__subpackages__"],
deps = ["//tools:module_py"],
)

py_library(
drake_py_library(
name = "otool",
srcs = ["otool.py"],
visibility = ["//:__subpackages__"],
deps = [":module_py"],
)

py_library(
drake_py_library(
name = "install_test_helper",
testonly = 1,
srcs = ["install_test_helper.py"],
Expand Down
4 changes: 2 additions & 2 deletions tools/install/dummy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ load(
)
load(
"//tools/skylark:drake_py.bzl",
"drake_py_library",
"drake_py_unittest",
)
load("//tools/skylark:py.bzl", "py_library")

package(default_visibility = ["//tools/install:__subpackages__"])

Expand All @@ -35,7 +35,7 @@ drake_transitive_installed_hdrs_filegroup(
deps = [":dummy_private"],
)

py_library(
drake_py_library(
name = "dummy_py",
testonly = 1,
srcs = ["dummy.py"],
Expand Down
2 changes: 1 addition & 1 deletion tools/install/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from subprocess import check_output, check_call

from drake.tools.install import otool
from tools.install import otool

# Used for matching against libraries and extracting useful components.
# N.B. On linux, dynamic libraries may have their version number as a suffix
Expand Down
2 changes: 1 addition & 1 deletion tools/install/test/install_meta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from subprocess import STDOUT, check_output
import sys

import drake.tools.install.installer as installer
import tools.install.installer as installer

# TODO(eric.cousineau): Expand on these tests, especially for nuanced things
# like Python C extensions.
Expand Down
2 changes: 1 addition & 1 deletion tools/install/test/installer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os.path import isdir, join
import unittest

from drake.tools.install.installer import Installer
from tools.install.installer import Installer


class TestInstall(unittest.TestCase):
Expand Down
27 changes: 13 additions & 14 deletions tools/jupyter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
load("//tools/jupyter:jupyter_py.bzl", "drake_jupyter_py_binary")
load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:drake_py.bzl",
"drake_py_library",
"drake_py_unittest",
)
load("//tools/skylark:drake_py.bzl", "drake_py_unittest")
load("//tools/skylark:py.bzl", "py_library")

drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
deps = ["//tools:module_py"],
)
package(default_visibility = ["//visibility:private"])

drake_py_library(
# Note that we do NOT use drake_py_library here, so that our codegen via the
# JUPYTER_PY_TEMPLATE file is as simple and portable as possible. Instead,
# we'll use an `imports = ["."]` to establish a helpful `sys.path`.
py_library(
name = "jupyter_bazel_py",
srcs = ["jupyter_bazel.py"],
imports = ["."],
visibility = ["//visibility:public"],
deps = [":module_py"],
)

drake_py_library(
# Note that we do NOT use drake_py_library here, so that this program is a
# more representative demo of what and end user would experience. Instead,
# we'll use an `imports = ["."]` to establish a helpful `sys.path`.
py_library(
name = "example_library",
srcs = ["example_library.py"],
imports = [":module_py"],
imports = ["."],
)

drake_jupyter_py_binary(
Expand Down
1 change: 0 additions & 1 deletion tools/jupyter/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion tools/jupyter/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"import ipywidgets as widgets\n",
"import numpy as np\n",
"\n",
"from drake.tools.jupyter.example_library import my_func"
"from example_library import my_func"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion tools/jupyter/jupyter_py.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ load("//tools/workspace:generate_file.bzl", "generate_file")
_JUPYTER_PY_TEMPLATE = """
import sys
from drake.tools.jupyter.jupyter_bazel import _jupyter_bazel_notebook_main
from jupyter_bazel import _jupyter_bazel_notebook_main
def main():
Expand Down
2 changes: 1 addition & 1 deletion tools/lcm_gen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ load(
load("//tools/lint:lint.bzl", "add_lint_tests")

# The library target for this tool.
py_library(
drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
deps = ["//tools:module_py"],
Expand Down
2 changes: 1 addition & 1 deletion tools/lcm_gen/test/lcm_gen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from python import runfiles

from drake.tools.lcm_gen import (
from tools.lcm_gen import (
CppGen,
Parser,
PrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ load(

package(default_visibility = ["//visibility:public"])

py_library(
drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
deps = ["//tools:module_py"],
Expand Down
4 changes: 2 additions & 2 deletions tools/lint/buildifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import sys
from subprocess import Popen, PIPE, STDOUT

from drake.tools.lint.find_data import find_data
from drake.tools.lint.util import find_all_sources
from tools.lint.find_data import find_data
from tools.lint.util import find_all_sources

# These match data=[] in our BUILD.bazel file.
_BUILDIFIER = "external/buildifier/buildifier"
Expand Down
4 changes: 2 additions & 2 deletions tools/lint/clang_format_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import os
import sys

from drake.tools.lint.formatter import IncludeFormatter
from drake.tools.lint.util import find_all_sources
from tools.lint.formatter import IncludeFormatter
from tools.lint.util import find_all_sources


def main(workspace_name="drake"):
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/clang_format_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import sys

import drake.tools.lint.clang_format as clang_format_lib
import tools.lint.clang_format as clang_format_lib


def _is_cxx(filename):
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/drakelint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
import sys

from drake.tools.lint.formatter import IncludeFormatter
from tools.lint.formatter import IncludeFormatter


def _check_unguarded_openmp_uses(filename):
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from subprocess import Popen, PIPE, CalledProcessError

import drake.tools.lint.clang_format as clang_format_lib
import tools.lint.clang_format as clang_format_lib


class FormatterBase:
Expand Down
12 changes: 0 additions & 12 deletions tools/lint/python_lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ def python_lint(
PYTHON_LINT_IGNORE_DEFAULT (as strings, with the 'E' or 'W').
exclude: List of labels to exclude from linting, e.g., [:foo.py].
extra_srcs: Source files that are not discoverable via rules.
Example:
BUILD:
load("//tools/lint:python_lint.bzl", "python_lint")
load("//tools/skylark:py.bzl", "py_library")
py_library(
name = "foo",
srcs = ["foo.py"],
)
python_lint()
"""
if existing_rules == None:
existing_rules = native.existing_rules().values()
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/test/find_data_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest

from drake.tools.lint.find_data import find_data
from tools.lint.find_data import find_data


class FindDataTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/test/formatter_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from drake.tools.lint.formatter import FormatterBase, IncludeFormatter
from tools.lint.formatter import FormatterBase, IncludeFormatter


class TestFormatterBase(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/test/util_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest

from drake.tools.lint.util import find_all_sources
from tools.lint.util import find_all_sources


class UtilTest(unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tools/skylark/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_unittest")
load("//tools/skylark:py.bzl", "py_binary")
load("//tools/skylark:drake_py.bzl", "drake_py_binary", "drake_py_unittest")

package(default_visibility = ["//visibility:public"])

Expand All @@ -14,7 +13,7 @@ config_setting(
constraint_values = ["@platforms//os:osx"],
)

py_binary(
drake_py_binary(
name = "rewrite_osx_ar_hidden",
srcs = ["rewrite_osx_ar_hidden.py"],
)
Expand Down
4 changes: 2 additions & 2 deletions tools/wheel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_binary")
load("//tools/skylark:drake_py.bzl", "drake_py_binary", "drake_py_library")

exports_files(
glob(["**"]),
visibility = ["//tools:__subpackages__"],
)

py_library(
drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
visibility = [":__subpackages__"],
Expand Down
Loading

0 comments on commit 1426f9c

Please sign in to comment.