Skip to content

Commit

Permalink
New python dev container (#2)
Browse files Browse the repository at this point in the history
New version for app-python & azure-function-python
  • Loading branch information
bastgau authored Oct 26, 2023
1 parent b3a97a9 commit a55a4c6
Show file tree
Hide file tree
Showing 95 changed files with 2,953 additions and 452 deletions.
26 changes: 0 additions & 26 deletions .devcontainer/check-post-install.sh

This file was deleted.

36 changes: 0 additions & 36 deletions .devcontainer/devcontainer.json

This file was deleted.

11 changes: 1 addition & 10 deletions .devcontainer/install-deps.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#!/bin/bash

sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install vim

# Install the Microsoft ODBC driver for SQL Server (Linux)
# https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server

# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
# sudo apt-get update
# sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# Notice: Add pyodbc in requirements.txt or pyproject.toml file.
sudo DEBIAN_FRONTEND=noninteractive apt-get -yq install vim dos2unix
132 changes: 31 additions & 101 deletions .devcontainer/install.sh
Original file line number Diff line number Diff line change
@@ -1,121 +1,51 @@
#!/bin/bash

RED="\e[31m"
GREEN="\e[32m"
BLUE="\e[34m"
YELLOW="\e[33m"
ENDCOLOR="\e[0m"
. "$WORKSPACE_PATH/tools/color.sh"

echo -e "\n${BLUE}################################"
echo -e "${BLUE}#### INSTALL.SH ####${ENDCOLOR}"
echo -e "${BLUE}################################"
cd "$WORKSPACE_PATH/.devcontainer/install"

echo -e "\n${GREEN}> Configure virtual environment.${ENDCOLOR}\n"
CONTAINER_TYPE=$(jq -r '.customizations.vscode.settings."container.type"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
echo -e "\n${BLUE}You are about to initiate a project '${YELLOW}$CONTAINER_TYPE${BLUE}'.${ENDCOLOR}"

sudo chgrp vscode /workspaces/app/.venv
sudo chown vscode /workspaces/app/.venv

git config --global --add safe.directory /workspaces/app
git config --global core.eol lf
git config --global core.autocrlf false

python3 -m venv /workspaces/app/.venv
PATH="/workspaces/app/.venv/bin:$PATH"

echo -e "Done.\n"

echo -e "${GREEN}> Update PIP tool.${ENDCOLOR}\n"
pip install --upgrade pip

echo -e "\n${GREEN}> Identify the packaging and dependency manager to install.${ENDCOLOR}\n"

PIP_MANAGER=false
POETRY_MANAGER=false

NEW_POETRY_INSTALL=false

FILE=/workspaces/app/requirements.txt

if [ -f "$FILE" ];
then
echo -e "PIP configuration file was found (requirements.txt).\n"
PIP_MANAGER=true
fi

FILE=/workspaces/app/pyproject.toml

if [ -f "$FILE" ];
then
echo -e "POETRY configuration file was found (pyproject.toml).${ENDCOLOR}"
POETRY_MANAGER=true
fi

if [ "$POETRY_MANAGER" = true ] && [ "$PIP_MANAGER" = true ];
then
echo -e "${RED}> ERROR: You cannot define two packaging and dependency manager in the same time.${ENDCOLOR}\n"
if [ ]; then
echo -e "\n💥 ${RED}Installation was aborted. Specified container type '$CONTAINER_TYPE' is not supported.${ENDCOLOR}💥\n"
exit 1
fi

if [ "$POETRY_MANAGER" = false ] && [ "$PIP_MANAGER" = false ];
then

echo -e "${YELLOW}No packaging and dependency manager was found.${ENDCOLOR}"
echo -e "${YELLOW}Type 'PIP' or 'POETRY' if you want to install a packaging and dependency manager !${ENDCOLOR}"
echo -e "${YELLOW}Another option will install no packaging and dependency manager.${ENDCOLOR}"
echo -e "${YELLOW}Your selection :${ENDCOLOR}"
install_files=()

read MANAGER
echo -e "The following packaging and dependency manager will be installed : $MANAGER\n"

if [ "${MANAGER^^}" = "POETRY" ]
then
POETRY_MANAGER=true
NEW_POETRY_INSTALL=true
while IFS= read -r -d $'\0' current_file ; do
if ! grep -q "# ignored: $CONTAINER_TYPE" "$current_file"; then
install_files+=("$current_file")
fi
done < <(find ./ -type f -name "*.sh" -print0)

if [ "${MANAGER^^}" = "PIP" ]
then
PIP_MANAGER=true
touch /workspaces/app/requirements.txt
touch /workspaces/app/requirements-dev.txt
fi
install_files=($(printf "%s\n" "${install_files[@]}" | sort))

fi

source /workspaces/app/.venv/bin/activate
echo -e "${BLUE}We have found ${YELLOW}${#install_files[@]}${BLUE} installation files.${ENDCOLOR}"

if [ "$PIP_MANAGER" = true ];
then
for install_file in "${install_files[@]}"; do
if [ -f "$install_file" ]; then

echo -e "${GREEN}> Install dependencies with PIP.${ENDCOLOR}\n"
echo -e "\n\e[104m Execute: $install_file \e[49m"

# pip install keyring artifacts-keyring
if [ ! -x "$install_file" ]; then
chmod +x "$install_file"
fi

# cat <<EOF >> /workspaces/app/.venv/pip.conf
# [global]
# extra-index-url=https://pkgs.dev.azure.com/...
# EOF
./"$install_file"

pip install -r /workspaces/app/requirements-dev.txt
pip install -r /workspaces/app/requirements.txt
if [ "$?" -ge 1 ]; then
echo -e "\n💥 ${RED}Installation was aborted. Check the errors displayed above.${ENDCOLOR}💥\n"
exit 1
else
echo -e "${YELLOW}... Press any key to continue ..."
read -s -p " " -n 1 -r
echo -e "${ENDCOLOR}"
fi

fi

if [ "$POETRY_MANAGER" = true ];
then

echo -e "${GREEN}> Install POETRY tool and install dependencies.${ENDCOLOR}\n"
curl -sSL https://install.python-poetry.org | python3 -
poetry completions bash >> ~/.bash_completion

if [ "$POETRY_MANAGER" = true ];
then
poetry init
fi
done

poetry install

fi

chmod +x /workspaces/app/.devcontainer/check-post-install.sh
/workspaces/app/.devcontainer/check-post-install.sh
echo -e "🎉 ${YELLOW}Installation is finished!${ENDCOLOR}"
echo -e "🎉 ${YELLOW}You can close all terminal windows and reload the project!${ENDCOLOR}\n"
71 changes: 71 additions & 0 deletions .devcontainer/install/01-project-review.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

. "$WORKSPACE_PATH/tools/color.sh"

echo -e "\n${BLUE}#############################################################${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}##### Project review #####${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}#############################################################${ENDCOLOR}"

echo -e "\n${GREEN}> Project review.${ENDCOLOR}\n"

DEPENDENCY_MANAGER=$(jq -r '.customizations.vscode.settings."python.dependencyManager"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
PRE_COMMIT_ENABLED=$(jq -r '.customizations.vscode.settings."git.preCommitEnabled"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
PYTEST_ENABLED=$(jq -r '.customizations.vscode.settings."python.testing.pytestEnabled"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
UNITTEST_ENABLED=$(jq -r '.customizations.vscode.settings."python.testing.unittestEnabled"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
COVERAGE_ENABLED=$(jq -r '.customizations.vscode.settings."python.testing.coverageEnabled"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);
FORMATTER=$(jq -r '.customizations.vscode.settings."editor.defaultFormatter"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);

PIP_EXTRA_INDEX_URL=$(jq -r '.customizations.vscode.settings."python.pip.extraIndexUrl"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);

echo -e "The dependency manager used for the project is ${YELLOW}$DEPENDENCY_MANAGER${ENDCOLOR}."
echo -e "The formatter used for the project is ${YELLOW}$FORMATTER${ENDCOLOR}.\n"

if [ "$PIP_EXTRA_INDEX_URL" = "" ] || [ "$PIP_EXTRA_INDEX_URL" = "null" ]; then
echo -e "No additional package indexes is configured.\n"
else
echo -e "An additional package indexes is specified (value: ${YELLOW}$PIP_EXTRA_INDEX_URL${ENDCOLOR}).\n"
fi

if [ $PRE_COMMIT_ENABLED = "true" ]; then
echo -e "✔️ㅤPre-commit"
else
echo -e "✖️ㅤPre-commit"
fi

if [ $PYTEST_ENABLED = "true" ]; then
echo -e "✔️ㅤPytest"
else
echo -e "✖️ㅤPytest"
fi

if [ $UNITTEST_ENABLED = "true" ]; then
echo -e "✔️ㅤUnittest"
else
echo -e "✖️ㅤUnittest"
fi

if [ $COVERAGE_ENABLED = "true" ]; then
echo -e "✔️ㅤCode coverage"
else
echo -e "✖️ㅤCode coverage"
fi

if [ "$DEPENDENCY_MANAGER" != "pip" ] && [ "$DEPENDENCY_MANAGER" != "poetry" ]; then
echo -e "\n${RED}No correct packaging and dependency manager is configured.${ENDCOLOR}"
echo -e "${RED}Only 'pip' and 'poetry' managers are supported.${ENDCOLOR}"
STOP=true
fi

if [ "$FORMATTER" != "eeyore.yapf" ] && [ "$FORMATTER" != "ms-python.black-formatter" ]; then
echo -e "\n${RED}No correct formatter is configured.${ENDCOLOR}"
echo -e "${RED}Only 'eeyore.yapf' and 'ms-python.black-formatter' formaters are supported.${ENDCOLOR}"
STOP=true
fi

if [ "$STOP" == "true" ]; then
exit 1
fi

echo -e ""
25 changes: 25 additions & 0 deletions .devcontainer/install/02-fix-vscode-settings-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

. "$WORKSPACE_PATH/tools/color.sh"

echo -e "\n${BLUE}#############################################################${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}##### Fix VSCode Settings File #####${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}#############################################################${ENDCOLOR}"

echo -e "\n${GREEN}> Fix VSCode Settings File.${ENDCOLOR}\n"

jq 'del(.["python.formatting.autopep8Path",
"python.formatting.blackPath",
"python.linting.flake8Path",
"python.linting.flake8Enabled",
"python.linting.mypyPath",
"python.linting.mypyEnabled",
"python.linting.pylintPath",
"python.linting.pylintEnabled"
])' $HOME/.vscode-server/data/Machine/settings.json > /tmp/.vscode-server-settings.json

mv /tmp/.vscode-server-settings.json $HOME/.vscode-server/data/Machine/settings.json

echo -e "File '$HOME/.vscode-server/data/Machine/settings.json' has been modified to fix slow issue with linters.\n"
53 changes: 53 additions & 0 deletions .devcontainer/install/03-generate-file-vscode-extensions-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# can-be-removed-after-installation

. "$WORKSPACE_PATH/tools/color.sh"

echo -e "\n${BLUE}#############################################################${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}##### Generate .vscode/extensions.json file #####${ENDCOLOR}"
echo -e "${BLUE}##### #####${ENDCOLOR}"
echo -e "${BLUE}#############################################################${ENDCOLOR}"

mkdir -p $SOURCE_PATH
mkdir "$WORKSPACE_PATH/.vscode"

if [ -f "$WORKSPACE_PATH/.vscode/extensions.json" ]; then
echo -e "\n${YELLOW}Nothing to do because file is already existing.\n${ENDCOLOR}"
else

echo -e "\n${GREEN}> Generate file '.vscode/extensions.json'.${ENDCOLOR}\n"

# Create initial file.
merged_content=$(echo '{"recommendations": []}' | jq '.')

# Add extensions specifically for the container type.
CONTAINER_TYPE=$(jq -r '.customizations.vscode.settings."container.type"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);

if [ -f "$WORKSPACE_PATH/.devcontainer/templates/${CONTAINER_TYPE}/config/extensions.json" ]; then
content=$(cat $WORKSPACE_PATH/.devcontainer/templates/${CONTAINER_TYPE}/config/extensions.json)
merged_content=$(echo "$merged_content" | jq ".recommendations += $content")
fi

# Add extensions specifically for the default formatter.
defaultFormatter=$(jq -r '.customizations.vscode.settings."editor.defaultFormatter"' $WORKSPACE_PATH/.devcontainer/devcontainer.json);

if [ "$defaultFormatter" == "ms-python.autopep8" ] || [ "$defaultFormatter" == "ms-python.black-formatter" ] || [ "$defaultFormatter" == "eeyore.yapf" ]; then
content="[\"$defaultFormatter\"]"
merged_content=$(echo "$merged_content" | jq ".recommendations += $content")
fi

# Add other extensions.
if [ -f "$WORKSPACE_PATH/.devcontainer/templates/default/config/extensions.json" ]; then
content=$(cat $WORKSPACE_PATH/.devcontainer/templates/default/config/extensions.json)
merged_content=$(echo "$merged_content" | jq ".recommendations += $content")
fi

# Pretty-print the content and create the file.
formatted_content=$(echo $merged_content | jq -c '.')
echo $formatted_content | python -m json.tool --indent=2 > "$WORKSPACE_PATH/.vscode/extensions.json"

echo -e "Done.\n"

fi
Loading

0 comments on commit a55a4c6

Please sign in to comment.