Skip to content

Commit

Permalink
Also scan bug_refs.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 3, 2023
1 parent 395ac75 commit a378005
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions scantags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from services import TAG_REGEX
from utils import utc_date

FILE_PATTERN = "*.pm"
LINE_REGEX = rf"soft_fail.*?({TAG_REGEX})"
IGNORE_DIRECTORIES = [".git", "t"]
FILE_PATTERNS = ["*.pm", "bug_refs.json"]
LINE_REGEX = re.compile(rf"soft_fail.*?({TAG_REGEX})")


def git_branch(directory: str) -> str:
Expand Down Expand Up @@ -78,32 +79,39 @@ def check_repo(directory: str, repo_name: str, branch: str, token: str) -> None:
pass


def recursive_grep(
def grep_file(filename: str, line_regex: re.Pattern) -> Iterator[tuple[str, int, str]]:
"""
Grep file
"""
try:
with open(filename, encoding="utf-8") as file:
for line_number, line in enumerate(file, start=1):
for match in line_regex.findall(line):
yield (filename, line_number, match)
except UnicodeError:
pass


def grep_dir(
directory: str,
line_regex: str | re.Pattern,
file_pattern: str = "*",
line_regex: re.Pattern,
file_patterns: list[str],
ignore_dirs: list[str] | None = None,
) -> Iterator[tuple[str, int, str]]:
"""
Recursive grep
"""
if ignore_dirs is None:
ignore_dirs = []
line_regex = re.compile(line_regex)
for root, dirs, files in os.walk(directory):
for ignore in set(ignore_dirs) & set(dirs):
dirs.remove(ignore)
for filename in files:
if not fnmatch.fnmatch(filename, file_pattern):
continue
filename = os.path.join(root, filename)
try:
with open(filename, encoding="utf-8") as file:
for line_number, line in enumerate(file, start=1):
for match in line_regex.findall(line):
yield (filename, line_number, match)
except UnicodeError:
pass
for file_pattern in file_patterns:
if fnmatch.fnmatch(filename, file_pattern):
filename = os.path.join(root, filename)
yield from grep_file(filename, line_regex)
break


def scan_tags( # pylint: disable=too-many-locals
Expand Down Expand Up @@ -142,11 +150,18 @@ def process_line(

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for file, line_number, tag in recursive_grep(
for file, line_number, tag in grep_dir(
directory,
line_regex=LINE_REGEX,
file_pattern=FILE_PATTERN,
ignore_dirs=[".git", "t"],
file_patterns=FILE_PATTERNS,
ignore_dirs=IGNORE_DIRECTORIES,
):
file = file.removeprefix(f"{directory}/")
futures.append(executor.submit(process_line, file, line_number, tag))

for file, line_number, tag in grep_file(
os.path.join(directory, "data", "journal_check", "bug_refs.json"),
line_regex=re.compile(f"({TAG_REGEX})"),
):
file = file.removeprefix(f"{directory}/")
futures.append(executor.submit(process_line, file, line_number, tag))
Expand Down

0 comments on commit a378005

Please sign in to comment.