From 8346942a364b61121f49b9e62ece842486cc7a62 Mon Sep 17 00:00:00 2001 From: Bastien Gautier Date: Fri, 19 Apr 2024 14:51:38 +0200 Subject: [PATCH] Feature/improve azure function (#4) Installation scripts improved. Fix issue about the Azure Function Application. Complete missing documentation. --- ...03-generate-file-vscode-extensions-json.sh | 27 +-- .../27-install-requirements-app-python.sh | 3 +- ...tall-requirements-azure-function-python.sh | 159 ------------------ .../install/85-extension-installation.sh | 8 +- .../install/86-fix-mypy-extension.sh | 4 +- ...__65-generate-file-vscode-launch-json.sh__ | 23 --- .../templates/app-python/config/launch.json | 2 +- .../config/extensions.json | 5 +- .../azure-function-python/config/launch.json | 2 +- .../templates/default/config/launch.json | 2 +- ...01-duplicate-configuration-example-file.md | 18 +- 11 files changed, 44 insertions(+), 209 deletions(-) delete mode 100755 .devcontainer/install/27-install-requirements-azure-function-python.sh delete mode 100755 .devcontainer/install/__65-generate-file-vscode-launch-json.sh__ diff --git a/.devcontainer/install/03-generate-file-vscode-extensions-json.sh b/.devcontainer/install/03-generate-file-vscode-extensions-json.sh index 79af617..4adbcc4 100755 --- a/.devcontainer/install/03-generate-file-vscode-extensions-json.sh +++ b/.devcontainer/install/03-generate-file-vscode-extensions-json.sh @@ -13,12 +13,12 @@ echo -e "${BLUE}#############################################################${E mkdir -p $SOURCE_PATH mkdir -p "$WORKSPACE_PATH/.vscode" +echo -e "\n${GREEN}> Generate file '.vscode/extensions.json'.${ENDCOLOR}\n" + if [ -f "$WORKSPACE_PATH/.vscode/extensions.json" ]; then - echo -e "\n${YELLOW}Nothing to do because file is already existing.\n${ENDCOLOR}" + echo -e "${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 '.') @@ -30,14 +30,6 @@ else 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) @@ -51,3 +43,16 @@ else echo -e "Done.\n" fi + +# Add extensions specifically for the default formatter in 'devcontainer.json' file. +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 + exist=`cat $WORKSPACE_PATH/.devcontainer/devcontainer.json | jq ".customizations.vscode.extensions | contains([\"$defaultFormatter\"])"` + if [ $exist == 'false' ]; then + echo -e "${GREEN}> Update file '.devcontainer/devcontainer.json'.${ENDCOLOR}\n" + cat $WORKSPACE_PATH/.devcontainer/devcontainer.json | jq ".customizations.vscode.extensions += [\"$defaultFormatter\"]" > /tmp/devcontainer.json + mv /tmp/devcontainer.json $WORKSPACE_PATH/.devcontainer/devcontainer.json + echo -e "Done.\n" + fi +fi diff --git a/.devcontainer/install/27-install-requirements-app-python.sh b/.devcontainer/install/27-install-requirements-app-python.sh index d597d46..8095694 100755 --- a/.devcontainer/install/27-install-requirements-app-python.sh +++ b/.devcontainer/install/27-install-requirements-app-python.sh @@ -1,13 +1,12 @@ #!/bin/bash -# ignored: azure-function-python # ignored: poetry-dependency-manager . "$WORKSPACE_PATH/tools/color.sh" echo -e "\n${BLUE}#############################################################${ENDCOLOR}" echo -e "${BLUE}##### #####${ENDCOLOR}" -echo -e "${BLUE}##### Install requirements.txt (app-python version) #####${ENDCOLOR}" +echo -e "${BLUE}##### Install requirements.txt #####${ENDCOLOR}" echo -e "${BLUE}##### #####${ENDCOLOR}" echo -e "${BLUE}#############################################################${ENDCOLOR}" diff --git a/.devcontainer/install/27-install-requirements-azure-function-python.sh b/.devcontainer/install/27-install-requirements-azure-function-python.sh deleted file mode 100755 index 0716645..0000000 --- a/.devcontainer/install/27-install-requirements-azure-function-python.sh +++ /dev/null @@ -1,159 +0,0 @@ -#!/bin/bash - -# ignored: app-python -# ignored: poetry-dependency-manager - -. "$WORKSPACE_PATH/tools/color.sh" - -echo -e "\n${BLUE}#############################################################${ENDCOLOR}" -echo -e "${BLUE}##### #####${ENDCOLOR}" -echo -e "${BLUE}##### Install requirements.txt (app-python version) #####${ENDCOLOR}" -echo -e "${BLUE}##### #####${ENDCOLOR}" -echo -e "${BLUE}#############################################################${ENDCOLOR}" - -source $WORKSPACE_PATH/.venv/bin/activate - -DEPENDENCY_MANAGER=$(jq -r '.customizations.vscode.settings."python.dependencyManager"' $WORKSPACE_PATH/.devcontainer/devcontainer.json); - -if [ "$DEPENDENCY_MANAGER" = "pip" ]; then - - # INSTALL PACKAGE FROM REQUIREMENTS.TXT - - no_existing_file=true - - projects=`find "$SOURCE_PATH" -mindepth 1 -maxdepth 1 -type d -name "*"` - requirements_files=`find "$SOURCE_PATH" -mindepth 2 -maxdepth 2 -type f -name 'requirements.txt'` - hosts_files=`find "$SOURCE_PATH" -mindepth 2 -maxdepth 2 -type f -name 'host.json'` - - if [ -n "$requirements_files" ]; then - - echo -e "\n${GREEN}> Initialize pip Manager (requirements.txt).${ENDCOLOR}" - echo -e "\n${BLUE}At least one 'requirements.txt' file was found.${ENDCOLOR}\n" - IFS=$'\n' - for file in $requirements_files; do - echo -e "- $file" - done - echo -e "\n${BLUE}These files will be used and not modified. No other file will be created.${ENDCOLOR}" - no_existing_file=false - - if [ "$(echo "$requirements_files" | wc -l)" -ne "$(echo "$hosts_files" | wc -l)" ]; then - echo -e "\n${RED}The number of 'requirements.txt' files should match the number of Azure Function projects.${ENDCOLOR}\n" - fi - - fi - - if [ "$no_existing_file" == true ]; then - - echo -e "\n${GREEN}> Initialize pip Manager (requirements.txt).${ENDCOLOR}\n" - - IFS=$'\n' - CONTAINER_TYPE=$(jq -r '.customizations.vscode.settings."container.type"' $WORKSPACE_PATH/.devcontainer/devcontainer.json); - - for project in $projects; do - - echo "" > /tmp/tmp_requirements.txt - - if [ -f "$WORKSPACE_PATH/.devcontainer/templates/default/config/requirements.txt" ]; then - cat "$WORKSPACE_PATH/.devcontainer/templates/default/config/requirements.txt" >> /tmp/tmp_requirements.txt - fi - - if [ -f "$WORKSPACE_PATH/.devcontainer/templates/${CONTAINER_TYPE}/config/requirements.txt" ]; then - cat "$WORKSPACE_PATH/.devcontainer/templates/${CONTAINER_TYPE}/config/requirements.txt" >> /tmp/tmp_requirements.txt - fi - - sed -i '/^$/d' /tmp/tmp_requirements.txt - sort -u /tmp/tmp_requirements.txt > $project/requirements.txt - - rm -f /tmp/tmp_requirements.txt - - done - - echo -e "Pip configuration file was created (requirements.txt)." - - quantity=0 - - echo -e "\n${GREEN}> Install package in the project (requirements.txt).${ENDCOLOR}\n" - - requirements_files=`find "$SOURCE_PATH" -mindepth 2 -maxdepth 2 -type f -name 'requirements.txt'` - - for file in $requirements_files; do - cat "$file" >> /tmp/tmp_requirements.txt - done - - sort -u /tmp/tmp_requirements.txt > /tmp/requirements.txt - - if [ ! -z "$(cat /tmp/requirements.txt)" ]; then - echo -e "${BLUE}> The following packages will be automatically installed.${ENDCOLOR}\n" - cat /tmp/requirements.txt - fi - - rm -f /tmp/requirements.txt /tmp/tmp_requirements.txt - - while true; do - - if [ "$quantity" -eq 0 ]; then - echo -e "\n${BLUE}You can directly edit the 'requirements.txt' files to add packages faster and answer 'no' to the next question.${ENDCOLOR}" - echo -e "\n${YELLOW}Do you want to install a package (pandas, numpy, snowflake-connector-python, streamlit, etc.)? (y/n)${ENDCOLOR}" - read -p "> " choice - else - echo -e "\n${YELLOW}Do you want to install another package (pandas, numpy, marshmallow, streamlit, etc.)? (y/n)${ENDCOLOR}" - read -p "> " choice - fi - - case "$choice" in - [Yy]*) - - echo -e "\n${YELLOW}What is the package name?${ENDCOLOR}" - read -p "> " package_name - - echo -e "" - pip install "$package_name" - - if pip show "$package_name" >/dev/null 2>&1; then - echo -e "\n${BLUE}The package '$package_name' is now installed.${ENDCOLOR}" - - for project in $projects; do - echo $package_name >> $project/requirements.txt - done - - else - echo -e "\n${RED}The package '$package_name' cannot be installed.${ENDCOLOR}" - fi - - quantity=$((quantity + 1)) - - ;; - [Nn]*) echo -n ""; break ;; - *) echo -e "\n${RED}Please answer with 'y' (yes) or 'n' (no).${ENDCOLOR}\n" ;; - - esac - - done - - fi - - echo -e "\n${GREEN}> Install dependencies with pip (requirements.txt).${ENDCOLOR}\n" - - echo "" > /tmp/requirements.txt - echo "" > /tmp/tmp_requirements.txt - - requirements_files=`find "$SOURCE_PATH" -mindepth 2 -maxdepth 2 -type f -name 'requirements.txt'` - - for file in $requirements_files; do - cat "$file" >> /tmp/tmp_requirements.txt - done - - sort -u /tmp/tmp_requirements.txt > /tmp/requirements.txt - - if [ -z "$(cat /tmp/requirements.txt)" ]; then - echo -e "No package to install\n" - else - pip install -r /tmp/requirements.txt - echo -e "\nDone\n" - fi - - rm -f /tmp/requirements.txt /tmp/tmp_requirements.txt - -else - echo -e "\n${YELLOW}Nothing to do because PIP is not used.${ENDCOLOR}" -fi diff --git a/.devcontainer/install/85-extension-installation.sh b/.devcontainer/install/85-extension-installation.sh index ce07a59..2f3bc1d 100755 --- a/.devcontainer/install/85-extension-installation.sh +++ b/.devcontainer/install/85-extension-installation.sh @@ -45,7 +45,7 @@ if [ "$defaultFormatter" == "ms-python.autopep8" ] || [ "$defaultFormatter" == " quantity=(`code --list-extensions --show-versions 2>/dev/null | grep "$defaultFormatter" | wc -l`) - if [ "$quantity" == "0" ]; then + if [ "$quantity" == "-1" ]; then echo -e "\n${RED}The extension has not been automatically installed.${ENDCOLOR}" echo -e "\n${BLUE}To install the extension manually, you can in the sidebar on the left, in the 'extension' menu (ctrl+shift+x) search for the extension from its extension id which is:${ENDCOLOR} '$defaultFormatter'\n" @@ -67,12 +67,12 @@ if [ "$defaultFormatter" == "ms-python.autopep8" ] || [ "$defaultFormatter" == " quantity=(`code --list-extensions --show-versions 2>/dev/null | grep "$defaultFormatter_id" | wc -l`) - if [ "$quantity" == "0" ]; then + if [ "$quantity" == "-1" ]; then echo -e "${RED}The extension seems not to be installed. Please check again!${ENDCOLOR}" echo -e "${RED}Failure to adhere to a common code formatting tool in a Python project can lead to style inconsistencies, merge conflicts, and reduced team productivity.${ENDCOLOR}\n" echo -e "${RED}Despite this anomaly, the container installation processus will continue.${ENDCOLOR}" - else - echo -e "Done. Extension is installed." + # else + # echo -e "Done. Extension is installed." fi fi diff --git a/.devcontainer/install/86-fix-mypy-extension.sh b/.devcontainer/install/86-fix-mypy-extension.sh index d82e803..2f01910 100755 --- a/.devcontainer/install/86-fix-mypy-extension.sh +++ b/.devcontainer/install/86-fix-mypy-extension.sh @@ -14,11 +14,11 @@ ORIGINAL_FILE_1="$HOME/.vscode-server/extensions/ms-python.mypy-type-checker-202 ORIGINAL_FILE_2="$HOME/.vscode-server/extensions/ms-python.mypy-type-checker-2023.6.0/bundled/tool/lsp_server.py" if [ -f "$ORIGINAL_FILE_1" ]; then - $ORIGINAL_FILE=$ORIGINAL_FILE_1 + ORIGINAL_FILE=$ORIGINAL_FILE_1 fi if [ -f "$ORIGINAL_FILE_2" ]; then - $ORIGINAL_FILE=$ORIGINAL_FILE_2 + ORIGINAL_FILE=$ORIGINAL_FILE_2 fi if [ -f "$ORIGINAL_FILE" ]; then diff --git a/.devcontainer/install/__65-generate-file-vscode-launch-json.sh__ b/.devcontainer/install/__65-generate-file-vscode-launch-json.sh__ deleted file mode 100755 index 31edc69..0000000 --- a/.devcontainer/install/__65-generate-file-vscode-launch-json.sh__ +++ /dev/null @@ -1,23 +0,0 @@ -# can-be-removed-after-installation - - if [ -f "$WORKSPACE_PATH/src/$package_directory/run_streamlit.py" ]; then - -new_config=$( -cat < /tmp/temporary_launch.json - mv /tmp/temporary_launch.json "$WORKSPACE_PATH/.vscode/launch.json" - - fi diff --git a/.devcontainer/templates/app-python/config/launch.json b/.devcontainer/templates/app-python/config/launch.json index 176fcc1..7366bd1 100644 --- a/.devcontainer/templates/app-python/config/launch.json +++ b/.devcontainer/templates/app-python/config/launch.json @@ -1,7 +1,7 @@ [ { "name": "Execute module '{project_name}'", - "type": "python", + "type": "debugpy", "request": "launch", "module": "{project_name}" } diff --git a/.devcontainer/templates/azure-function-python/config/extensions.json b/.devcontainer/templates/azure-function-python/config/extensions.json index 0f6626b..fe51488 100644 --- a/.devcontainer/templates/azure-function-python/config/extensions.json +++ b/.devcontainer/templates/azure-function-python/config/extensions.json @@ -1,4 +1 @@ -[ - "ms-azuretools.vscode-azurefunctions", - "ms-python.python" -] +[] diff --git a/.devcontainer/templates/azure-function-python/config/launch.json b/.devcontainer/templates/azure-function-python/config/launch.json index 122791d..2bafba7 100644 --- a/.devcontainer/templates/azure-function-python/config/launch.json +++ b/.devcontainer/templates/azure-function-python/config/launch.json @@ -1,7 +1,7 @@ [ { "name": "Attach to Python Functions: {project_name}", - "type": "python", + "type": "debugpy", "request": "attach", "port": 9091, "preLaunchTask": "Azure Function - Start: {project_name}", diff --git a/.devcontainer/templates/default/config/launch.json b/.devcontainer/templates/default/config/launch.json index d891f63..c643f2c 100644 --- a/.devcontainer/templates/default/config/launch.json +++ b/.devcontainer/templates/default/config/launch.json @@ -1,7 +1,7 @@ [ { "name": "Python: File", - "type": "python", + "type": "debugpy", "request": "launch", "program": "${file}", "console": "integratedTerminal", diff --git a/docs/dev-container-configuration/01-duplicate-configuration-example-file.md b/docs/dev-container-configuration/01-duplicate-configuration-example-file.md index 926c2ed..b50fde9 100644 --- a/docs/dev-container-configuration/01-duplicate-configuration-example-file.md +++ b/docs/dev-container-configuration/01-duplicate-configuration-example-file.md @@ -2,7 +2,21 @@ # Duplicate configuration example file -Before you begin, you need to rename the file named *'devcontainer.json-example'* located in the *'.devcontainer/'* directory to *'devcontainer.json'*. +Before you begin, you need to create a *'devcontainer.json'* file and a *'Dockerfile'* file in the directory *'.devcontainer/'*. + +If you want to create a python application, you need to copy/paste the following files into the directory: *'.devcontainer/'*. + +``` +.devcontainer/templates/app-python/devcontainer.json +.devcontainer/templates/app-python/Dockerfile +``` + +If you want to create an application based on Azure Functions, you must copy/paste the following files into the directory: *'.devcontainer/'*: + +``` +.devcontainer/templates/azure-function-python/devcontainer.json +.devcontainer/templates/azure-function-python/Dockerfile +``` The new tree should look like : @@ -15,6 +29,8 @@ The new tree should look like : └── install.sh ```` + + Now we will mainly modify this file. ###### **▶ Next step: [Configure a packaging and dependency manager](./02-configure-packaging-dependency-manager.md)**