Skip to content

Commit e2fc77a

Browse files
numpy: Use later version of NumPy
setup: Remove direct dependencies on NumPy
1 parent 36bf31b commit e2fc77a

File tree

12 files changed

+150
-68
lines changed

12 files changed

+150
-68
lines changed

bindings/pydrake/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ drake_pybind_library(
4545
py_deps = [
4646
"//bindings/pydrake/util:deprecation_py",
4747
"//bindings:bazel_workaround_4594_libdrake_py",
48+
"@numpy_py",
4849
],
4950
py_srcs = [
5051
"__init__.py",

setup/mac/binary_distribution/Brewfile

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ brew 'ipopt'
1515
brew 'libyaml'
1616
brew 'lz4'
1717
brew 'nlopt'
18-
brew 'numpy'
1918
brew 'protobuf'
2019
brew 'python@2'
2120
brew 'scipy'

setup/ubuntu/16.04/binary_distribution/packages.txt

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ openjdk-8-jre
3232
python
3333
python-lxml
3434
python-matplotlib
35-
python-numpy
3635
python-pydot
3736
python-scipy
3837
python-tk

tools/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ py_binary(
2121
"//lcmtypes:lcmtypes_py",
2222
"//tools/workspace/drake_visualizer:stub_pydrake",
2323
"@drake_visualizer//:drake_visualizer_python_deps",
24+
# TODO(eric.cousineau): Move `numpy_py` dependency to a `vtk` Python
25+
# library.
26+
"@numpy_py",
2427
"@optitrack_driver//lcmtypes:py_optitrack_lcmtypes",
2528
],
2629
)

tools/workspace/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _DRAKE_EXTERNAL_PACKAGE_INSTALLS = ["@%s//:install" % p for p in [
2727
"lcm",
2828
"lcmtypes_bot2_core",
2929
"lcmtypes_robotlocomotion",
30+
"numpy_py",
3031
"octomap",
3132
"osqp",
3233
"pybind11",

tools/workspace/default.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ load("@drake//tools/workspace/libprotobuf:repository.bzl", "libprotobuf_reposito
3939
load("@drake//tools/workspace/mosek:repository.bzl", "mosek_repository")
4040
load("@drake//tools/workspace/net_sf_jchart2d:repository.bzl", "net_sf_jchart2d_repository") # noqa
4141
load("@drake//tools/workspace/nlopt:repository.bzl", "nlopt_repository")
42-
load("@drake//tools/workspace/numpy:repository.bzl", "numpy_repository")
42+
load("@drake//tools/workspace/numpy:repository.bzl", "numpy_py_repository")
4343
load("@drake//tools/workspace/octomap:repository.bzl", "octomap_repository")
4444
load("@drake//tools/workspace/optitrack_driver:repository.bzl", "optitrack_driver_repository") # noqa
4545
load("@drake//tools/workspace/org_apache_xmlgraphics_commons:repository.bzl", "org_apache_xmlgraphics_commons_repository") # noqa
@@ -154,8 +154,8 @@ def add_default_repositories(excludes = [], mirrors = DEFAULT_MIRRORS):
154154
net_sf_jchart2d_repository(name = "net_sf_jchart2d", mirrors = mirrors)
155155
if "nlopt" not in excludes:
156156
nlopt_repository(name = "nlopt")
157-
if "numpy" not in excludes:
158-
numpy_repository(name = "numpy")
157+
if "numpy_py" not in excludes:
158+
numpy_py_repository(name = "numpy_py")
159159
if "octomap" not in excludes:
160160
octomap_repository(name = "octomap", mirrors = mirrors)
161161
if "optitrack_driver" not in excludes:

tools/workspace/numpy/BUILD.bazel

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
# -*- python -*-
22

3-
# This file exists to make our directory into a Bazel package, so that our
4-
# neighboring *.bzl file can be loaded elsewhere.
5-
63
load("//tools/lint:lint.bzl", "add_lint_tests")
4+
load(
5+
"//tools/skylark:drake_py.bzl",
6+
"drake_py_unittest",
7+
)
8+
9+
drake_py_unittest(
10+
name = "numpy_test",
11+
deps = ["@numpy_py"],
12+
)
13+
14+
# Used in `./package.BUILD.bazel`, in `install(..., install_tests = [...])`.
15+
exports_files(
16+
srcs = ["test/numpy_install_test.py"],
17+
)
718

8-
add_lint_tests()
19+
add_lint_tests(
20+
python_lint_extra_srcs = [
21+
":test/numpy_install_test.py",
22+
],
23+
)
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- python -*-
2+
3+
load(
4+
"@drake//tools/install:install.bzl",
5+
"install",
6+
)
7+
8+
licenses([
9+
"notice", # BSD-2-Clause AND BSD-3-Clause AND MIT AND Python-2.0
10+
"unencumbered", # Public-Domain
11+
])
12+
13+
# Interpret `numpy` sources as data to simplify handling.
14+
filegroup(
15+
name = "data",
16+
srcs = glob(["numpy/**/*"]),
17+
visibility = ["//visibility:private"],
18+
)
19+
20+
py_library(
21+
name = "numpy_py",
22+
data = [":data"],
23+
imports = ["."],
24+
visibility = ["//visibility:public"],
25+
)
26+
27+
install(
28+
name = "install",
29+
install_tests = [
30+
"@drake//tools/workspace/numpy:test/numpy_install_test.py",
31+
],
32+
data = [":data"],
33+
data_dest = "lib/python2.7/site-packages",
34+
visibility = ["//visibility:public"],
35+
)

tools/workspace/numpy/repository.bzl

+50-58
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,74 @@
22
# vi: set ft=python :
33

44
"""
5-
Finds local system NumPy headers and makes them available to be used as a
6-
C/C++ dependency.
5+
Provides NumPy from a wheel file.
76
87
Example:
98
WORKSPACE:
10-
load("@drake//tools/workspace/numpy:repo.bzl", "numpy_repository")
11-
numpy_repository(
9+
load("@drake//tools/workspace/numpy:repo.bzl", "numpy_py_repository")
10+
numpy_py_repository(
1211
name = "foo",
13-
python_version = "2",
1412
)
1513
1614
BUILD:
17-
cc_library(
15+
py_library(
1816
name = "foobar",
19-
deps = ["@foo//:numpy"],
20-
srcs = ["bar.cc"],
17+
deps = ["@foo//:numpy_py"],
18+
srcs = ["bar.py"],
2119
)
2220
2321
Arguments:
2422
name: A unique name for this rule.
25-
python_version: The major or major.minor version of Python for which NumPy
26-
headers are to be found.
2723
"""
2824

29-
load("@drake//tools/workspace:execute.bzl", "which")
25+
load("@drake//tools/workspace:os.bzl", "determine_os")
3026

31-
def _impl(repository_ctx):
32-
python = which(repository_ctx, "python{}".format(
33-
repository_ctx.attr.python_version,
34-
))
35-
36-
if not python:
37-
fail("Could NOT find python{}".format(repository_ctx.attr.version))
38-
39-
result = repository_ctx.execute([
40-
python,
41-
"-c",
42-
"; ".join([
43-
"from __future__ import print_function",
44-
"import numpy",
45-
"print(numpy.get_include())",
46-
]),
47-
])
48-
49-
if result.return_code != 0:
50-
fail("Could NOT determine NumPy include", attr = result.stderr)
51-
52-
source = repository_ctx.path(result.stdout.strip())
53-
destination = repository_ctx.path("include")
54-
repository_ctx.symlink(source, destination)
55-
56-
file_content = """# -*- python -*-
27+
# See: https://pypi.org/project/numpy/#files
28+
wheels = {
29+
"ubuntu_16.04": {
30+
"url": "https://files.pythonhosted.org/packages/40/c5/f1ed15dd931d6667b40f1ab1c2fe1f26805fc2b6c3e25e45664f838de9d0/numpy-1.15.2-cp27-cp27mu-manylinux1_x86_64.whl", # noqa
31+
"sha256": "82f00a1e2695a0e5b89879aa25ea614530b8ebdca6d49d4834843d498e8a5e92", # noqa
32+
},
33+
"mac": {
34+
"url": "https://files.pythonhosted.org/packages/d6/47/447d4e08e18c4f0e7f935db24d8afcfc9026a84002c0e5d85103c14baaf1/numpy-1.15.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl", # noqa
35+
"sha256": "b5ff7dae352fd9e1edddad1348698e9fea14064460a7e39121ef9526745802e6", # noqa
36+
},
37+
}
5738

58-
# DO NOT EDIT: generated by numpy_repository()
59-
60-
licenses([
61-
"notice", # BSD-2-Clause AND BSD-3-Clause AND MIT AND Python-2.0
62-
"unencumbered", # Public-Domain
63-
])
64-
65-
cc_library(
66-
name = "numpy",
67-
hdrs = glob(["include/**"]),
68-
includes = ["include"],
69-
visibility = ["//visibility:public"],
70-
)
71-
"""
72-
73-
repository_ctx.file(
39+
def _impl(repository_ctx):
40+
# Do not name this `numpy`, as Bazel will confuse `PYTHONPATH`:
41+
# If `numpy` is located in the repo root, `random` will be shadowed by
42+
# `numpy.random`, causing everything to fail.
43+
# If installed elsewhere, Bazel will put an autogenerated `__init__.py` at
44+
# external directory's root, leak the wrong path, and shadow the real
45+
# `numpy`. See https://github.com/bazelbuild/bazel/issues/3998
46+
if repository_ctx.name == "numpy":
47+
fail("Do not name this repository `numpy`. Please name it " +
48+
"`numpy_py` or something else.")
49+
50+
os_result = determine_os(repository_ctx)
51+
if os_result.error != None:
52+
fail(os_result.error)
53+
54+
wheel = None
55+
if os_result.is_macos:
56+
wheel = wheels["mac"]
57+
elif os_result.is_ubuntu:
58+
key = "ubuntu_" + os_result.ubuntu_release
59+
wheel = wheels.get(key)
60+
if wheel == None:
61+
fail("Unsupported platform")
62+
63+
repository_ctx.download_and_extract(
64+
url = wheel["url"],
65+
sha256 = wheel["sha256"],
66+
type = "zip",
67+
)
68+
repository_ctx.symlink(
69+
Label("@drake//tools/workspace/numpy:package.BUILD.bazel"),
7470
"BUILD.bazel",
75-
content = file_content,
76-
executable = False,
7771
)
7872

79-
numpy_repository = repository_rule(
73+
numpy_py_repository = repository_rule(
8074
_impl,
81-
attrs = {"python_version": attr.string(default = "2")},
82-
local = True,
8375
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python2
2+
3+
"""
4+
Tests that the Drake-installed version of NumPy is used.
5+
"""
6+
7+
import numpy as np
8+
import unittest
9+
10+
11+
class TestNumpyInstall(unittest.TestCase):
12+
def test_installed_numpy(self):
13+
# Check the NumPy version.
14+
# N.B. This should be updated each time the version is bumped.
15+
self.assertEqual(np.version.version, "1.15.2")
16+
17+
18+
# Define a main since this is not run via `drake_py_unittest`.
19+
if __name__ == '__main__':
20+
unittest.main()
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Tests that we can import NumPy, and have the version that we desire.
3+
"""
4+
5+
import numpy as np
6+
7+
import numpy as np
8+
import unittest
9+
10+
11+
class TestNumpyInstall(unittest.TestCase):
12+
def test_bazel_numpy(self):
13+
self.assertIn(
14+
".runfiles", np.__file__,
15+
"You must use the Bazel-specified `numpy`! Please ensure your "
16+
"WORKSPACE and usage of `numpy_py_repository` are well-formed.")
17+
# N.B. This should be updated each time the version is bumped.
18+
self.assertEqual("1.15.2", np.version.version)

tools/workspace/pybind11/package.BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ cc_library(
4444
includes = ["include"],
4545
deps = [
4646
"@eigen",
47-
"@numpy",
4847
"@python",
4948
],
5049
)

0 commit comments

Comments
 (0)