Skip to content

Commit

Permalink
redo python venv machinery. Allow poetry
Browse files Browse the repository at this point in the history
Refs #211
  • Loading branch information
mxcl committed Jan 4, 2024
1 parent adc5f42 commit 3bd9b17
Show file tree
Hide file tree
Showing 28 changed files with 160 additions and 89 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
integration-tests:
name: ${{matrix.pkg}} (${{matrix.platform.tag}})
strategy:
fail-fast: false
matrix:
platform:
- os: ubuntu-latest
Expand All @@ -20,8 +21,8 @@ jobs:
pkg:
- stark.com/foo
- toolchain.com
- pyapp.com/1
- pyapp.com/2
- pyapp.com/pip
- pyapp.com/poetry
- stark.com/foo@1.2.3
- git-clone.com
- pc-cmake.com
Expand Down Expand Up @@ -56,6 +57,7 @@ jobs:
- run: test $(pkgx +stark.com/foo -- stark) = not_much_u
- run: bin/bk audit


unit-tests:
runs-on: ubuntu-latest
env:
Expand Down
5 changes: 5 additions & 0 deletions bin/cmd/build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { options, args } = await new Command()
.name("build")
.description("Build pkgx pantry pkgs with brewkit")
.option("-C, --clean", "Clean everything first")
.option("-s, --stage", "Stage, do not run build")
.arguments("[pkgspec]")
.parse();

Expand Down Expand Up @@ -93,6 +94,10 @@ const script = new Path(`${config.path.build}.sh`)
console.log('writing', script)
script.write({text: script_content, force: true}).chmod(0o755)

if (options.stage) {
Deno.exit(0)
}

/// run script
await gum('build')

Expand Down
15 changes: 13 additions & 2 deletions lib/porcelain/build-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function(config: Config, PATH?: Path): Promise<string> {

const brewkitd = new Path(new URL(import.meta.url).pathname).parent().parent().parent()
const brewkit_PATHs = [
brewkitd.join("share/brewkit"),
brewkitd.join("libexec"),
PATH
].compact(x => x?.string).join(':')

Expand All @@ -26,6 +26,16 @@ export default async function(config: Config, PATH?: Path): Promise<string> {
FLAGS.push("export MACOSX_DEPLOYMENT_TARGET=11.0")
}

const tmp = (() => {
switch (host().platform) {
case 'darwin':
case 'linux':
return `export TMPDIR="$HOME/tmp"; mkdir -p "$TMPDIR"`
case 'windows':
return `export TMP="$HOME/tmp"; export TEMP="$HOME/tmp"; mkdir -p "$TMP"`
}
})

return undent`
#!/${bash}
Expand All @@ -39,8 +49,9 @@ export default async function(config: Config, PATH?: Path): Promise<string> {
set +a
export PKGX="${pkgx}"
export SRCROOT=${config.path.build.string}
export HOME=${config.path.home.string}
export SRCROOT=${config.path.build.string}
${tmp()}
if [ -n "$CI" ]; then
export FORCE_UNSAFE_CONFIGURE=1
fi
Expand Down
63 changes: 63 additions & 0 deletions libexec/bkpyvenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env -S pkgx +git +install bash

set -eo pipefail

cd "$SRCROOT"

d="$(cd "$(dirname "$0")"/.. && pwd)"

CMD=$1
shift

if [ $1 = '--engine=poetry' ]; then
ENGINE=poetry
shift
fi

PREFIX=$1
shift

set -x

case $CMD in
stage)
VERSION=$1

if [ ! -d "$SRCROOT/.git" ]; then
GIT_DIR="$SRCROOT/.git"
git init
git config user.name 'pkgx[bot]'
git config user.email 'bot@pkgx.dev'
git commit -mnil --allow-empty
git tag -a "$VERSION" -m "Version $VERSION" --force
unset GIT_DIR
fi

if [ "$ENGINE" = poetry ]; then
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
else
python -m venv "$PREFIX"/venv
fi
;;
seal)
PYTHON=$(python --version | sed -n 's/Python \([0-9]\+\.[0-9]\+\).*/\1/p')

if [ "$ENGINE" = poetry ]; then
# FIXME is there a more efficient way to do this?
# FIXME the glob is unfortunate
poetry build -f sdist
tar xzf \
dist/*.tar.gz \
--directory "$SRCROOT"/.venv/lib/python$PYTHON/site-packages \
--strip-components=1
mkdir -p "$PREFIX"
mv "$SRCROOT"/.venv "$PREFIX/venv"
fi

for cmd in $@; do
install -D "$d/share/brewkit/python-venv-stub.py" $PREFIX/bin/$cmd
sed -i "1s|.*|#!/usr/bin/env -S pkgx python@$PYTHON|" $PREFIX/bin/$cmd
done
;;
esac
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions projects/git-clone.com/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build:
script:
- touch foo
- git add foo
# test run can be an array
- run:
- mkdir -p {{prefix}}
- mv foo {{prefix}}
Expand Down
27 changes: 0 additions & 27 deletions projects/pyapp.com/1/package.yml

This file was deleted.

18 changes: 0 additions & 18 deletions projects/pyapp.com/2/package.yml

This file was deleted.

1 change: 0 additions & 1 deletion projects/pyapp.com/2/requirements.txt

This file was deleted.

15 changes: 0 additions & 15 deletions projects/pyapp.com/2/setup.py

This file was deleted.

File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions projects/pyapp.com/pip/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
distributable:
# test can fetch a git repo
url: git+https://github.com/pkgxdev/brewkit
ref: v{{version}}

versions:
- 1.0.0

dependencies:
pkgx.sh: ^1

build:
dependencies:
python.org: ^3.11
script:
- mv props/* .

- bkpyvenv stage {{prefix}} {{version}}
- ${{prefix}}/venv/bin/pip install {{srcroot}}
- bkpyvenv seal {{prefix}} myapp

provides:
- bin/myapp

test:
- myapp
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions projects/pyapp.com/poetry/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
versions:
- 1.0.0

dependencies:
pkgx.sh: ^1

build:
dependencies:
python.org: ~3.11
python-poetry.org: ^1.7
script:
- mv props/* .

- bkpyvenv stage --engine=poetry {{prefix}} {{version}}
- poetry install
- bkpyvenv seal --engine=poetry {{prefix}} myapp

provides:
- bin/myapp

test:
- myapp

# test fixtures can have extnames
- run: |
test $(basename $FIXTURE .rb).rb = $(basename $FIXTURE)
test $(cat $FIXTURE) = foo
fixture:
content: foo
extname: .rb
16 changes: 16 additions & 0 deletions projects/pyapp.com/poetry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "myapp"
version = "1.0.0"
authors = ['foo']
description = 'bar'

[tool.poetry.scripts]
myapp = 'myapp.main:main'

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = ">=3.8.1,<3.12" # updated for llama-index dependency
requests = '*'
26 changes: 2 additions & 24 deletions share/brewkit/python-venv-stubber.sh → share/brewkit/python-venv-stub.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
#!/usr/bin/env -S pkgx bash
# shellcheck shell=bash

# python virtual-envs are not relocatable
# our only working choice is to rewrite these files and symlinks every time
# because we promise that pkgx is relocatable *at any time*
#FIXME requiring sed is a bit lame

if test -z "$VIRTUAL_ENV"; then
echo "error: VIRTUAL_ENV not set" >&2
exit 1
fi

mkdir -p "$VIRTUAL_ENV/../bin"

CMD_NAME="$1"

cat <<EOF > "$VIRTUAL_ENV"/../bin/"$CMD_NAME"
#!/usr/bin/env python

import os
import sys
import glob
import shutil
from pathlib import Path

# Determine directories and paths
script_dir = os.path.dirname(os.path.realpath(__file__))
virtual_env = os.path.normpath(os.path.join(script_dir, '..', 'venv'))
arg0 = os.path.basename(sys.argv[0])
python_path = shutil.which('python')
python_path = sys.executable
python_home = os.path.dirname(python_path)

# Write pyvenv.cfg file
Expand Down Expand Up @@ -64,9 +44,7 @@

# Execute the corresponding script in the virtual environment
arg0 = os.path.join(virtual_env, 'bin', arg0)

args = sys.argv[1:]
args.insert(0, arg0)
os.execv(arg0, args)
EOF

chmod +x "$VIRTUAL_ENV/../bin/$CMD_NAME"

0 comments on commit 3bd9b17

Please sign in to comment.