Skip to content

Commit 8f3cb93

Browse files
refactor: Add pipx
1 parent 2567058 commit 8f3cb93

1 file changed

Lines changed: 72 additions & 2 deletions

File tree

.devcontainer/library-scripts/setup-python-tools.sh

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22

33
set -e
44

5-
PYTHON=${1:-"/usr/local/python/bin/python3"}
6-
USERNAME=${2-"automatic"}
5+
PYTHON=${1:-"/usr/local/python/bin/python3"} # "/usr/local/bin/python" or "/usr/local/python/bin/python3"
6+
PYTHON_INSTALL_PATH=${2:-"/usr/local/python"} # "/usr/local/" or "/usr/local/python"
7+
USERNAME=${3-"automatic"}
8+
INSTALL_PIPX_TOOLS=${4:-"true"}
9+
export PIPX_HOME=${5:-"/usr/local/pipx"} # "/usr/local/py-utils" "/usr/local/share/pipx"
10+
UPDATE_RC=${6:-"true"}
11+
12+
DEFAULT_UTILS=(
13+
"autopep8"
14+
"bandit"
15+
# "black"
16+
"flake8"
17+
# "isort"
18+
"mypy"
19+
"pycodestyle"
20+
"pydocstyle"
21+
"pylint"
22+
"pytest"
23+
"twine"
24+
"yapf"
25+
)
726

827
if [ "${USERNAME}" = "automatic" ]; then
928
USERNAME=""
@@ -39,6 +58,16 @@ function sudoUserIf() {
3958
fi
4059
}
4160

61+
updaterc() {
62+
if [ "${UPDATE_RC}" = "true" ]; then
63+
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
64+
echo -e "$1" >>/etc/bash.bashrc
65+
fi
66+
fi
67+
}
68+
69+
export DEBIAN_FRONTEND=noninteractive
70+
4271
if ! ${PYTHON} --version >/dev/null; then
4372
echo "You need to install Python before installing packages"
4473
exit 1
@@ -48,3 +77,44 @@ installPythonPackage "pip" "latest"
4877
installPythonPackage "setuptools" "latest"
4978
installPythonPackage "art" "latest"
5079
installPythonPackage "nox" "latest"
80+
81+
if [ "${INSTALL_PIPX_TOOLS}" != "true" ]; then
82+
exit 0
83+
fi
84+
85+
export PIPX_BIN_DIR="${PIPX_HOME}/bin"
86+
export PATH="${PYTHON_INSTALL_PATH}/bin:${PIPX_BIN_DIR}:${PATH}"
87+
88+
if ! grep -e '^pipx:' </etc/group >/dev/null 2>&1; then
89+
groupadd --system pipx
90+
fi
91+
usermod --append --groups pipx ${USERNAME}
92+
umask 0002
93+
mkdir --parents ${PIPX_BIN_DIR}
94+
chown :pipx ${PIPX_HOME} ${PIPX_BIN_DIR}
95+
chmod g+s ${PIPX_HOME} ${PIPX_BIN_DIR}
96+
97+
export PYTHONUSERBASE=/tmp/pip-tmp
98+
export PIP_CACHE_DIR=/tmp/pip-tmp/cache
99+
pipx_path=""
100+
if ! type pipx >/dev/null 2>&1; then
101+
pip3 install --disable-pip-version-check --no-cache-dir --user pipx 2>&1
102+
/tmp/pip-tmp/bin/pipx install --pip-args=--no-cache-dir pipx
103+
pipx_path=$PYTHONUSERBASE"/bin/"
104+
fi
105+
for util in "${DEFAULT_UTILS[@]}"; do
106+
if ! type ${util} >/dev/null 2>&1; then
107+
${pipx_path}pipx install --system-site-packages --pip-args '--no-cache-dir --force-reinstall' ${util}
108+
else
109+
echo "${util} already installed. Skipping."
110+
fi
111+
done
112+
rm -rf /tmp/pip-tmp
113+
114+
updaterc "$(
115+
cat <<EOF
116+
export PIPX_HOME="${PIPX_HOME}"
117+
export PIPX_BIN_DIR="${PIPX_BIN_DIR}"
118+
if [[ "\${PATH}" != *"\${PIPX_BIN_DIR}"* ]]; then export PATH="\${PATH}:\${PIPX_BIN_DIR}"; fi
119+
EOF
120+
)"

0 commit comments

Comments
 (0)