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

Add python-as-wrapper package and use it in asciidoc build. #36891

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 3 additions & 10 deletions asciidoc.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package:
name: asciidoc
version: 10.2.1
epoch: 1
epoch: 2
description: "Text based documentation"
copyright:
- license: GPL-2.0-or-later
Expand All @@ -21,7 +21,8 @@ environment:
- libxml2-utils
- libxslt
- py${{vars.py-version}}-pip
- python-${{vars.py-version}}
- python-${{vars.py-version}}-base
- python3-as-${{vars.py-version}}
- wolfi-base

pipeline:
Expand All @@ -31,10 +32,6 @@ pipeline:
tag: ${{package.version}}
expected-commit: 21e33efe96ba9a51d99d1150691dae750afd6ed1

- uses: patch
with:
patches: allow-specify-python.yaml

- runs: |
autoreconf -fi

Expand All @@ -47,12 +44,8 @@ pipeline:
--infodir=/usr/share/info

- uses: autoconf/make
with:
opts: PYTHON=python${{vars.py-version}}

- uses: autoconf/make-install
with:
opts: PYTHON=python${{vars.py-version}}

update:
enabled: true
Expand Down
77 changes: 0 additions & 77 deletions asciidoc/allow-specify-python.yaml

This file was deleted.

115 changes: 115 additions & 0 deletions python-as-wrapper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package:
name: python-as-wrapper
version: 0.1.0
epoch: 0
description: Shell script wrapper to exec python3.XX
copyright:
- license: Apache-2.0

# In a "normal" installation of python, 'python3' is a symlink
# to 'pythonX.Y' where X.Y is Major.Minor (3.13). In that scenario,
# if caller invokes 'python3', then when python reads its sys.executable,
# the executable is 'python3'.
#
# That value of sys.executable is "sticky" and finds its way into shbangs that
# are installed with 'pip install' or other places.
#
# In the vast majority of places, we do not want shebang with
# /usr/bin/python3 because at runtime:
# * python3 might be a symlink to another python version
# * python3 may not exist at all - with only python3.XX installed.
#
# This wrapper will cause the scripts generated by 'python3 -m pip install pkg'
# to be generated with a shbang of /usr/bin/python3.Y instead of /usr/bin/python3.
#
# Note: None of these packages are intended for production use.
# They are intended to be used to accomodate package build systems
# That invoke 'python3' or 'python'.
data:
- name: py-versions
items:
3.10: '310'
3.11: '311'
3.12: '312'
3.13: '313'
- name: python-as-names
items:
python: PYTHON_AS
python3: PYTHON3_AS

environment:
contents:
packages:
- busybox

pipeline:
- runs: |
chmod 755 write-strict write-env
chmod 755 test-python3-as-env test-python3-as-wrapper

subpackages:
- range: py-versions
name: python3-as-${{range.key}}
description: provides /usr/bin/python3 that execs python${{range.key}}
dependencies:
runtime:
- python-${{range.key}}-base
pipeline:
- runs: |
./write-strict "python${{range.key}}" ${{targets.contextdir}}/usr/bin/python3
test:
environment:
contents:
packages:
- python3-as-test
pipeline:
- name: validate wrapper 'python3' for ${{range.key}}
smoser marked this conversation as resolved.
Show resolved Hide resolved
runs: |
python3 /usr/bin/test-python3-as-wrapper \
python3 test-python3-as-wrapper ${{range.key}}

- range: py-versions
name: python-as-${{range.key}}
description: provides /usr/bin/python that execs python${{range.key}}
dependencies:
runtime:
- python-${{range.key}}-base
pipeline:
- runs: |
./write-strict "python${{range.key}}" ${{targets.contextdir}}/usr/bin/python
test:
environment:
contents:
packages:
- python3-as-test
pipeline:
- name: validate wrapper 'python' for ${{range.key}}
runs: |
python /usr/bin/test-python3-as-wrapper \
python test-python3-as-wrapper ${{range.key}}

- range: python-as-names
name: ${{range.key}}-as-env
description: provides /usr/bin/${{range.key}} that execs env var ${{range.value}}
pipeline:
- runs: |
./write-env "${{range.value}}" ${{targets.contextdir}}/usr/bin/${{range.key}}
test:
environment:
contents:
packages:
- python3-as-test
pipeline:
- name: validate wrapper '${{range.key}}'
runs: |
test-python3-as-env /usr/bin/${{range.key}} ${{range.value}} ${{range.key}}

- name: python3-as-test
description: provides the tests for this package
pipeline:
- runs: |
install -D -m755 "-t${{targets.contextdir}}/usr/bin" test-*

update:
enabled: false
exclude-reason: No source to watch for the new versions - this is the source.
8 changes: 8 additions & 0 deletions python-as-wrapper/env-script.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
fail() { echo "FATAL:" "$0" "$@"; exit 9; }
[ -n "${ENV_NAME}" ] || fail "invoked without ENV_NAME set"
[ "$ENV_NAME" -ef "$0" ] && fail "invoked with ENV_NAME = $0"
[ "${ENV_NAME}" != "${0##*/}" ] || fail "invoked with ENV_NAME = basename($0)"
command -v "$ENV_NAME" >/dev/null ||
fail "invoked with ENV_NAME=$ENV_NAME, but $ENV_NAME not found in PATH"
exec "${ENV_NAME}" "$@"
35 changes: 35 additions & 0 deletions python-as-wrapper/test-python3-as-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
fail() { echo "FAIL:" "$@"; exit 1; }

wrapper="$1"
varname="$2"
wrapped="$3"
[ -x "$wrapper" ] || fail "$wrapper is not executable file"
echo "PASS: $wrapper is executable"

if env "$varname=true" "$wrapper"; then
echo "PASS: $varname=true exited 0"
else
fail "$wrapper exited $? with $varname=true"
fi

if env "${varname}=false" "$wrapper"; then
fail "$wrapper exited 0 with ${varname}=false"
else
echo "PASS: ${varname}=false exited $?"
fi

# test that bad values are caught by the wrapper
# 'wrapped' here is 'python' or 'python3'. We want
# to check that potential recursion is blocked.
for val in bogus-prog-name $wrapped /usr/bin/$wrapped; do
out=$(env "$varname=$val" "$wrapper" 2>&1)
rc=$?
case "$rc" in
9) echo "PASS: $wrapper exited $rc with ${varname}=$val";;
*) echo "output: $out";
fail "$wrapper exited $rc with ${varname}=$val";;
esac
done

exit 0
12 changes: 12 additions & 0 deletions python-as-wrapper/test-python3-as-wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python3
import os, sys
wrapper = sys.argv[1]
fname = sys.argv[2]
majmin = sys.argv[3]

assert f"/usr/bin/python{majmin}" == sys.executable
assert majmin == "%s.%s" % (sys.version_info.major, sys.version_info.minor)
assert os.path.basename(sys.argv[0]) == os.path.basename(fname)
assert len(sys.argv) == 4, f"expected 4 args found {sys.argv}"

print(f"PASS: {wrapper} wrapped {sys.executable} successfully")
13 changes: 13 additions & 0 deletions python-as-wrapper/write-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh -e
input="$1"
outf="$2"
d=$(dirname "$0")
tmpl="$d/env-script.tmpl"

[ -f "$tmpl" ] ||
{ echo "no 'wrapper-env.tmpl' in dir '$d/'"; exit 1; }

mkdir -p "${outf%/*}"
sed "s,ENV_NAME,$input,g" "$tmpl" > "$outf"

chmod 755 "$outf"
11 changes: 11 additions & 0 deletions python-as-wrapper/write-strict
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh -e
input="$1"
outf="$2"
mkdir -p "${outf%/*}"

cat >"$outf" <<END
#!/bin/sh
exec "$input" "\$@"
END

chmod 755 "$outf"
Loading