Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify python interpreter when installing pip dependencies for CI #656

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
4 changes: 3 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ load("@vaticle_dependencies//tool/swig:deps.bzl", swig_deps = "deps")
swig_deps()

# Load //tool/common
load("//python:python_versions.bzl", "create_interpreter_symlinks")
create_interpreter_symlinks({"python39_symlink" : "python39"})
load("@vaticle_dependencies//tool/common:deps.bzl", "vaticle_dependencies_ci_pip")
vaticle_dependencies_ci_pip()
vaticle_dependencies_ci_pip("@python39_symlink//:python")
load("@vaticle_dependencies_ci_pip//:requirements.bzl", "install_deps")
install_deps()

Expand Down
45 changes: 45 additions & 0 deletions python/python_versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,48 @@ python_versions = [
def register_all_toolchains():
for version in python_versions:
python_register_toolchains(name=version["name"], python_version=version["python_version"], ignore_root_user_error = True)

# Symlinks which allow us to use the right python interpreter
def create_interpreter_symlinks(target_names_to_version):
for (name, version) in target_names_to_version.items():
python_interpreter_symlink(name=name, version=version)

OS_NAMES = ("mac", "win", "linux")
OS_ARCHS = ("aarch64", "x86_64")
(MAC, WIN, LINUX) = OS_NAMES
(ARM64, X64) = OS_ARCHS

PYTHON_INTERPRETER_SUFFIXES = {
(MAC, ARM64) : "aarch64-apple-darwin",
(MAC, X64) : "x86_64-apple-darwin",
(LINUX, ARM64): "aarch64-unknown-linux-gnu",
(LINUX, X64): "x86_64-unknown-linux-gnu",
(WIN, X64) : "x86_64-pc-windows-msvc",
}

def _python_interpreter_symlink_impl(rctx):
os_name = None
for name in OS_NAMES:
if name in rctx.os.name:
os_name = name

if os_name == None:
fail
os_arch = rctx.os.arch
interpreter_suffix = PYTHON_INTERPRETER_SUFFIXES.get((os_name, os_arch))

resolved_interpreter_label = Label("@" + rctx.attr.version + "_" + interpreter_suffix + "//:python")
build_file_content = """
package(default_visibility = ["//visibility:public"])
exports_files(["python"])
"""
rctx.file("WORKSPACE", "workspace(name = \"%s\")"%rctx.attr.name)
rctx.file("BUILD.bazel", content=build_file_content, executable=False)
rctx.symlink(resolved_interpreter_label, "python")
Copy link
Member

Choose a reason for hiding this comment

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

Is this Windows-friendly?


python_interpreter_symlink = repository_rule(
implementation= _python_interpreter_symlink_impl,
attrs={
"version" : attr.string(mandatory=True),
}
)