Skip to content

Commit

Permalink
Minor refactor of find_used_variables_* functions (#5296)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Apr 18, 2024
1 parent 7dcab57 commit f2c3f3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
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 @@ -765,23 +766,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|\}}])",
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"}

0 comments on commit f2c3f3b

Please sign in to comment.