Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor of find_used_variables_* functions #5296

Merged
merged 2 commits into from
Apr 18, 2024
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
53 changes: 35 additions & 18 deletions conda_build/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from copy import copy
from functools import lru_cache
from itertools import product
from pathlib import Path
from typing import TYPE_CHECKING

import yaml
Expand Down Expand Up @@ -766,23 +767,39 @@ def find_used_variables_in_text(variant, recipe_text, selectors_only=False):
return used_variables


def find_used_variables_in_shell_script(variant, file_path):
with open(file_path) as f:
text = f.read()
used_variables = set()
for v in variant:
variant_regex = rf"(^[^$]*?\$\{{?\s*{re.escape(v)}\s*[\s|\}}])"
if re.search(variant_regex, text, flags=re.MULTILINE | re.DOTALL):
used_variables.add(v)
return used_variables
def find_used_variables_in_shell_script(
variants: Iterable[str],
file_path: str | os.PathLike | Path,
) -> set[str]:
text = Path(file_path).read_text()
return {
variant
for variant in variants
if (
variant in text # str in str is faster than re.search
and re.search(
rf"(^[^$]*?\$\{{?\s*{re.escape(variant)}\s*[\s|\}}])",
Copy link
Member

@jezdez jezdez Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to compile and cache this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not any more than re already does since we're formatting the pattern with the variant in each iteration

text,
flags=re.MULTILINE | re.DOTALL,
)
)
}


def find_used_variables_in_batch_script(variant, file_path):
with open(file_path) as f:
text = f.read()
used_variables = set()
for v in variant:
variant_regex = rf"\%{re.escape(v)}\%"
if re.search(variant_regex, text, flags=re.MULTILINE | re.DOTALL):
used_variables.add(v)
return used_variables
def find_used_variables_in_batch_script(
variants: Iterable[str],
file_path: str | os.PathLike | Path,
) -> set[str]:
text = Path(file_path).read_text()
return {
variant
for variant in variants
if (
variant in text # str in str is faster than re.search
and re.search(
rf"\%{re.escape(variant)}\%",
text,
flags=re.MULTILINE | re.DOTALL,
)
)
}
24 changes: 24 additions & 0 deletions tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
combine_specs,
dict_of_lists_to_list_of_dicts,
filter_combined_spec_to_used_keys,
find_used_variables_in_batch_script,
find_used_variables_in_shell_script,
get_package_variants,
get_vars,
validate_spec,
Expand Down Expand Up @@ -715,3 +717,25 @@ def test_get_vars():
]

assert get_vars(variants) == {"nodejs"}


def test_find_used_variables_in_shell_script(tmp_path: Path) -> None:
variants = ("FOO", "BAR", "BAZ", "QUX")
(script := tmp_path / "script.sh").write_text(
f"${variants[0]}\n"
f"${{{variants[1]}}}\n"
f"${{{{{variants[2]}}}}}\n"
f"$${variants[3]}\n"
)
assert find_used_variables_in_shell_script(variants, script) == {"FOO", "BAR"}


def test_find_used_variables_in_batch_script(tmp_path: Path) -> None:
variants = ("FOO", "BAR", "BAZ", "QUX")
(script := tmp_path / "script.sh").write_text(
f"%{variants[0]}%\n"
f"%%{variants[1]}%%\n"
f"${variants[2]}\n"
f"${{{variants[3]}}}\n"
)
assert find_used_variables_in_batch_script(variants, script) == {"FOO", "BAR"}
Loading