Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/mccode_antlr/cli/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 12 additions & 3 deletions src/mccode_antlr/io/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -253,25 +257,30 @@ 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


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.

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
25 changes: 25 additions & 0 deletions tests/test_cli_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading