diff --git a/idf_build_apps/args.py b/idf_build_apps/args.py index 565a02e..bd5f44d 100644 --- a/idf_build_apps/args.py +++ b/idf_build_apps/args.py @@ -3,6 +3,7 @@ import argparse import enum +import glob import inspect import logging import os @@ -191,6 +192,15 @@ class DependencyDrivenBuildArguments(GlobalArguments): validation_alias=AliasChoices('manifest_files', 'manifest_file'), default=None, ) + manifest_filepatterns: t.Optional[t.List[str]] = field( + FieldMetadata( + validate_method=[ValidateMethod.TO_LIST], + nargs='+', + ), + description='space-separated list of file patterns to search for the manifest files. ' + 'The matched files will be loaded as the manifest files.', + default=None, + ) manifest_rootpath: str = field( None, description='Root path to resolve the relative paths defined in the manifest files. ' @@ -279,6 +289,17 @@ class DependencyDrivenBuildArguments(GlobalArguments): def model_post_init(self, __context: Any) -> None: super().model_post_init(__context) + if self.manifest_filepatterns: + matched_paths = set() + for pat in [to_absolute_path(p, self.manifest_rootpath) for p in self.manifest_filepatterns]: + matched_paths.update(glob.glob(str(pat), recursive=True)) + + if matched_paths: + if self.manifest_files: + self.manifest_files.extend(matched_paths) + else: + self.manifest_files = list(matched_paths) + Manifest.CHECK_MANIFEST_RULES = self.check_manifest_rules if self.manifest_files: App.MANIFEST = Manifest.from_files( diff --git a/tests/test_cmd.py b/tests/test_cmd.py index ce701b1..20b519d 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -1,6 +1,6 @@ # SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 - +import os import sys from pathlib import Path @@ -67,3 +67,31 @@ def test_manifest_dump_sha_values( f'foo:{sha_of_enable_only_esp32}\n' f'foobar:{sha_of_enable_esp32_or_esp32s2}\n' ) + + +def test_manifest_patterns(tmp_path, monkeypatch, capsys): + manifest = tmp_path / 'manifest.yml' + manifest.write_text( + """foo: + enable: + - if: IDF_TARGET == "esp32" +bar: + enable: + - if: IDF_TARGET == "esp32" + """ + ) + + with pytest.raises(SystemExit) as e: + with monkeypatch.context() as m: + m.setattr( + sys, + 'argv', + ['idf-build-apps', 'find', '--manifest-filepatterns', '**.whatever', '**/manifest.yml', '-vv'], + ) + main() + assert e.retcode == 0 + + _, err = capsys.readouterr() + assert f'Loading manifest file {os.path.join(tmp_path, "manifest.yml")}' in err + assert f'"{os.path.join(tmp_path, "foo")}" does not exist' in err + assert f'"{os.path.join(tmp_path, "bar")}" does not exist' in err