Skip to content

Commit

Permalink
extra_env_vars support fnmatch globs
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Dec 19, 2024
1 parent d462949 commit 47dc38c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/notes/2.25.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Thank you to [Klayvio](https://www.klaviyo.com/) and [Normal Computing](https://
- Fixed a longstanding bug in the processing of [synthetic targets](https://www.pantsbuild.org/2.24/docs/writing-plugins/the-target-api/concepts#synthetic-targets-api). This fix has the side-effect of requiring immutability and hashability of scalar values in BUILD files, which was always assumed but not enforced. This may cause BUILD file parsing errors, if you have custom field types involving custom mutable data structures. See ([#21725](https://github.com/pantsbuild/pants/pull/21725)) for more.
- [Fixed](https://github.com/pantsbuild/pants/pull/21665) bug where `pants --export-resolve=<resolve> --export-py-generated-sources-in-resolve=<resolve>` fails (see [#21659](https://github.com/pantsbuild/pants/issues/21659) for more info).
- [Fixed](https://github.com/pantsbuild/pants/pull/21694) bug where an `archive` target is unable to produce a ZIP file with no extension (see [#21693](https://github.com/pantsbuild/pants/issues/21693) for more info).
- `extra_env_vars` (on many subsystems) now supports a generalised glob syntax using Python [fnmatch](https://docs.python.org/3/library/fnmatch.html) to construct patterns like `AWS_*`, `TF_*`, and `S2TESTS_*`.
- `[subprocess-environment].env_vars` and `extra_env_vars` (on many subsystems) now supports a generalised glob syntax using Python [fnmatch](https://docs.python.org/3/library/fnmatch.html) to construct patterns like `AWS_*`, `TF_*`, and `S2TESTS_*`.

#### Remote Caching/Execution

Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/engine/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def check_and_set(name: str, value: Optional[str]):
return FrozenDict(env_var_subset)

def get_or_match(self, name_or_pattern: str) -> dict[str, str]:
"""
Get the value of an envvar if it has an exact match, otherwise all fnmatches.
"""
"""Get the value of an envvar if it has an exact match, otherwise all fnmatches."""
if name_or_pattern in self:
return {name_or_pattern: self.get(name_or_pattern)}
return {k: v for k, v in self.items() if fnmatch.fnmatch(k, name_or_pattern)}
Expand Down
12 changes: 7 additions & 5 deletions src/python/pants/engine/environment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ def test_invalid_variable() -> None:


def test_envvar_fnmatch() -> None:
"""Test fnmatch patterns correctly pull in all matching envvars"""
"""Test fnmatch patterns correctly pull in all matching envvars."""

pants_env = CompleteEnvironmentVars({
"LETTER_C": "prefix_char_match",
"LETTER_PI": "prefix",
})
pants_env = CompleteEnvironmentVars(
{
"LETTER_C": "prefix_char_match",
"LETTER_PI": "prefix",
}
)

char_match = pants_env.get_subset(["LETTER_?"])
assert char_match == {"LETTER_C": "prefix_char_match"}
Expand Down

0 comments on commit 47dc38c

Please sign in to comment.