Skip to content

Commit

Permalink
feat: allow skipping py2 installation
Browse files Browse the repository at this point in the history
allow skipping py2 installation
  • Loading branch information
ddlees authored Mar 3, 2022
2 parents 630936c + 0f8f664 commit 78d214a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
12 changes: 12 additions & 0 deletions examples/py3_only/WORKSPACE
Original file line number Diff line number Diff line change
@@ -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",
)
2 changes: 2 additions & 0 deletions examples/py3_only/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sys
print(sys.version)
71 changes: 59 additions & 12 deletions pyenv/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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 = {
Expand All @@ -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.
"""
Expand Down

1 comment on commit 78d214a

@alexeagle
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey you might want to come collaborate over here: bazelbuild/rules_python#618

Please sign in to comment.