Skip to content

Commit

Permalink
Refactor find_used_variables_*
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard committed Apr 18, 2024
1 parent c95dff8 commit 5381d97
Showing 1 changed file with 35 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 @@ -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|\}}])",
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,
)
)
}

0 comments on commit 5381d97

Please sign in to comment.