Skip to content

Commit

Permalink
Merge pull request tweag#103 from tweag/python-toolchain-platform-run…
Browse files Browse the repository at this point in the history
…time

nixpkgs_python_configure: Use platform toolchain
  • Loading branch information
aherrmann authored Feb 10, 2020
2 parents adfe991 + 7e7f5a5 commit 49d527c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 45 deletions.
11 changes: 10 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ nixpkgs_package(

nixpkgs_cc_configure(repository = "@remote_nixpkgs")

nixpkgs_python_configure(repository = "@remote_nixpkgs")
nixpkgs_python_configure(
python2_attribute_path = "python2",
repository = "@remote_nixpkgs",
)

nixpkgs_package(
name = "nixpkgs_python_configure_test",
nix_file = "//tests:python-test.nix",
repository = "@remote_nixpkgs",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
53 changes: 32 additions & 21 deletions nixpkgs/nixpkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ def _nixpkgs_package_impl(repository_ctx):

# ensure that the output is a directory
test_path = repository_ctx.which("test")
_execute_or_fail(repository_ctx, [test_path, "-d", output_path],
failure_message = "nixpkgs_package '@{}' outputs a single file which is not supported by rules_nixpkgs. Please only use directories.".format(
repository_ctx.name
),
_execute_or_fail(
repository_ctx,
[test_path, "-d", output_path],
failure_message = "nixpkgs_package '@{}' outputs a single file which is not supported by rules_nixpkgs. Please only use directories.".format(
repository_ctx.name,
),
)

# Build a forest of symlinks (like new_local_package() does) to the
Expand Down Expand Up @@ -357,14 +359,27 @@ _nixpkgs_python_toolchain = repository_rule(
},
)

_python_build_file_content = """
py_runtime(
name = "runtime",
files = glob(["**"]),
interpreter = "{bin_path}",
python_version = "{version}",
visibility = ["//visibility:public"],
)
_python_nix_file_content = """
with import <nixpkgs> {{ config = {{}}; overlays = []; }};
runCommand "bazel-nixpkgs-python-toolchain"
{{ executable = false;
# Pointless to do this on a remote machine.
preferLocalBuild = true;
allowSubstitutes = false;
}}
''
n=$out/BUILD.bazel
mkdir -p "$(dirname "$n")"
cat >>$n <<EOF
py_runtime(
name = "runtime",
interpreter_path = "${{{attribute_path}}}/{bin_path}",
python_version = "{version}",
visibility = ["//visibility:public"],
)
EOF
''
"""

def nixpkgs_python_configure(
Expand All @@ -375,8 +390,6 @@ def nixpkgs_python_configure(
python3_bin_path = "bin/python",
repository = None,
repositories = {},
nix_file = None,
nix_file_content = None,
nix_file_deps = None,
nixopts = [],
fail_not_supported = True):
Expand All @@ -402,8 +415,6 @@ def nixpkgs_python_configure(
kwargs = dict(
repository = repository,
repositories = repositories,
nix_file = nix_file,
nix_file_content = nix_file_content,
nix_file_deps = nix_file_deps,
nixopts = nixopts,
fail_not_supported = fail_not_supported,
Expand All @@ -413,8 +424,8 @@ def nixpkgs_python_configure(
python2_runtime = "@%s_python2//:runtime" % name
nixpkgs_package(
name = name + "_python2",
attribute_path = python2_attribute_path,
build_file_content = _python_build_file_content.format(
nix_file_content = _python_nix_file_content.format(
attribute_path = python2_attribute_path,
bin_path = python2_bin_path,
version = "PY2",
),
Expand All @@ -425,8 +436,8 @@ def nixpkgs_python_configure(
python3_runtime = "@%s_python3//:runtime" % name
nixpkgs_package(
name = name + "_python3",
attribute_path = python3_attribute_path,
build_file_content = _python_build_file_content.format(
nix_file_content = _python_nix_file_content.format(
attribute_path = python3_attribute_path,
bin_path = python3_bin_path,
version = "PY3",
),
Expand Down Expand Up @@ -596,7 +607,7 @@ def _find_children(repository_ctx, target_dir):
"-print0",
]
exec_result = _execute_or_fail(repository_ctx, find_args)
return exec_result.stdout.rstrip("\0").split("\0")
return exec_result.stdout.rstrip("\000").split("\000")

def _executable_path(repository_ctx, exe_name, extra_msg = ""):
"""Try to find the executable, fail with an error."""
Expand Down
9 changes: 5 additions & 4 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ cc_binary(
)

# Test nixpkgs_python_configure() by running some Python code.
py_test(
test_suite(
name = "python-test",
srcs = ["python-test.py"],
python_version = "PY3",
srcs_version = "PY3",
tests = [
"@nixpkgs_python_configure_test//:python2-test",
"@nixpkgs_python_configure_test//:python3-test",
],
)

# Test nixpkgs_sh_posix_configure() checking that Unix commands are in Nix store.
Expand Down
48 changes: 48 additions & 0 deletions tests/python-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
with import <nixpkgs> { config = {}; overlays = []; };
runCommand "test-nixpkgs-python-toolchain"
{ executable = false; }
''
mkdir -p $out
cat >$out/BUILD.bazel <<'EOF_BUILD'
py_test(
name = "python2-test",
main = "python-test.py",
srcs = ["python-test.py"],
python_version = "PY2",
srcs_version = "PY2",
visibility = ["//visibility:public"],
)
py_test(
name = "python3-test",
main = "python-test.py",
srcs = ["python-test.py"],
python_version = "PY3",
srcs_version = "PY3",
visibility = ["//visibility:public"],
)
EOF_BUILD
cat >$out/python-test.py <<'EOF_PYTHON'
import os
import sys
_failure_message = """\
Python interpreter is not provided by the toolchain.
Expected: '{expected}'
Actual: '{actual}'.
"""
if __name__ == "__main__":
if sys.version_info.major == 2:
python_bin = "${python2}/bin/python"
else:
python_bin = "${python3}/bin/python"
if not sys.executable == python_bin:
sys.stderr.write(_failure_message.format(
expected = python_bin,
actual = sys.executable,
))
sys.exit(1)
EOF_PYTHON
''
19 changes: 0 additions & 19 deletions tests/python-test.py

This file was deleted.

0 comments on commit 49d527c

Please sign in to comment.