diff --git a/src/mccode_antlr/cli/extract.py b/src/mccode_antlr/cli/extract.py index beb1264c..a7be779c 100644 --- a/src/mccode_antlr/cli/extract.py +++ b/src/mccode_antlr/cli/extract.py @@ -42,6 +42,7 @@ def extract( members: list[str] | None = None, exclude: list[str] | None = None, repository: list[str] | None = None, + exclude_repository: list[str] | None = None, list_only: bool = False, ): from types import SimpleNamespace @@ -55,13 +56,14 @@ def extract( if list_only: manifest = build_manifest(instr, include_remote=include_remote) - _print_manifest(select_members(manifest, members or None, exclude, repository=repository)) + _print_manifest(select_members(manifest, members or None, exclude, repository=repository, + exclude_repository=exclude_repository)) return destination = Path(output).resolve() if output is not None else _default_output_directory(source) extract_to_directory( instr, destination, include_remote=include_remote, select=members or None, exclude=exclude, - repository=repository, + repository=repository, exclude_repository=exclude_repository, ) print(destination) @@ -131,6 +133,14 @@ def add_extract_management_parser(modes): "an embedded registry's name. Naming a repository extracts/lists its " "files even without --include-remote, like positional members.", ) + parser.add_argument( + '-X', '--exclude-repository', + action='append', + default=None, + help="Glob pattern of source repositories to omit (repeatable), matched like " + "-r/--repository. E.g. --include-remote -X '*/mccode-dev/McCode' bundles " + "everything a stock McStas installation does not already provide.", + ) parser.add_argument( '-l', '--list', dest='list_only', diff --git a/src/mccode_antlr/io/extract.py b/src/mccode_antlr/io/extract.py index 09107973..5e107e87 100644 --- a/src/mccode_antlr/io/extract.py +++ b/src/mccode_antlr/io/extract.py @@ -229,6 +229,7 @@ def select_members( select: list[str] | None = None, exclude: list[str] | None = None, repository: list[str] | None = None, + exclude_repository: list[str] | None = None, ) -> list[ExtractMember]: """The subset of *manifest* a given extract/list invocation would produce. @@ -241,7 +242,10 @@ def select_members( explicitly is unambiguous. When both `select` and `repository` are given they AND together: a member must match a `select` pattern (by name) *and* a `repository` pattern (against `repository_match`). `exclude` globs are - then dropped from whichever set results, same as `unzip -x`. + then dropped from whichever set results, same as `unzip -x`, and so are + members whose `repository_match` matches an `exclude_repository` glob -- + e.g. '*/mccode-dev/McCode' keeps a stock McStas's own library out of an + --include-remote bundle. """ if select or repository: chosen = manifest @@ -253,6 +257,9 @@ def select_members( chosen = [m for m in manifest if m.default_included] if exclude: chosen = [m for m in chosen if not any(fnmatch(m.name, pattern) for pattern in exclude)] + if exclude_repository: + chosen = [m for m in chosen + if not any(fnmatch(m.repository_match, pattern) for pattern in exclude_repository)] return chosen @@ -260,6 +267,7 @@ def extract_to_directory( instr, directory, include_remote: bool = False, select: list[str] | None = None, exclude: list[str] | None = None, repository: list[str] | None = None, + exclude_repository: list[str] | None = None, ) -> Path: """Reconstitute *instr* into *directory*: .instr hierarchy, stashed files, and component definitions, all flat. @@ -267,11 +275,12 @@ def extract_to_directory( With include_remote=False (default) and no select/exclude/repository, only files not otherwise available from a remote registry are written -- what a consumer could not get any other way. See `select_members` for how - include_remote, select, exclude, and repository interact. + include_remote, select, exclude, repository and exclude_repository interact. """ directory = Path(directory) directory.mkdir(parents=True, exist_ok=True) manifest = build_manifest(instr, include_remote) - for member in select_members(manifest, select, exclude, repository=repository): + for member in select_members(manifest, select, exclude, repository=repository, + exclude_repository=exclude_repository): _write_flat(instr, directory, member.name, member.payload) return directory diff --git a/tests/test_cli_extract.py b/tests/test_cli_extract.py index 72d2c839..beb6e0e4 100644 --- a/tests/test_cli_extract.py +++ b/tests/test_cli_extract.py @@ -186,6 +186,31 @@ def test_extract_repository_filter_selects_only_matching_files(tmp_path): assert {p.name for p in destination.iterdir()} == {'Progress_bar.comp'} +def test_extract_exclude_repository_drops_matching_files(tmp_path): + """--include-remote -X '*/mccode-dev/*' bundles everything but the stock library.""" + from mccode_antlr.cli.extract import extract + + lib = tmp_path / 'lib' + lib.mkdir() + (lib / 'UsesLib.comp').write_text( + 'DEFINE COMPONENT UsesLib\nSETTING PARAMETERS (thing=1)\nTRACE %{ SCATTER; %}\nEND\n' + ) + instr_path = tmp_path / 'u.instr' + instr_path.write_text( + 'DEFINE INSTRUMENT u()\nTRACE\n' + 'COMPONENT o = Progress_bar() AT (0,0,0) ABSOLUTE\n' + 'COMPONENT m = UsesLib(thing=3) AT (0,0,1) ABSOLUTE\nEND\n' + ) + + destination = tmp_path / 'bundle' + extract( + filename=str(instr_path), output=str(destination), search_dir=[lib], + include_remote=True, exclude_repository=['*/mccode-dev/*'], + ) + + assert {p.name for p in destination.iterdir()} == {'u.instr', 'UsesLib.comp'} + + def test_trust_local_registries_does_not_duplicate_embedded_listing(tmp_path, capsys, monkeypatch): """--trust-local-registries can make a file both embedded (from the JSON) and locally resolvable (from the restored registry) -- it must be listed