Skip to content

Commit

Permalink
added standard python plugins pulled from ca-llm-gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
admackin committed Jun 21, 2024
1 parent a9cf731 commit 42cc9f0
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
32 changes: 32 additions & 0 deletions plugins/ca-python-standard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CA Python Devbox Plugin

* Installs `pipx` (via `pipx` [plugin](https://github.com/cultureamp/devbox-extras/tree/main/plugins/pipx))
* Installs [`poetry`](https://python-poetry.org) to manage package installation
* Decides whether you need private python packages
* This is determined by the presence of extra repos in `pyproject.toml`
* It can be overridden by setting the environment variable `POETRY_CA_PRIVATE_PYTHON` to the string `false`, probably in the `env` section of your `devbox.json`)
* If you need private python packages
* Installs `poetry-codeartifact-auth` as a poetry plugin
* Checks that you have an appropriate environment variable set to point to an AWS role which can access CodeArtifact
* Installs [`pre-commit`](https://pre-commit.com) hooks, if the `pre-commit` package is present in `pyproject.toml` (or `pre-commit` is installed externally).
* Automatically activates virtual env in `$VENV_DIR` unless `$DEVBOX_PYTHON_AUTO_VENV` environment variable is `false`
* Culture Amp convention is to set `VENV_DIR` to `"$PWD/.venv` in your `devbox.json` but this is only a loose convention and subject to change


## Usage

This plugin is designed to work for common cases with minimal extra configuration needed. See notes above about environment variables you may wish to set in some cases, eg `POETRY_CA_PRIVATE_PYTHON`.

The main thing to know is **the package installation step doesn't run by default** (you don't want to always run in an init hook as it's expensive). The package installation can be run using the auto-created `python-install` script using

devbox run python-install

The best place to run this is in the setup script in your app. If you don't need to do any other setup, this could be as simple as adding the following to your `devbox.json`:

"shell": {
"scripts": {
"setup": "devbox run python-install"
}
}

But if you have other more complicated needs which you have wisely put into a standalone `setup` script, you can instead add the line there.
10 changes: 10 additions & 0 deletions plugins/ca-python-standard/bin/auto-venv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -e

if [ "${DEVBOX_PYTHON_AUTO_VENV:-true}" == "true" ]; then
echo "Activating venv $VENV_DIR by adding to path (ignore below message)";
export PATH="$VENV_DIR/bin:$PATH"
else
echo "You have chose to manage virtualenv activation manually"
fi
34 changes: 34 additions & 0 deletions plugins/ca-python-standard/bin/ca-python-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -e

pipx install poetry

poetry_repos="$(poetry config repositories)"

if [[ "$poetry_repos" != "{}" && "${POETRY_CA_PRIVATE_PYTHON:-true}" == "true" ]] ; then
# if there are extra poetry repos and we haven't opted out of private package handling
# then assume we have private python packages and are using poetry-codeartifact-auth
echo "Setting up helpers for installing private python packages and checking config"
if [[ -z $POETRY_CA_DEFAULT_AWS_PROFILE ]] ; then
cat >&2 <<'EOF'
#############################################################################
NOTE: `POETRY_CA_DEFAULT_AWS_PROFILE` environment variable is unset
Set a value for this in your profile, or if not needed, then opt out of private
package handling by either removing extra private package repositories from
pyproject.toml or set `POETRY_CA_PRIVATE_PYTHON` to `"false"` in
`devbox.json`
#############################################################################
EOF
exit 1
fi
poetry self add git+https://github.com/cultureamp/poetry-codeartifact-auth.git#main -E plugin
else
echo "You do not appear to want handling of private Python packages so it will not be set up"
fi

. ${VENV_DIR}/bin/activate
poetry install
echo "Devbox setup has completed poetry install"

pre-commit install --install-hooks || echo "Could not install pre-commit hooks. Is pre-commit present in `pyproject.toml`?" >&2
20 changes: 20 additions & 0 deletions plugins/ca-python-standard/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "ca-python-standard",
"version": "0.1.0",
"description": "Installs Poetry and CA poetry helpers, activates virtualenv, and installs pre-commit hooks",
"packages": [],
"include": [
"github:cultureamp/devbox-extras?dir=plugins/pipx"
],
"env": {},
"create_files": {
"{{.Virtenv}}/bin/ca-python-install": "bin/ca-python-install",
"{{.Virtenv}}/bin/auto-venv": "bin/auto-venv"
},
"shell": {
"scripts": {
"python-install": "{{.Virtenv}}/bin/ca-python-install"
},
"init_hook": ". {{.Virtenv}}/bin/auto-venv"
}
}

0 comments on commit 42cc9f0

Please sign in to comment.