From cbac39c9848b8ba2dc79b6eeeaf4a8eaf06d646a Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Thu, 30 Apr 2026 09:26:47 +0530 Subject: [PATCH] ci(esp_wifi_remote): silently exit if related files are not modified --- .../remote/scripts/generate_and_check.py | 60 +++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/components/esp_wifi/remote/scripts/generate_and_check.py b/components/esp_wifi/remote/scripts/generate_and_check.py index fac36d38a95..05b67784f7a 100644 --- a/components/esp_wifi/remote/scripts/generate_and_check.py +++ b/components/esp_wifi/remote/scripts/generate_and_check.py @@ -15,15 +15,12 @@ from typing import cast script_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.abspath(os.getcwd()) -if script_dir != current_dir: - print(f'Error: This script must be run from its own directory: {script_dir}') - print(f'Current working directory is: {current_dir}') - sys.exit(1) idf_path = os.getenv('IDF_PATH') if idf_path is None: idf_path = os.path.realpath(os.path.join(script_dir, '..', '..', '..', '..')) + Param = namedtuple('Param', ['ptr', 'array', 'qual', 'type', 'name']) component_path = os.path.normpath(os.path.join(os.path.realpath(__file__), '..', '..')) @@ -114,10 +111,43 @@ def exec_cmd(what: list[str], out_file: IO[str] | None = None) -> tuple[int, str return rc, output, err, ' '.join(what) +def _has_related_changes() -> bool: + try: + # Check if we are in a git repository + rc, _, _, _ = exec_cmd(['git', 'rev-parse', '--is-inside-work-tree']) + if rc != 0: + return True + + # Get changes in tracked files (staged and unstaged) relative to HEAD + rc, output, _, _ = exec_cmd(['git', 'diff', 'HEAD', '--name-only']) + if rc != 0: + return True + except Exception: + return True + + # Patterns from .pre-commit-config.yaml plus the script itself + related_files_re = re.compile( + r"""(?x)^( + components/esp_wifi/remote/scripts/(generate_and_check\.py|ignore_extensions\.h|copyright_header\.h)| + components/esp_wifi/include/(esp_wifi.*|esp_mesh.*|esp_now.*)\.h| + components/esp_wifi/Kconfig| + components/wpa_supplicant/esp_supplicant/include/esp_eap_client\.h| + components/soc/.+/include/soc/Kconfig\.soc_caps\.in + )$""" + ) + + for line in output.splitlines(): + if related_files_re.match(line): + return True + return False + + _cached_include_dir_flags: list[str] | None = None def _handle_missing_tool(msg: str) -> None: + if not _has_related_changes(): + sys.exit(0) YELLOW = '\033[33m' RESET = '\033[0m' full_msg = ( @@ -132,12 +162,6 @@ def _handle_missing_tool(msg: str) -> None: sys.exit(0) -try: - import pycparser # noqa: F401 -except ImportError: - _handle_missing_tool('ESP-IDF environment not found (missing python dependencies)') - - def preprocess(idf_path: str, header: str) -> str: global _cached_include_dir_flags project_dir = os.path.join(idf_path, 'examples', 'wifi', 'getting_started', 'station') @@ -711,6 +735,22 @@ making changes you might need to modify 'copyright_header.h' in the script direc parser.add_argument('--base-dir', help='Base directory to compare generated files against') args = parser.parse_args() + # Skip work if no related files have changed, unless we are in CI or doing a base-dir comparison. + # CI environments usually require a full check to ensure repository integrity even if + # no local uncommitted changes are present. + if args.base_dir is None and not os.getenv('CI') and not _has_related_changes(): + sys.exit(0) + + if script_dir != current_dir: + print(f'Error: This script must be run from its own directory: {script_dir}') + print(f'Current working directory is: {current_dir}') + sys.exit(1) + + try: + import pycparser # noqa: F401 + except ImportError: + _handle_missing_tool('ESP-IDF environment not found (missing python dependencies)') + header = os.path.join(idf_path, 'components', 'esp_wifi', 'include', 'esp_wifi.h') eap_header = os.path.join(idf_path, 'components', 'wpa_supplicant', 'esp_supplicant', 'include', 'esp_eap_client.h') function_prototypes = extract_function_prototypes(preprocess(idf_path, header), header, ['esp_wifi_'])