From 42cc9f037160c64a61fbff2d7f9f7929fa0243e6 Mon Sep 17 00:00:00 2001 From: "andy.mackinlay" Date: Fri, 21 Jun 2024 11:58:12 +1000 Subject: [PATCH] added standard python plugins pulled from ca-llm-gateway --- plugins/ca-python-standard/README.md | 32 +++++++++++++++++ plugins/ca-python-standard/bin/auto-venv | 10 ++++++ .../ca-python-standard/bin/ca-python-install | 34 +++++++++++++++++++ plugins/ca-python-standard/plugin.json | 20 +++++++++++ 4 files changed, 96 insertions(+) create mode 100644 plugins/ca-python-standard/README.md create mode 100755 plugins/ca-python-standard/bin/auto-venv create mode 100755 plugins/ca-python-standard/bin/ca-python-install create mode 100644 plugins/ca-python-standard/plugin.json diff --git a/plugins/ca-python-standard/README.md b/plugins/ca-python-standard/README.md new file mode 100644 index 0000000..2749db5 --- /dev/null +++ b/plugins/ca-python-standard/README.md @@ -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. \ No newline at end of file diff --git a/plugins/ca-python-standard/bin/auto-venv b/plugins/ca-python-standard/bin/auto-venv new file mode 100755 index 0000000..5b7802a --- /dev/null +++ b/plugins/ca-python-standard/bin/auto-venv @@ -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 \ No newline at end of file diff --git a/plugins/ca-python-standard/bin/ca-python-install b/plugins/ca-python-standard/bin/ca-python-install new file mode 100755 index 0000000..0d73e8d --- /dev/null +++ b/plugins/ca-python-standard/bin/ca-python-install @@ -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 diff --git a/plugins/ca-python-standard/plugin.json b/plugins/ca-python-standard/plugin.json new file mode 100644 index 0000000..172c45f --- /dev/null +++ b/plugins/ca-python-standard/plugin.json @@ -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" + } +}