From 0f8f664852da20c7c524c1af7b3cecfbd41314f7 Mon Sep 17 00:00:00 2001 From: Thinh Ha Date: Tue, 21 Sep 2021 13:48:52 +0000 Subject: [PATCH] allow skipping py2 installation --- .../BUILD.bazel => py3_only/BUILD} | 7 -- examples/py3_only/WORKSPACE | 12 ++++ examples/py3_only/main.py | 2 + pyenv/defs.bzl | 71 +++++++++++++++---- 4 files changed, 73 insertions(+), 19 deletions(-) rename examples/{verify_versions/BUILD.bazel => py3_only/BUILD} (69%) create mode 100644 examples/py3_only/WORKSPACE create mode 100644 examples/py3_only/main.py diff --git a/examples/verify_versions/BUILD.bazel b/examples/py3_only/BUILD similarity index 69% rename from examples/verify_versions/BUILD.bazel rename to examples/py3_only/BUILD index 1bebc4b..d7d996f 100644 --- a/examples/verify_versions/BUILD.bazel +++ b/examples/py3_only/BUILD @@ -6,13 +6,6 @@ py_binary( python_version = "PY3", ) -py_binary( - name = "main_py2", - srcs = ["main.py"], - main = "main.py", - python_version = "PY2", -) - filegroup( name = "srcs", srcs = [ diff --git a/examples/py3_only/WORKSPACE b/examples/py3_only/WORKSPACE new file mode 100644 index 0000000..658dafc --- /dev/null +++ b/examples/py3_only/WORKSPACE @@ -0,0 +1,12 @@ +workspace(name = "examples_p3_only") + +local_repository( + name = "dpu_rules_pyenv", + path = "../../", +) + +load("@dpu_rules_pyenv//pyenv:defs.bzl", "pyenv_install") + +pyenv_install( + py3 = "3.7.7", +) diff --git a/examples/py3_only/main.py b/examples/py3_only/main.py new file mode 100644 index 0000000..261a651 --- /dev/null +++ b/examples/py3_only/main.py @@ -0,0 +1,2 @@ +import sys +print(sys.version) diff --git a/pyenv/defs.bzl b/pyenv/defs.bzl index 7c82999..d22a7d1 100644 --- a/pyenv/defs.bzl +++ b/pyenv/defs.bzl @@ -24,11 +24,13 @@ CONFIGURATIONS = { } } -BUILD_FILE_CONTENT = """# This file was automatically generated by @dpu_rules_pyenv//pyenv:defs.bzl +BUILD_FILE_CONTENT_HEADER = """# This file was automatically generated by @dpu_rules_pyenv//pyenv:defs.bzl package(default_visibility = ["//visibility:public"]) load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") +""" +BUILD_CONTENT_PY2_PY3 = """ # export interpreter so it can accessed as a regular file exports_files([ "{py2_dir_link}/{interpreter}", @@ -62,6 +64,49 @@ toolchain( ) """ +BUILD_CONTENT_PY3_ONLY = """ +# export interpreter so it can accessed as a regular file +exports_files([ + "{py3_dir_link}/{interpreter}" +]) + +py_runtime( + name = "python_{py3}_runtime", + files = glob(["versions/{py3}/**/*"], exclude_directories = 0), + interpreter = "versions/{py3}/{interpreter_path}", + python_version = "PY3" +) + +py_runtime_pair( + name = "runtimes", + py3_runtime = "@pyenv//:python_{py3}_runtime", +) + +toolchain( + name = "python_toolchain", + toolchain = ":runtimes", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) +""" + +def _generate_build_file_content(py2, + py3, + py2_dir_link, + py3_dir_link, + interpreter_path, + interpreter = INTERPRETER_DEFAULT_NAME): + build_file_content_template = BUILD_FILE_CONTENT_HEADER + if py2: + build_file_content_template += BUILD_CONTENT_PY2_PY3 + else: + build_file_content_template += BUILD_CONTENT_PY3_ONLY + return build_file_content_template.format(py2 = py2, + py3 = py3, + py2_dir_link = py2_dir_link, + py3_dir_link = py3_dir_link, + interpreter = interpreter, + interpreter_path = interpreter_path) + def _is_windows(repository_ctx): """Returns true when os is recognized as windows Args: @@ -147,14 +192,15 @@ def _setup_links(repository_ctx, version, dir_name): def _setup_build_files(repository_ctx, py2, py3, py2_dir, py3_dir): interpreter_dir = _get_config(repository_ctx)["interpreter_dir"] interpreter_path = "{}/{}".format(interpreter_dir, INTERPRETER_DEFAULT_NAME) if interpreter_dir else INTERPRETER_DEFAULT_NAME + build_file_content = _generate_build_file_content(py2 = py2, + py3 = py3, + py2_dir_link = py2_dir, + py3_dir_link = py3_dir, + interpreter = INTERPRETER_DEFAULT_NAME, + interpreter_path = interpreter_path) repository_ctx.file( "BUILD", - content = BUILD_FILE_CONTENT.format(py2 = py2, - py3 = py3, - py2_dir_link = py2_dir, - py3_dir_link = py3_dir, - interpreter = INTERPRETER_DEFAULT_NAME, - interpreter_path = interpreter_path) + content = build_file_content ) def _pyenv_install_impl(repository_ctx): @@ -164,18 +210,19 @@ def _pyenv_install_impl(repository_ctx): py3_dir = repository_ctx.attr.py3_dir _setup_pyenv(repository_ctx) - _install_python(repository_ctx, py2) + if py2: + _install_python(repository_ctx, py2) + _setup_links(repository_ctx, py2, py2_dir) _install_python(repository_ctx, py3) - _setup_links(repository_ctx, py2, py2_dir) _setup_links(repository_ctx, py3, py3_dir) _setup_build_files(repository_ctx, py2, py3, py2_dir, py3_dir) _pyenv_install = repository_rule( _pyenv_install_impl, attrs = { - "py2": attr.string(mandatory = True, doc = "exact version of python2"), + "py2": attr.string(mandatory = False, doc = "exact version of python2"), "py3": attr.string(mandatory = True, doc = "exact version of python3"), - "py2_dir": attr.string(mandatory = True, doc = "directory for python2"), + "py2_dir": attr.string(mandatory = False, doc = "directory for python2"), "py3_dir": attr.string(mandatory = True, doc = "directory for python3"), "hermetic": attr.bool(default = True, doc = "True if pyenv should be downloaded, False if local pyenv should be used"), "pyenv_repo": attr.string_dict(default = { @@ -191,7 +238,7 @@ _pyenv_install = repository_rule( } ) -def pyenv_install(py2, py3, py2_dir = PY2_DEFAULT_DIR_NAME, py3_dir = PY3_DEFAULT_DIR_NAME, name = DEFAULT_REPOSITORY_NAME, **kwargs): +def pyenv_install(py3, py2 = None, py3_dir = PY3_DEFAULT_DIR_NAME, py2_dir = PY2_DEFAULT_DIR_NAME, name = DEFAULT_REPOSITORY_NAME, **kwargs): """ Macro to install and register a py_runtime_pair. """