Skip to content

Commit

Permalink
Merge pull request #31 from cultureamp/add-ca-python-standard
Browse files Browse the repository at this point in the history
added standard python plugin pulled from ca-llm-gateway
  • Loading branch information
admackin authored Jun 28, 2024
2 parents f47c3c5 + 446d7a6 commit 7ed8281
Show file tree
Hide file tree
Showing 4 changed files with 108 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.
12 changes: 12 additions & 0 deletions plugins/ca-python-standard/bin/auto-venv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/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"
echo "Unsetting PYTHONPATH to avoid clashing with Nix poetry"
unset PYTHONPATH
else
echo "You have chose to manage virtualenv activation manually"
fi
42 changes: 42 additions & 0 deletions plugins/ca-python-standard/bin/ca-python-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/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"
auth_method="${POETRY_CA_AUTH_METHOD:-sso}"
if [[ "${auth_method}" == "sso" || "${auth_method}" == "vault" ]] ; then
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
fi

if [[ "${auth_method}" == "environment" ]] ; then
if [[ -z "$AWS_ACCESS_KEY_ID" ]] ; then
echo "AWS environment credentials not found"
fi
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
## running twice seems to get around auth issues on headless systems
poetry -v install || poetry -v install || exit 1
echo "Devbox setup has completed poetry install"
pre-commit install --install-hooks || echo "WARNING: could not install pre-commit hooks" >&2
22 changes: 22 additions & 0 deletions plugins/ca-python-standard/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "ca-python-standard",
"version": "0.1.0",
"description": "Installs Poetry and CA poetry helpers, activates virtualenv, and installs pre-commit hooks",
"packages": [
"awscli2"
],
"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 7ed8281

Please sign in to comment.