Skip to content

Commit

Permalink
[DEV-2453] Allow excluding patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarkovtsev committed Jun 8, 2021
1 parent ac551e4 commit e33a431
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import re
import sys

from typing import List


def compile_regex() -> re.Pattern:
try:
Expand All @@ -30,58 +32,75 @@ def compile_regex() -> re.Pattern:
return x


def scan_file(path: Path, comment: str, regexp: re.Pattern) -> bool:
def scan_file(path: Path, regexp: re.Pattern, excluded_patterns: List[str], reldir: str) -> bool:
try:
with path.open(encoding="utf-8") as fin:
if not comment:
text = fin.read()
else:
text = "".join(line for line in fin if not line.lstrip().startswith(comment))
text = fin.read()
except Exception as e:
print(f"Warning: failed to read {path}: {type(e).__name__}: {e}", file=sys.stderr)
if matches := list(regexp.finditer(text)):
print(f"Found {len(matches)} confusing symbol{'s' if len(matches) > 1 else ''} in {path}",
file=sys.stderr)
if excluded_patterns:
exclude_re = re.compile(f"{'|'.join(re.escape(p) for p in excluded_patterns)}")
exclude_window = max(len(p) for p in excluded_patterns)
passed = []
for match in matches:
print(f"Offset {match.start()}: {match.group(0)}", file=sys.stderr)
return False
skip = False
if excluded_patterns:
offset = max(0, match.start() - exclude_window)
locality = text[offset:match.end() + exclude_window]
for excl_match in exclude_re.finditer(locality):
if (excl_match.start() + offset) <= match.start() and \
(excl_match.end() + offset) >= match.end():
skip = True
break
if skip:
continue
passed.append((match.start(), match.group(0)))
if (delta := len(matches) - len(passed)) > 0:
print(f"Ignored {delta} confusing symbol{'s' if delta > 1 else ''} "
f"in {path.relative_to(reldir)}",
file=sys.stderr)
if passed:
print(f"Found {len(passed)} confusing symbol{'s' if len(passed) > 1 else ''} "
f"in {path.relative_to(reldir)}",
file=sys.stderr)
for start, group in passed:
print(f"Offset {start}: {group}", file=sys.stderr)
return False
return True


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--include", default="[]")
parser.add_argument("--exclude", default="[]")
parser.add_argument("--include-without-comments", default="{}")
parser.add_argument("--exclude-patterns", default="{}")
args = parser.parse_args()
return (
json.loads(args.include),
json.loads(args.exclude),
json.loads(args.include_without_comments),
json.loads(args.exclude_patterns),
)


def main():
include, exclude, include_without_comments = parse_args()
include, exclude, exclude_patterns = parse_args()
cwd = Path(os.getenv("GITHUB_WORKSPACE", "."))
include = set(chain.from_iterable(cwd.glob(os.path.relpath(s, cwd)) for s in include))
exclude = frozenset(chain.from_iterable(cwd.glob(os.path.relpath(s, cwd)) for s in exclude))
exclude = set(chain.from_iterable(cwd.glob(os.path.relpath(s, cwd)) for s in exclude))
include -= exclude
include_without_comments_refined = {}
for k, v in include_without_comments.items():
paths = frozenset(cwd.glob(os.path.relpath(k, cwd))) - exclude
include_without_comments_refined[paths] = v
include_without_comments = include_without_comments_refined
exclude_patterns = {frozenset(cwd.glob(os.path.relpath(k, cwd))): v
for k, v in exclude_patterns.items()}
include = sorted(include)
regexp = compile_regex()
success = True
scanned = len(include)
for file in sorted(include):
success &= scan_file(file, "", regexp)
for files, comment in include_without_comments.items():
scanned += len(files)
for file in sorted(files):
success &= scan_file(file, comment, regexp)
exclude_patterns_in_file = []
for k, v in exclude_patterns.items():
if file in k:
exclude_patterns_in_file.extend(v)
success &= scan_file(file, regexp, exclude_patterns_in_file, str(cwd))
print(f"Scanned {scanned} files.", file=sys.stderr)
return int(not success)

Expand Down

0 comments on commit e33a431

Please sign in to comment.