#!/bin/bash
# Build packages with dh-virtualenv (sequence, --builtin-venv and the
# buildsystem), check that they do not hold the build path, then
# install, use and purge them. Runs offline: the virtualenvs use the
# system setuptools and pip does not reach the index.
set -u

WORK=${AUTOPKGTEST_TMP:-$(mktemp -d)}
export PIP_NO_INDEX=1 PIP_NO_BUILD_ISOLATION=0
FAILED=0
ok() { echo "ok: $*"; }
fail() { echo "FAIL: $*"; FAILED=1; }
check() {
    local msg=$1; shift
    if "$@"; then ok "$msg"; else fail "$msg"; fi
}

# make_source DIR RULES: a project with a console script and a local
# requirement, packaged with the given debian/rules
make_source() {
    local dir=$1
    mkdir -p "$dir/demo" "$dir/helper/helper" "$dir/debian"
    cat > "$dir/setup.py" <<'EOF'
from setuptools import setup

setup(name='demo', version='0.1', packages=['demo'],
      entry_points={'console_scripts': ['demo = demo:main']})
EOF
    cat > "$dir/demo/__init__.py" <<'EOF'
import helper


def main():
    print('demo ok', helper.status())
EOF
    cat > "$dir/helper/pyproject.toml" <<'EOF'
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "helper"
version = "1.0"
EOF
    printf 'def status():\n    return "helper ok"\n' > "$dir/helper/helper/__init__.py"
    echo ./helper > "$dir/requirements.txt"
    cat > "$dir/debian/control" <<'EOF'
Source: demo
Section: python
Maintainer: Test <test@example.org>
Build-Depends: debhelper-compat (= 13), dh-virtualenv

Package: demo
Architecture: any
Depends: ${misc:Depends}
Description: dh-virtualenv test package
 Test package.
EOF
    cat > "$dir/debian/changelog" <<'EOF'
demo (0.1) unstable; urgency=medium

  * Test.

 -- Test <test@example.org>  Wed, 01 Jan 2020 00:00:00 +0000
EOF
    printf '#!/usr/bin/make -f\n%s\n' "$2" > "$dir/debian/rules"
    chmod +x "$dir/debian/rules"
}

# build DIR [DEB_BUILD_OPTIONS]
build() {
    (cd "$1" && DEB_BUILD_OPTIONS=${2:-} dpkg-buildpackage -us -uc -b) \
        > "$1.log" 2>&1
}

MINOR=$(python3 -c 'import sys; print(sys.version_info[1])')
PYTHON3_DEPENDS="python3 (>= 3.$MINOR~), python3 (<< 3.$((MINOR + 1))~)"

# deps_contain DEB DEPENDS: each item of DEPENDS is one of the Depends of DEB
deps_contain() {
    local have want
    have=$(dpkg-deb -f "$1" Depends | sed 's/, */\n/g')
    while read -r want; do
        grep -qxF "$want" <<<"$have" || return 1
    done < <(sed 's/, */\n/g' <<<"$2")
}

# check_package NAME VENV [DEPENDS]: build, inspect, install, use and
# purge; DEPENDS is the dependency on Python expected in the package
check_package() {
    local name=$1 venv=$2 depends=${3:-$PYTHON3_DEPENDS} dir=$WORK/$1/demo
    echo "== $name"
    if ! build "$dir"; then
        fail "$name: build"; tail -30 "$dir.log"; return
    fi
    local deb tree=$WORK/$name.tree
    deb=$(ls "$WORK/$name"/demo_0.1_*.deb)
    dpkg-deb -x "$deb" "$tree"
    check "$name: no build path in the package" \
        sh -c "! grep -rqF '$WORK/$name' '$tree'"
    check "$name: bytecode records $venv" python3 - "$tree" "$venv" <<'EOF'
import glob, marshal, sys
tree, venv = sys.argv[1:]
pycs = glob.glob(tree + venv + '/lib/*/site-packages/demo/__pycache__/*.pyc')
names = [marshal.loads(open(p, 'rb').read()[16:]).co_filename for p in pycs]
sys.exit(not names or any(not n.startswith(venv + '/lib/') for n in names))
EOF
    check "$name: no direct_url.json, RECORD up to date" python3 - "$tree$venv" <<'EOF'
import base64, csv, glob, hashlib, os, sys
errors = 0
for dist_info in glob.glob(sys.argv[1] + '/lib/*/site-packages/*.dist-info'):
    if not os.path.basename(dist_info).startswith(('demo-', 'helper-')):
        continue
    errors += os.path.exists(dist_info + '/direct_url.json')
    with open(dist_info + '/RECORD', newline='') as fh:
        for row in csv.reader(fh):
            path = os.path.join(os.path.dirname(dist_info), row[0])
            if not os.path.lexists(path):
                errors += 1
            elif row[1]:
                algorithm, digest = row[1].split('=', 1)
                data = open(path, 'rb').read()
                errors += digest != base64.urlsafe_b64encode(
                    hashlib.new(algorithm, data).digest()).rstrip(b'=').decode()
                errors += row[2] != str(len(data))
sys.exit(errors)
EOF
    check "$name: depends on $depends" deps_contain "$deb" "$depends"
    check "$name: prerm cleans $venv" \
        sh -c "dpkg-deb -I '$deb' prerm | grep -qx \"dh_venv_install_dir='$venv'\""

    dpkg -i "$deb" > /dev/null || { fail "$name: install"; return; }
    check "$name: console script runs" \
        sh -c "'$venv/bin/demo' | grep -qx 'demo ok helper ok'"
    check "$name: activate sets VIRTUAL_ENV" \
        bash -c ". '$venv/bin/activate' && [ \"\$VIRTUAL_ENV\" = '$venv' ]"
    check "$name: pip freeze without file URLs" \
        sh -c "'$venv/bin/python' -m pip freeze 2>/dev/null | grep -E '^(demo|helper)' | sort | tr '\n' ' ' | grep -qx 'demo==0.1 helper==1.0 '"
    check "$name: pip uninstall with the RECORD" \
        sh -c "'$venv/bin/python' -m pip uninstall -y -q helper && ! '$venv/bin/python' -c 'import helper' 2>/dev/null"
    # bytecode written at runtime, which dpkg does not know about
    "$venv/bin/python" -O -c 'import demo'
    dpkg --purge demo > /dev/null
    check "$name: purge removes $venv" [ ! -e "$venv" ]
}

seq=$'%:\n\tdh $@ --with python-virtualenv\n\noverride_dh_virtualenv:\n\tdh_virtualenv --use-system-packages'
make_source "$WORK/sequence/demo" "$seq"
check_package sequence /opt/venvs/demo
make_source "$WORK/builtin-venv/demo" "$seq --builtin-venv"
check_package builtin-venv /opt/venvs/demo
make_source "$WORK/buildsystem/demo" $'export DH_VIRTUALENV_ARGUMENTS = --system-site-packages\n\n%:\n\tdh $@ --buildsystem=dh_virtualenv'
check_package buildsystem /usr/share/python/demo

# virtualenv links bin/python to /usr/bin/python3 even with --python python3.X
make_source "$WORK/versioned-python/demo" "$seq --builtin-venv --python python3.$MINOR"
check_package versioned-python /opt/venvs/demo "python3.$MINOR"
make_source "$WORK/copied-python/demo" "$seq --extra-virtualenv-arg=--copies"
check_package copied-python /opt/venvs/demo "python3.$MINOR"

# The other supported versions: virtualenv links their versioned
# interpreter (venv would need the pythonX.Y-venv package)
for version in $(py3versions -s); do
    [ "$version" = "python3.$MINOR" ] && continue
    make_source "$WORK/$version/demo" "$seq --python $version"
    check_package "$version" /opt/venvs/demo "$version"
done

# helper from a wheelhouse, locked in pylock.toml instead of requirements.txt
make_wheelhouse() {
    local dir=$1
    (cd "$dir" && rm requirements.txt \
        && python3 -m pip wheel -q --no-deps --no-build-isolation -w debian/wheels ./helper \
        && rm -r helper \
        && python3 -m pip lock -q --no-index --find-links debian/wheels 'helper==1.0' -o pylock.toml) \
        > "$dir.prepare.log" 2>&1 \
        || { fail "$dir: prepare the wheelhouse"; cat "$dir.prepare.log"; return 1; }
}
make_source "$WORK/wheelhouse/demo" "$seq --wheelhouse debian/wheels"
make_wheelhouse "$WORK/wheelhouse/demo" && check_package wheelhouse /opt/venvs/demo
make_source "$WORK/wheelhouse-buildsystem/demo" $'export DH_VIRTUALENV_ARGUMENTS = --system-site-packages\nexport DH_VIRTUALENV_WHEELHOUSE = debian/wheels\n\n%:\n\tdh $@ --buildsystem=dh_virtualenv'
make_wheelhouse "$WORK/wheelhouse-buildsystem/demo" \
    && check_package wheelhouse-buildsystem /usr/share/python/demo

echo "== reproducibility"
make_source "$WORK/r1/demo" "$seq"
make_source "$WORK/reproducible-longer-path/demo" "$seq"
if build "$WORK/r1/demo" && build "$WORK/reproducible-longer-path/demo"; then
    check "same package from two build directories" \
        cmp "$WORK"/r1/demo_0.1_*.deb "$WORK"/reproducible-longer-path/demo_0.1_*.deb
else
    fail "reproducibility builds"
fi

echo "== DEB_BUILD_OPTIONS"
make_source "$WORK/options/demo" "$seq --setuptools-test --verbose"
for options in nocheck "parallel=2 nocheck" nocheckx foo-nocheck; do
    build "$WORK/options/demo" "$options"
    case $options in
        *" nocheck"|nocheck) check "'$options' skips the tests" grep -q 'Skipped tests' "$WORK/options/demo.log" ;;
        *) check "'$options' runs the tests" grep -q 'Running tests' "$WORK/options/demo.log" ;;
    esac
done

echo "== test command"
# probe.py runs from the source directory with the virtualenv in PATH
make_probe() {
    make_source "$1" "$2"
    printf 'import sys, helper\nopen("tested", "w").write(sys.prefix)\n' > "$1/probe.py"
}
tested_in() { build "$1" && grep -q "$2\$" "$1/tested"; }
fails_with() { ! build "$1" && grep -q "$2" "$1.log"; }

dir=$WORK/test-command/demo
make_probe "$dir" "$seq --test-command='python probe.py'"
check "dh_virtualenv runs the test command" tested_in "$dir" "$dir/debian/demo/opt/venvs/demo"
rm -f "$dir/tested"
build "$dir" nocheck
check "nocheck skips the test command" [ ! -e "$dir/tested" ]
make_source "$WORK/test-command-fails/demo" "$seq --test-command=false"
check "a failing test command fails the build" fails_with "$WORK/test-command-fails/demo" 'tests failed'
dir=$WORK/test-command-buildsystem/demo
make_probe "$dir" $'export DH_VIRTUALENV_ARGUMENTS = --system-site-packages\nexport DH_VIRTUALENV_TEST_COMMAND = python probe.py\n\n%:\n\tdh $@ --buildsystem=dh_virtualenv'
check "the buildsystem runs DH_VIRTUALENV_TEST_COMMAND" tested_in "$dir" /usr/share/python/demo
dir=$WORK/test-command-pytest/demo
make_source "$dir" "$seq --test-command='python -m pytest -q tests'"
mkdir "$dir/tests"
printf 'import demo\n\n\ndef test_main(capsys):\n    demo.main()\n    assert capsys.readouterr().out == "demo ok helper ok\\n"\n' \
    > "$dir/tests/test_demo.py"
pytest_passes() { build "$1" && grep -q '1 passed' "$1.log"; }
check "pytest runs from the test command" pytest_passes "$dir"

exit $FAILED
