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

Read the Python version from toolchain #49

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ _common_attrs = {
"_uv": attr.label(default = "@multitool//tools/uv", executable = True, cfg = "exec"),
}

def _python_version(py_toolchain):
return "{major}.{minor}".format(
major = py_toolchain.py3_runtime.interpreter_version_info.major,
minor = py_toolchain.py3_runtime.interpreter_version_info.minor,
)

def _python_platform(maybe_python_platform):
if maybe_python_platform == "":
return ""
Expand All @@ -24,6 +30,7 @@ def _uv_pip_compile(ctx, template, executable, generator_label):
"{{requirements_in}}": ctx.file.requirements_in.short_path,
"{{requirements_txt}}": ctx.file.requirements_txt.short_path,
"{{resolved_python}}": py_toolchain.py3_runtime.interpreter.short_path,
"{{python_version}}": _python_version(py_toolchain),
"{{python_platform}}": _python_platform(ctx.attr.python_platform),
"{{label}}": str(generator_label),
},
Expand Down
10 changes: 4 additions & 6 deletions uv/private/pip_compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -euo pipefail
UV="{{uv}}"
PYTHON_PLATFORM="{{python_platform}}"
RESOLVED_PYTHON="{{resolved_python}}"
PYTHON_VERSION="{{python_version}}"
REQUIREMENTS_IN="{{requirements_in}}"
REQUIREMENTS_TXT="{{requirements_txt}}"
LABEL="{{label}}"
Expand All @@ -14,16 +15,13 @@ RESOLVED_PYTHON_BIN="$(dirname "$RESOLVED_PYTHON")"
# set resolved python to front of the path
export PATH="$RESOLVED_PYTHON_BIN:$PATH"

# get the version of python to hand to uv pip compile
PYTHON_VERSION="$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"

$UV pip compile \
--generate-hashes \
--emit-index-url \
--no-strip-extras \
--custom-compile-command "bazel run $LABEL" \
--python-version=$PYTHON_VERSION \
--python-version="$PYTHON_VERSION" \
$(echo $PYTHON_PLATFORM) \
-o $REQUIREMENTS_TXT \
$REQUIREMENTS_IN \
-o "$REQUIREMENTS_TXT" \
"$REQUIREMENTS_IN" \
"$@"
8 changes: 3 additions & 5 deletions uv/private/pip_compile_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -euo pipefail
UV="{{uv}}"
PYTHON_PLATFORM="{{python_platform}}"
RESOLVED_PYTHON="{{resolved_python}}"
PYTHON_VERSION="{{python_version}}"
REQUIREMENTS_IN="{{requirements_in}}"
REQUIREMENTS_TXT="{{requirements_txt}}"
LABEL="{{label}}"
Expand All @@ -17,20 +18,17 @@ export PATH="$RESOLVED_PYTHON_BIN:$PATH"
# make a writable copy of incoming requirements
cp "$REQUIREMENTS_TXT" __updated__

# get the version of python to hand to uv pip compile
PYTHON_VERSION="$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"

$UV pip compile \
--quiet \
--no-cache \
--generate-hashes \
--emit-index-url \
--no-strip-extras \
--custom-compile-command "bazel run ${LABEL}" \
--python-version=$PYTHON_VERSION \
--python-version="$PYTHON_VERSION" \
$(echo $PYTHON_PLATFORM) \
-o __updated__ \
$REQUIREMENTS_IN
"$REQUIREMENTS_IN"

# check files match
DIFF="$(diff "$REQUIREMENTS_TXT" "__updated__" || true)"
Expand Down