From cf3ff253d1c866d06f51d93bc525f3a5ab4f6f00 Mon Sep 17 00:00:00 2001 From: Ahzyuan Date: Thu, 25 Sep 2025 19:41:06 +0800 Subject: [PATCH 1/2] refactor(misc): extract common utilities to shared script - Extracted common functions (e.g., `red_output`, `green_output`, `find_dir`) into a new shared `utils.sh` script to reduce duplication across shell scripts. - Updated existing scripts to source `utils.sh` instead of redefining helper functions. - Removed redundant or commented-out code blocks (e.g., drawio and xvfb checks in `docs_preview.sh`). --- misc/check_meta.sh | 48 +++-------------- misc/docs_preview.sh | 106 ++++++++++---------------------------- misc/lint_format.sh | 71 +++++-------------------- misc/packaging.sh | 63 +++------------------- misc/utils.sh | 66 ++++++++++++++++++++++++ misc/validate_pr_title.sh | 8 +-- 6 files changed, 123 insertions(+), 239 deletions(-) create mode 100644 misc/utils.sh diff --git a/misc/check_meta.sh b/misc/check_meta.sh index 80db1ee..0cbf972 100755 --- a/misc/check_meta.sh +++ b/misc/check_meta.sh @@ -1,48 +1,16 @@ #!/usr/bin/env bash -red_output() { - echo -e "\033[31m$1\033[0m" -} - -green_output() { - echo -e "\033[32m$1\033[0m" -} - -yellow_output() { - echo -e "\033[33m$1\033[0m" -} - -find_dir() { - local target_path=$1 - local current_path=$(realpath $(dirname $0)) - local found_path="" - - while [ "$current_path" != "/" ]; do - if [ -d "$current_path/$target_path" ]; then - found_path="$current_path" - break - elif [ -f "$current_path/$target_path" ]; then - found_path="$current_path" - break - fi - current_path=$(dirname "$current_path") - done - - if [ -z "$found_path" ]; then - echo "Error: $target_path not found!" - exit 1 - else - echo $found_path - fi -} +source "$(dirname "$0")/utils.sh" # --------------------------------------------------------------------------------------------------- -ROOT=$(find_dir 'torchmeter') -if [[ $? -ne 0 ]]; then - exit 1 +if ROOT="$(find_dir 'torchmeter')"; then + cd "$ROOT" || { + red_output "Error: Cannot enter $ROOT" + exit 1 + } else - cd $ROOT + exit 1 fi if [[ ! -d "$ROOT/dist" ]]; then @@ -60,7 +28,7 @@ tar_requires_python=$(echo "$tar_output" | grep 'Requires-Python:' | head -n 1 | echo -e "Metadata_version: $metadata_version $(yellow_output '(need >=1.2)')" echo -e "Python Required: $tar_requires_python $(yellow_output "(need >=$requires_python)")" echo "----------------------------------------" -if [[ "$metadata_version" > "1.2" ]] && \ +if (( $(echo "$metadata_version > 1.2" | bc -l) )) && \ [[ $(echo -e "$tar_requires_python\n$requires_python" | sort -V | head -n 1) == "3.8" ]]; then green_output "Metadata and requires-python are correct." else diff --git a/misc/docs_preview.sh b/misc/docs_preview.sh index a592dce..e7671ec 100644 --- a/misc/docs_preview.sh +++ b/misc/docs_preview.sh @@ -1,44 +1,6 @@ #! /usr/bin/env bash -cyan_output() { - echo -e "\033[36m$1\033[0m" -} - -yellow_output() { - echo -e "\033[33m$1\033[0m" -} - -green_output() { - echo -e "\033[32m$1\033[0m" -} - -red_output() { - echo -e "\033[31m$1\033[0m" -} - -find_dir() { - local target_path=$1 - local current_path=$(realpath $(dirname $0)) - local found_path="" - - while [ "$current_path" != "/" ]; do - if [ -d "$current_path/$target_path" ]; then - found_path="$current_path" - break - elif [ -f "$current_path/$target_path" ]; then - found_path="$current_path" - break - fi - current_path=$(dirname "$current_path") - done - - if [ -z "$found_path" ]; then - echo "Error: $target_path not found!" - exit 1 - else - echo $found_path - fi -} +source "$(dirname "$0")/utils.sh" check_py_dependencies() { @@ -54,30 +16,32 @@ try: deps = config["options.extras_require"]["docs"].strip() for dep in deps.split('\n'): dep = re.findall(r"(\w+)(\[.+\])?", dep)[0][0] - if os.system(f"pip list | grep '{dep}'"): + if os.system(f"pip list | grep '{dep}' > /dev/null"): print(dep) except KeyError: sys.exit("Error: Missing [options.extras_require] or docs dependencies in setup.cfg") EOF ) + # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then red_output "Failed to parse setup.cfg" exit 1 - else - LACK=($LACK) fi - for dep in "${Deps[@]}"; do - if [[ -z "$dep" ]]; then - continue - fi - - pip install "$dep" || { - red_output "Failed to install $dep" - exit 1 - } - done + if [[ -n "$LACK" ]]; then + LACK=("$LACK") + for dep in "${LACK[@]}"; do + if [[ -z "$dep" ]]; then + continue + fi + + pip install "$dep" || { + red_output "Failed to install $dep" + exit 1 + } + done + fi green_output "All docs dependencies have been installed." } @@ -129,11 +93,13 @@ while [[ "$#" -gt 0 ]]; do done # navigate to project root -ROOT=$(find_dir 'torchmeter') -if [[ $? -ne 0 ]]; then +if ! ROOT="$(find_dir 'torchmeter')"; then exit 1 else - cd $ROOT + cd "$ROOT" || { + red_output "Error: Cannot enter $ROOT" + exit 1 + } fi if [[ -d "$ROOT/dist" ]]; then @@ -141,23 +107,7 @@ if [[ -d "$ROOT/dist" ]]; then fi # activate conda env -eval "$(conda shell.bash hook)" - -envs=$(conda env list | grep -v "#" | cut -d " " -f1) - -cyan_output "Available conda environments:" -PS3="Choose your Python env: " -select env in $envs -do - if [ -n "$env" ]; then - cyan_output "$env selected." - conda activate "$env" - green_output "$env activated." - break - else - red_output "Invalid selection. Please try again." - fi -done +activate_conda_env || exit 1 # check dependencies if [ "$QUICK_MODE" = false ]; then @@ -166,13 +116,13 @@ if [ "$QUICK_MODE" = false ]; then cyan_output "\nChecking Python dependencies..." check_py_dependencies - # Check Drawio - cyan_output "\nChecking drawio..." - check_drawio + # # Check Drawio + # cyan_output "\nChecking drawio..." + # check_drawio - # Check xvfb - cyan_output "\nChecking xvfb..." - check_xvfb + # # Check xvfb + # cyan_output "\nChecking xvfb..." + # check_xvfb else yellow_output "Quick mode enabled: skipping all dependency checks." diff --git a/misc/lint_format.sh b/misc/lint_format.sh index dc79c7d..e6844d7 100644 --- a/misc/lint_format.sh +++ b/misc/lint_format.sh @@ -1,65 +1,18 @@ #!/usr/bin/env bash -green_output() { - echo -e "\033[32m$1\033[0m" -} +source "$(dirname "$0")/utils.sh" -cyan_output() { - echo -e "\033[36m\033[0m" -} - -find_dir() { - local target_path=$1 - local current_path=$(realpath $(dirname $0)) - local found_path="" - - while [ "$current_path" != "/" ]; do - if [ -d "$current_path/$target_path" ]; then - found_path="$current_path" - break - elif [ -f "$current_path/$target_path" ]; then - found_path="$current_path" - break - fi - current_path=$(dirname "$current_path") - done - - if [ -z "$found_path" ]; then - echo "Error: $target_path not found!" - exit 1 - else - echo $found_path - fi -} - -# --------------------------------------------------------------------------------------------------- - -ROOT=$(find_dir 'torchmeter') -if [[ $? -ne 0 ]]; then +if ! ROOT="$(find_dir 'torchmeter')"; then exit 1 else - cd $ROOT + cd "$ROOT" || { + red_output "Error: Cannot enter $ROOT" + exit 1 + } fi # -------------------------------------- Activate Virtual Env --------------------------------------- - -eval "$(conda shell.bash hook)" - -envs=$(conda env list | grep -v "#" | cut -d " " -f1) - -cyan_output "Available conda environments:" -PS3="Choose your Python env: " -select env in $envs -do - if [ -n "$env" ]; then - cyan_output "$env selected." - conda activate "$env" - green_output "$env activated.\n" - break - else - red_output "Invalid selection. Please try again." - fi -done +activate_conda_env || exit 1 # --------------------------------------------- Format ----------------------------------------------- @@ -71,10 +24,10 @@ exit_code=$? set -e if [[ $exit_code -eq 0 ]]; then - echo -e "✅ Formatting finish! 🎉\n" + green_output "✅ Formatting finish! 🎉\n" else - echo -e "❌ Formatting failed! Some code does not meet the format requirements!s" >&2 - echo -e "❌ Ruff terminates abnormally due to invalid configuration, invalid CLI options, or an internal error" >&2 + red_output "❌ Formatting failed! Some code does not meet the format requirements!s" >&2 + red_output "❌ Ruff terminates abnormally due to invalid configuration, invalid CLI options, or an internal error" >&2 exit 1 fi @@ -91,8 +44,8 @@ exit_code=$? set -e if [[ $exit_code -eq 0 ]]; then - echo -e "✅ Linting passed! Code quality check successful! 🎉\n" + green_output "✅ Linting passed! Code quality check successful! 🎉\n" else - echo -e "❌ Linting failed! Some code does not meet the linting rules!\n" >&2 + red_output "❌ Linting failed! Some code does not meet the linting rules!\n" >&2 exit 1 fi \ No newline at end of file diff --git a/misc/packaging.sh b/misc/packaging.sh index 44361f1..5886d20 100755 --- a/misc/packaging.sh +++ b/misc/packaging.sh @@ -1,71 +1,24 @@ #! /usr/bin/env bash -cyan_output() { - echo -e "\033[36m$1\033[0m" -} - -green_output() { - echo -e "\033[32m$1\033[0m" -} - -red_output() { - echo -e "\033[31m$1\033[0m" -} - -find_dir() { - local target_path=$1 - local current_path=$(realpath $(dirname $0)) - local found_path="" - - while [ "$current_path" != "/" ]; do - if [ -d "$current_path/$target_path" ]; then - found_path="$current_path" - break - elif [ -f "$current_path/$target_path" ]; then - found_path="$current_path" - break - fi - current_path=$(dirname "$current_path") - done - - if [ -z "$found_path" ]; then - echo "Error: $target_path not found!" - exit 1 - else - echo $found_path - fi -} +source "$(dirname "$0")/utils.sh" # --------------------------------------------------------------------------------------------------- -ROOT=$(find_dir 'torchmeter') -if [[ $? -ne 0 ]]; then + +if ! ROOT=$(find_dir 'torchmeter'); then exit 1 else - cd $ROOT + cd "$ROOT" || { + red_output "Error: Cannot enter $ROOT" + exit 1 + } fi if [[ -d "$ROOT/dist" ]]; then rm -r "$ROOT/dist" fi -eval "$(conda shell.bash hook)" - -envs=$(conda env list | grep -v "#" | cut -d " " -f1) - -cyan_output "Available conda environments:" -PS3="Choose your Python env: " -select env in $envs -do - if [ -n "$env" ]; then - cyan_output "$env selected." - conda activate "$env" - green_output "$env activated." - break - else - red_output "Invalid selection. Please try again." - fi -done +activate_conda_env || exit 1 cyan_output "building..." python -m build -v -n . \ No newline at end of file diff --git a/misc/utils.sh b/misc/utils.sh new file mode 100644 index 0000000..8119ec2 --- /dev/null +++ b/misc/utils.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash + +cyan_output() { + echo -e "\033[36m$1\033[0m" +} + +yellow_output() { + echo -e "\033[33m$1\033[0m" +} + +green_output() { + echo -e "\033[32m$1\033[0m" +} + +red_output() { + echo -e "\033[31m$1\033[0m" +} + +activate_conda_env() { + eval "$(conda shell.bash hook)" + + envs=$(conda env list | grep -v "#" | cut -d " " -f1) + + cyan_output "Available conda environments:" + PS3="Choose your Python env: " + select env in $envs + do + if [ -n "$env" ]; then + cyan_output "$env selected." + conda activate "$env" || { + red_output "Error: Cannot activate $env" + exit 1 + } + green_output "$env activated." + break + else + red_output "Invalid selection. Please try again." + fi + done +} + +find_dir() { + local target_path=$1 + local current_path found_path + + current_path=$(realpath "$(dirname "$0")") + found_path="" + + while [ "$current_path" != "/" ]; do + if [ -d "$current_path/$target_path" ]; then + found_path="$current_path" + break + elif [ -f "$current_path/$target_path" ]; then + found_path="$current_path" + break + fi + current_path=$(dirname "$current_path") + done + + if [ -z "$found_path" ]; then + echo "Error: $target_path not found!" + exit 1 + else + echo "$found_path" + fi +} \ No newline at end of file diff --git a/misc/validate_pr_title.sh b/misc/validate_pr_title.sh index 6a0eec2..03e02ed 100644 --- a/misc/validate_pr_title.sh +++ b/misc/validate_pr_title.sh @@ -1,12 +1,6 @@ #!/usr/bin/env bash -red_output() { - echo -e "\033[31m$1\033[0m" >&2 -} - -green_output() { - echo -e "\033[32m$1\033[0m" -} +source "$(dirname "$0")/utils.sh" # --------------------------------------------------------------------------------------------------- From 5dbe34d8c71b68bf980910971e6a5bc371e560fd Mon Sep 17 00:00:00 2001 From: Ahzyuan Date: Thu, 25 Sep 2025 20:10:40 +0800 Subject: [PATCH 2/2] chore: add author info in all shell scripts --- misc/check_meta.sh | 4 ++++ misc/docs_preview.sh | 4 ++++ misc/lint_format.sh | 4 ++++ misc/packaging.sh | 4 ++++ misc/utils.sh | 24 ++++++++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/misc/check_meta.sh b/misc/check_meta.sh index 0cbf972..0b790a8 100755 --- a/misc/check_meta.sh +++ b/misc/check_meta.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# TorchMeter, AGPL-3.0 license +# Author: Ahzyuan +# Repo: https://github.com/TorchMeter/torchmeter + source "$(dirname "$0")/utils.sh" # --------------------------------------------------------------------------------------------------- diff --git a/misc/docs_preview.sh b/misc/docs_preview.sh index e7671ec..585f25c 100644 --- a/misc/docs_preview.sh +++ b/misc/docs_preview.sh @@ -1,5 +1,9 @@ #! /usr/bin/env bash +# TorchMeter, AGPL-3.0 license +# Author: Ahzyuan +# Repo: https://github.com/TorchMeter/torchmeter + source "$(dirname "$0")/utils.sh" check_py_dependencies() { diff --git a/misc/lint_format.sh b/misc/lint_format.sh index e6844d7..204427d 100644 --- a/misc/lint_format.sh +++ b/misc/lint_format.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# TorchMeter, AGPL-3.0 license +# Author: Ahzyuan +# Repo: https://github.com/TorchMeter/torchmeter + source "$(dirname "$0")/utils.sh" if ! ROOT="$(find_dir 'torchmeter')"; then diff --git a/misc/packaging.sh b/misc/packaging.sh index 5886d20..7905f39 100755 --- a/misc/packaging.sh +++ b/misc/packaging.sh @@ -1,5 +1,9 @@ #! /usr/bin/env bash +# TorchMeter, AGPL-3.0 license +# Author: Ahzyuan +# Repo: https://github.com/TorchMeter/torchmeter + source "$(dirname "$0")/utils.sh" # --------------------------------------------------------------------------------------------------- diff --git a/misc/utils.sh b/misc/utils.sh index 8119ec2..52d0b30 100644 --- a/misc/utils.sh +++ b/misc/utils.sh @@ -1,5 +1,10 @@ #!/usr/bin/env bash +# TorchMeter, AGPL-3.0 license +# Author: Ahzyuan +# Repo: https://github.com/TorchMeter/torchmeter + +# ---------------------------- Colorful Output ---------------------------- cyan_output() { echo -e "\033[36m$1\033[0m" } @@ -16,6 +21,15 @@ red_output() { echo -e "\033[31m$1\033[0m" } +# ---------------------------- Helper Functions ---------------------------- + +# Interactively activate a conda environment +# This function lists all available conda environments and allows user to select one +# Usage: activate_conda_env +# Dependencies: conda must be installed and available in PATH +# Side effects: +# - Activates the selected conda environment +# - Exits with code 1 if activation fails or conda is not available activate_conda_env() { eval "$(conda shell.bash hook)" @@ -39,6 +53,16 @@ activate_conda_env() { done } +# Find the root directory containing a specific file or directory +# Searches upward from the script's location until it finds the target or reaches root +# Usage: find_dir "target_file_or_directory" +# Args: +# $1: target_path - The file or directory name to search for +# Returns: +# The absolute path of the directory containing the target +# Exit codes: +# 0: Success (target found) +# 1: Failure (target not found, reached filesystem root) find_dir() { local target_path=$1 local current_path found_path