venv/re.sh

182 lines
2.9 KiB
Bash
Raw Normal View History

2024-09-26 19:27:56 +00:00
#! /usr/bin/env sh
2024-09-29 10:53:34 +00:00
root="/prj/venv"
2024-09-26 19:27:56 +00:00
url="http://localhost:8000"
2024-09-30 06:41:08 +00:00
url_pypi="${url}/pypi/simple"
2024-09-26 19:27:56 +00:00
activate() {
2024-09-28 20:49:09 +00:00
echo
export VIRTUAL_ENV="${1}"
echo "${VIRTUAL_ENV}"
export OLD_PATH="${PATH}"
export PATH="${VIRTUAL_ENV}/bin:${PATH}"
echo "${PATH}"
python3 --version
pip --version
2024-09-26 19:27:56 +00:00
}
2024-09-29 11:54:57 +00:00
caches() {
2024-09-29 12:10:06 +00:00
local command="${1}"
2024-09-29 11:54:57 +00:00
set \
".cache/pip" \
".cache/uv" \
".local/share/pip" \
".local/share/uv"
2024-09-29 12:10:06 +00:00
echo
echo "caches:"
2024-09-29 11:54:57 +00:00
for cache in "${@}"; do
2024-09-29 12:10:06 +00:00
"${command}" "${HOME}/${cache}"
2024-09-29 11:54:57 +00:00
done
}
2024-09-26 19:27:56 +00:00
deactivate() {
2024-09-28 20:49:09 +00:00
echo
echo "${VIRTUAL_ENV}"
export PATH="${OLD_PATH}"
echo "${PATH}"
unset OLD_PATH VIRTUAL_ENV
2024-09-26 19:27:56 +00:00
}
list() {
2024-09-28 20:49:09 +00:00
echo
2024-09-29 12:10:06 +00:00
ls -a -l "${1}/${2}"
}
list_venv() {
2024-09-30 08:09:04 +00:00
list "${1}" "bin/python"
2024-09-30 08:16:02 +00:00
list "${1}" "lib/$(basename "${1}")/site-packages/rwx"
2024-09-26 19:27:56 +00:00
}
pip_install() {
2024-09-28 20:49:09 +00:00
echo
2024-09-29 10:53:34 +00:00
pip install \
--index-url "${url_pypi}" \
--no-cache-dir \
"${@}"
2024-09-26 19:27:56 +00:00
}
remove() {
2024-09-28 20:49:09 +00:00
rm --force --recursive "${@}"
2024-09-26 19:27:56 +00:00
}
uv_install() {
2024-09-29 10:19:08 +00:00
set \
"pelican" \
\
"hatch" \
\
"Sphinx" \
"sphinx-rtd-theme" \
\
"gitlint" \
\
"pydoclint" \
"pylint" \
"ruff" \
\
"pytest" \
\
"toml" \
\
"twine" \
\
"mypy" \
"pyright" \
\
"ruamel.yaml" \
"PyYAML" \
"types-PyYAML"
2024-09-28 20:49:09 +00:00
echo
uv pip install --index-url "${url_pypi}" "${@}"
2024-09-26 19:27:56 +00:00
}
uv_python() {
2024-09-28 20:49:09 +00:00
echo
export UV_PYTHON_INSTALL_MIRROR="${url}/cpypy"
2024-09-29 20:06:17 +00:00
export UV_PYPY_INSTALL_MIRROR="${UV_PYTHON_INSTALL_MIRROR}/pypy"
2024-09-29 19:41:09 +00:00
uv python install "${1}"
unset \
UV_PYPY_INSTALL_MIRROR \
UV_PYTHON_INSTALL_MIRROR
2024-09-26 19:27:56 +00:00
}
uv_venv() {
2024-09-28 20:49:09 +00:00
echo
2024-09-29 00:16:09 +00:00
uv venv \
2024-09-29 10:54:58 +00:00
--allow-existing \
2024-09-29 00:16:09 +00:00
--index-url "${url_pypi}" \
2024-09-29 19:34:47 +00:00
--no-python-downloads \
2024-09-29 11:19:30 +00:00
--python "${2}" \
2024-09-29 00:16:09 +00:00
--relocatable \
--seed \
"${1}"
2024-09-26 19:27:56 +00:00
}
2024-09-29 15:07:32 +00:00
venvs() {
local action="${1}"
local venv version
set \
2024-09-30 08:13:07 +00:00
"python3.12" \
"python3.11" \
"python3.10" \
2024-09-29 19:34:47 +00:00
"pypy3.10"
2024-09-29 15:07:32 +00:00
for version in "${@}"; do
venv="${root}/${version}"
2024-09-29 15:31:06 +00:00
echo
echo "${venv}${action}"
2024-09-29 15:07:32 +00:00
case "${action}" in
2024-09-29 15:16:17 +00:00
"create")
export UV_PYTHON_INSTALL_DIR="${venv}/cpypy"
echo "→ install python ${version}"
2024-09-29 19:41:09 +00:00
uv_python "${version}"
2024-09-29 15:16:17 +00:00
echo "→ create venv ${version}"
uv_venv "${venv}" "${version}"
unset UV_PYTHON_INSTALL_DIR
;;
2024-09-29 15:23:06 +00:00
"init")
2024-09-29 15:19:15 +00:00
activate "${venv}"
pip_install "uv"
2024-09-29 15:23:06 +00:00
deactivate
;;
"install")
activate "${venv}"
uv_install
2024-09-29 15:19:15 +00:00
deactivate
;;
2024-09-29 15:26:02 +00:00
"link")
activate "${venv}"
for module in "rwx"; do
ln --symbolic \
"/rwx/rwx/${module}" \
2024-09-30 08:13:07 +00:00
"${venv}/lib/${version}/site-packages/${module}"
2024-09-29 15:26:02 +00:00
done
deactivate
;;
2024-09-29 15:28:16 +00:00
"list")
activate "${venv}"
list_venv "${venv}"
deactivate
;;
2024-09-29 18:49:09 +00:00
"relate")
absolute="$(readlink -f "${venv}/bin/python")"
prevenv="$(readlink -f "${venv}")"
relative=$(echo "${absolute}" | sed "s|${prevenv}|..|")
2024-09-29 18:53:02 +00:00
ln --force --symbolic "${relative}" "${venv}/bin/python"
2024-09-29 18:49:09 +00:00
;;
2024-09-29 15:07:32 +00:00
*) ;;
esac
done
}
2024-09-26 19:27:56 +00:00
2024-09-29 11:56:10 +00:00
caches remove
2024-09-29 15:10:59 +00:00
remove "${root}"
2024-09-29 10:53:34 +00:00
os="${root}/os"
2024-09-28 21:47:31 +00:00
python3 -m "venv" "${os}"
activate "${os}"
2024-09-29 12:10:06 +00:00
list_venv "${os}"
2024-09-26 19:27:56 +00:00
pip_install --upgrade "pip"
pip_install "uv"
2024-09-29 12:10:06 +00:00
list_venv "${os}"
2024-09-29 15:16:17 +00:00
venvs create
2024-09-29 10:53:34 +00:00
deactivate
2024-09-28 21:40:14 +00:00
2024-09-29 15:23:06 +00:00
venvs init
2024-09-29 18:49:09 +00:00
venvs relate
2024-09-29 15:28:16 +00:00
venvs link
venvs install
venvs list
2024-09-29 11:56:10 +00:00
2024-09-30 08:19:24 +00:00
caches list