Add a multi-language duplicate finder that writes .metrics/dry.edn.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""Read source files and pair the forms that clear the similarity threshold."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from dryer.discover import language_of
|
||||
from dryer.extract import entries_in_source
|
||||
from dryer.model import Duplicate, Entry, Span
|
||||
from dryer.shape import jaccard
|
||||
|
||||
|
||||
def display_path(path: Path, root: Path) -> str:
|
||||
resolved = path.resolve()
|
||||
try:
|
||||
return resolved.relative_to(root.resolve()).as_posix()
|
||||
except ValueError:
|
||||
return resolved.as_posix()
|
||||
|
||||
|
||||
def _keep(entry: Entry, min_lines: int, min_nodes: int) -> bool:
|
||||
lines = entry.end_line - entry.start_line + 1
|
||||
return lines >= min_lines and entry.nodes >= min_nodes
|
||||
|
||||
|
||||
def scan_files(
|
||||
files: list[Path], root: Path, min_lines: int, min_nodes: int
|
||||
) -> tuple[list[Entry], list[str]]:
|
||||
entries: list[Entry] = []
|
||||
warnings: list[str] = []
|
||||
for path in files:
|
||||
language = language_of(path)
|
||||
if language is None:
|
||||
continue
|
||||
source = path.read_text(encoding="utf-8", errors="replace")
|
||||
file = display_path(path, root)
|
||||
found, warning = entries_in_source(language, source, path.as_posix(), file)
|
||||
if warning:
|
||||
warnings.append(f"{file}:{warning}")
|
||||
entries.extend(entry for entry in found if _keep(entry, min_lines, min_nodes))
|
||||
return entries, warnings
|
||||
|
||||
|
||||
def find_duplicates(entries: list[Entry], threshold: float) -> list[Duplicate]:
|
||||
"""Pair forms of the same language. Each unordered pair is reported once."""
|
||||
|
||||
by_language: dict[str, list[Entry]] = {}
|
||||
for entry in entries:
|
||||
by_language.setdefault(entry.language, []).append(entry)
|
||||
|
||||
found: list[Duplicate] = []
|
||||
for language, group in by_language.items():
|
||||
for index, left in enumerate(group):
|
||||
for right in group[index + 1 :]:
|
||||
if (
|
||||
left.file == right.file
|
||||
and left.start_line == right.start_line
|
||||
and left.end_line == right.end_line
|
||||
):
|
||||
continue
|
||||
score = jaccard(left.fingerprints, right.fingerprints)
|
||||
if score < threshold:
|
||||
continue
|
||||
found.append(
|
||||
Duplicate(
|
||||
score=score,
|
||||
language=language,
|
||||
left=Span(left.file, left.start_line, left.end_line),
|
||||
right=Span(right.file, right.start_line, right.end_line),
|
||||
left_nodes=left.nodes,
|
||||
right_nodes=right.nodes,
|
||||
)
|
||||
)
|
||||
found.sort(
|
||||
key=lambda item: (
|
||||
-item.score,
|
||||
item.language,
|
||||
item.left.file,
|
||||
item.left.start_line,
|
||||
item.right.file,
|
||||
item.right.start_line,
|
||||
)
|
||||
)
|
||||
return found
|
||||
Reference in New Issue
Block a user