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

An attempt to run Python exe in a more hermetic way #863

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def _python_repository_impl(rctx):
"share/**",
]

python_wrapper = "python_wrapper.sh"
python_wrapper_content = """\
#!/bin/bash

set -o errexit -o nounset -o pipefail

external/{repo_name}/{python_bin} -B -s -I "$@"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want -S (upper case) as well?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think this would harm in the case a custom toolchain from this repo is used. In the case a system python is used, this may even be desirable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the "external/" part here might depend on if a flag is set?
https://bazel.build/reference/command-line-reference#flag--legacy_external_runfiles

I think you can drop the "external/" part, but not 100% sure.

Actually, what's the CWD when this is invoked? The relative path here means it would have to be the runfiles root. But what is doing that chdir()? (Sorry if I missed it)

Copy link
Author

@mvukov mvukov Oct 25, 2022

Choose a reason for hiding this comment

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

Thanks for pointing this out. If --nolegacy_external_runfiles, then this path doesn't work. The CWD in my example is

/home/mvukov/.cache/bazel/_bazel_mvukov/bc9f8eb0563a76ddb0353ce7869ddf8d/execroot/hermetic_python/bazel-out/k8-fastbuild/bin/demo.runfiles/hermetic_python

where hermetic_python is the repo name. When --legacy_external_runfiles is used, then external/ has to be used...

But what is doing that chdir()? (Sorry if I missed it)

I'm not doing any chdir in this PR :) Can you be a bit more specific?

""".format(repo_name = rctx.name, python_bin = python_bin)
rctx.file(python_wrapper, python_wrapper_content, executable = True)

build_content = """\
# Generated by python/repositories.bzl

Expand Down Expand Up @@ -235,8 +245,8 @@ exports_files(["python", "{python_path}"])

py_runtime(
name = "py3_runtime",
files = [":files"],
interpreter = "{python_path}",
files = [":files", "{python_wrapper}"],
interpreter = "{python_wrapper}",
python_version = "PY3",
)

Expand All @@ -248,10 +258,11 @@ py_runtime_pair(
""".format(
glob_include = repr(glob_include),
python_path = python_bin,
python_wrapper = python_wrapper,
python_version = python_short_version,
)
rctx.delete("python")
rctx.symlink(python_bin, "python")
rctx.symlink(python_wrapper, "python")
rctx.file(STANDALONE_INTERPRETER_FILENAME, "# File intentionally left blank. Indicates that this is an interpreter repo created by rules_python.")
rctx.file("BUILD.bazel", build_content)

Expand Down