Skip to content

Commit

Permalink
Approach #1: check_banned_import_if_snippet_present()
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed May 11, 2019
1 parent 762c03a commit d6d2a90
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion build-support/bin/check_banned_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def main() -> None:
)
rust_files = find_files("src/rust/engine", extension=".rs")

check_banned_import(
check_banned_import_if_snippet_present(
python_files,
snippet_regex=r"^from __future__ import",
bad_import_regex=r"^import subprocess$",
correct_import_message="`from pants.util.process_handler import subprocess`"
)
Expand Down Expand Up @@ -54,5 +55,19 @@ def check_banned_import(files: List[str], *, bad_import_regex: str, correct_impo
f"{correct_import_message}. Bad files:\n{bad_files_str}")


def check_banned_import_if_snippet_present(
files: List[str], *, snippet_regex: str, bad_import_regex: str, correct_import_message: str
) -> None:
regex = re.compile(snippet_regex)
filtered_files: List[str] = []
for fp in files:
with open(fp, 'r') as f:
if any(re.search(regex, line) for line in f.readlines()):
filtered_files.append(fp)
check_banned_import(
filtered_files, bad_import_regex=bad_import_regex, correct_import_message=correct_import_message
)


if __name__ == "__main__":
main()

0 comments on commit d6d2a90

Please sign in to comment.