-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python3: Support Python2 for lcm when being used by drake_visualizer
- Loading branch information
1 parent
d667f77
commit 1945eab
Showing
7 changed files
with
264 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from subprocess import check_call | ||
import unittest | ||
|
||
# This will check the Bazel Python version (2 or 3). | ||
from lcm import LCM | ||
|
||
|
||
class TestLcmPython2and3(unittest.TestCase): | ||
def test_python2(self): | ||
# Explicitly check Python2. | ||
check_call(["python2", "-c", "from lcm import LCM"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
load( | ||
"@python//:version.bzl", | ||
"PYTHON_EXT_SUFFIX", | ||
"PYTHON_SITE_PACKAGES_RELPATH", | ||
"PYTHON_VERSION", | ||
) | ||
load( | ||
"@python2//:version.bzl", | ||
PYTHON2_EXT_SUFFIX = "PYTHON_EXT_SUFFIX", | ||
PYTHON2_SITE_PACKAGES_RELPATH = "PYTHON_SITE_PACKAGES_RELPATH", | ||
PYTHON2_VERSION = "PYTHON_VERSION", | ||
) | ||
|
||
_BAZEL = struct( | ||
version_field = "py" + PYTHON_VERSION.split(".")[0], | ||
ext_suffix = PYTHON_EXT_SUFFIX, | ||
major = PYTHON_VERSION.split(".")[0], | ||
site_packages = PYTHON_SITE_PACKAGES_RELPATH, | ||
) | ||
_PY2 = struct( | ||
version_field = "py2", | ||
ext_suffix = PYTHON2_EXT_SUFFIX, | ||
major = PYTHON2_VERSION.split(".")[0], | ||
site_packages = PYTHON2_SITE_PACKAGES_RELPATH, | ||
) | ||
|
||
# Cannot use `type(x) == str`, etc., but can infer from literals? | ||
_list = type([]) | ||
_str = type("") | ||
_struct = type(struct()) | ||
|
||
def py_select(py2, py3): | ||
return struct(py2 = py2, py3 = py3) | ||
|
||
def py2_py3( | ||
rule, | ||
alias = None, | ||
**kwargs): | ||
""" | ||
Provides Bazel Python and Python2 targets, without duplication or conflict. | ||
If Bazel is using Python2, only a Python2 target will be defined. | ||
If Bazel using Python3, both a Python2 and Python3 target will be defined. | ||
@param rule | ||
Starlark rule (e.g. `py_library`, `cc_binary`). | ||
@param alias | ||
If specified, will create an alias to the version of the target for | ||
Bazel. | ||
@param kwargs | ||
Arguments for the rule. | ||
Arguments are resolved in the following fashion: | ||
* If an argument's value comes from `py_select`, it will be replaced | ||
based on the Python version using the `py2` or `py3` values. | ||
* Strings have the following tokens replaced: | ||
@PYTHON_SITE_PACKAGES@ | ||
@PYTHON_EXT_SUFFIX@ | ||
""" | ||
|
||
# Define Python2 target. | ||
if _PY2.major != "2": | ||
fail("@python2 has the wrong major version: {}".format(_PY2)) | ||
kwargs_py2 = _format(kwargs, _PY2) | ||
rule(**kwargs_py2) | ||
alias_actual = kwargs_py2["name"] | ||
|
||
if _BAZEL.major == "3": | ||
# Define Python3 target. | ||
kwargs_py3 = _format(kwargs, _BAZEL) | ||
alias_actual = kwargs_py3["name"] | ||
rule(**kwargs_py3) | ||
|
||
# Define alias. | ||
if alias: | ||
native.alias( | ||
name = alias, | ||
actual = alias_actual, | ||
visibility = kwargs_py2.get("visibility"), | ||
) | ||
|
||
def _format(raw, build): | ||
# Format a dict of arguments for `py2_py3`. | ||
out = dict() | ||
for key, value in raw.items(): | ||
if type(value) == _struct: | ||
value = getattr(value, build.version_field) | ||
out[key] = _format_value(value, build) | ||
return out | ||
|
||
def _format_value(value, build): | ||
# N.B. `str` and `list` do not work in this comparison. | ||
if type(value) == _str: | ||
return _format_str(value, build) | ||
elif type(value) == _list: | ||
return [_format_str(x, build) for x in value] | ||
else: | ||
return value | ||
|
||
def _format_str(value, build): | ||
subs = ( | ||
("@PYTHON_SITE_PACKAGES@", build.site_packages), | ||
("@PYTHON_EXT_SUFFIX@", build.ext_suffix), | ||
) | ||
for old, new in subs: | ||
value = value.replace(old, new) | ||
return value |
Oops, something went wrong.