#! /usr/bin/env sh root="/prj/venv" url="http://localhost:8000" url_pypi="${url}/pypi/simple" activate() { echo export VIRTUAL_ENV="${1}" echo "→ ${VIRTUAL_ENV}" export OLD_PATH="${PATH}" export PATH="${VIRTUAL_ENV}/bin:${PATH}" echo "→ ${PATH}" python3 --version pip --version } caches() { local command="${1}" set \ ".cache/pip" \ ".cache/uv" \ ".local/share/pip" \ ".local/share/uv" echo echo "caches:" for cache in "${@}"; do "${command}" "${HOME}/${cache}" done } deactivate() { echo echo "← ${VIRTUAL_ENV}" export PATH="${OLD_PATH}" echo "← ${PATH}" unset OLD_PATH VIRTUAL_ENV } list() { echo ls -a -l "${1}/${2}" } list_venv() { list "${1}" "bin/python" } pip_install() { echo pip install \ --index-url "${url_pypi}" \ --no-cache-dir \ "${@}" } remove() { rm --force --recursive "${@}" } uv_install() { set \ "pelican" \ \ "hatch" \ \ "Sphinx" \ "sphinx-rtd-theme" \ \ "gitlint" \ \ "pydoclint" \ "pylint" \ "ruff" \ \ "pytest" \ \ "toml" \ \ "twine" \ \ "mypy" \ "pyright" \ \ "ruamel.yaml" \ "PyYAML" \ "types-PyYAML" echo uv pip install --index-url "${url_pypi}" "${@}" } uv_python() { echo export UV_PYTHON_INSTALL_MIRROR="${url}/cpypy" export UV_PYPY_INSTALL_MIRROR="${UV_PYTHON_INSTALL_MIRROR}/pypy" uv python install "${1}" unset \ UV_PYPY_INSTALL_MIRROR \ UV_PYTHON_INSTALL_MIRROR } uv_venv() { echo uv venv \ --allow-existing \ --index-url "${url_pypi}" \ --no-python-downloads \ --python "${2}" \ --relocatable \ --seed \ "${1}" } venvs() { local action="${1}" local venv version set \ "python3.12" \ "python3.11" \ "python3.10" \ "pypy3.10" for version in "${@}"; do venv="${root}/${version}" echo echo "${venv} ← ${action}" case "${action}" in "create") export UV_PYTHON_INSTALL_DIR="${venv}/cpypy" echo "→ install python ${version}" uv_python "${version}" echo "→ create venv ${version}" uv_venv "${venv}" "${version}" unset UV_PYTHON_INSTALL_DIR ;; "init") activate "${venv}" pip_install "uv" deactivate ;; "install") activate "${venv}" uv_install deactivate ;; "link") activate "${venv}" for module in "rwx"; do ln --symbolic \ "/rwx/rwx/${module}" \ "${venv}/lib/${version}/site-packages/${module}" done deactivate ;; "list") activate "${venv}" list_venv "${venv}" deactivate ;; "relate") absolute="$(readlink -f "${venv}/bin/python")" prevenv="$(readlink -f "${venv}")" relative=$(echo "${absolute}" | sed "s|${prevenv}|..|") ln --force --symbolic "${relative}" "${venv}/bin/python" ;; *) ;; esac done } caches remove remove "${root}" os="${root}/os" python3 -m "venv" "${os}" activate "${os}" list_venv "${os}" pip_install --upgrade "pip" pip_install "uv" list_venv "${os}" venvs create deactivate venvs init venvs relate venvs link venvs install venvs list caches list_venv