Skip to content

Commit

Permalink
Read the Python version from toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm committed May 24, 2024
1 parent d798106 commit 4cbc6c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
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

0 comments on commit 4cbc6c0

Please sign in to comment.