forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ecosystem.py
executable file
·203 lines (155 loc) · 5.39 KB
/
check_ecosystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""Check two versions of ruff against a corpus of open-source code.
Example usage:
scripts/check_ecosystem.py <path/to/ruff1> <path/to/ruff2>
"""
# ruff: noqa: T201
import argparse
import asyncio
import difflib
import heapq
import re
import tempfile
from asyncio.subprocess import PIPE, create_subprocess_exec
from contextlib import asynccontextmanager
from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple, Self
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterator, Sequence
class Repository(NamedTuple):
"""A GitHub repository at a specific ref."""
org: str
repo: str
ref: str
@asynccontextmanager
async def clone(self: Self) -> "AsyncIterator[Path]":
"""Shallow clone this repository to a temporary directory."""
with tempfile.TemporaryDirectory() as tmpdir:
process = await create_subprocess_exec(
"git",
"clone",
"--config",
"advice.detachedHead=false",
"--quiet",
"--depth",
"1",
"--no-tags",
"--branch",
self.ref,
f"https://github.com/{self.org}/{self.repo}",
tmpdir,
)
await process.wait()
yield Path(tmpdir)
REPOSITORIES = {
"zulip": Repository("zulip", "zulip", "main"),
"bokeh": Repository("bokeh", "bokeh", "branch-3.2"),
}
SUMMARY_LINE_RE = re.compile(r"^Found \d+ error.*$")
class RuffError(Exception):
"""An error reported by ruff."""
async def check(*, ruff: Path, path: Path) -> "Sequence[str]":
"""Run the given ruff binary against the specified path."""
proc = await create_subprocess_exec(
ruff.absolute(),
"check",
"--no-cache",
"--exit-zero",
".",
stdout=PIPE,
stderr=PIPE,
cwd=path,
)
result, err = await proc.communicate()
if proc.returncode != 0:
raise RuffError(err.decode("utf8"))
lines = [
line
for line in result.decode("utf8").splitlines()
if not SUMMARY_LINE_RE.match(line)
]
return sorted(lines)
class Diff(NamedTuple):
"""A diff between two runs of ruff."""
removed: set[str]
added: set[str]
def __bool__(self: Self) -> bool:
"""Return true if this diff is non-empty."""
return bool(self.removed or self.added)
def __iter__(self: Self) -> "Iterator[str]":
"""Iterate through the changed lines in diff format."""
for line in heapq.merge(self.removed, self.added):
if line in self.removed:
yield f"- {line}"
else:
yield f"+ {line}"
async def compare(ruff1: Path, ruff2: Path, repo: Repository) -> Diff | None:
"""Check a specific repository against two versions of ruff."""
removed, added = set(), set()
async with repo.clone() as path:
try:
async with asyncio.TaskGroup() as tg:
check1 = tg.create_task(check(ruff=ruff1, path=path))
check2 = tg.create_task(check(ruff=ruff2, path=path))
except ExceptionGroup as e:
raise e.exceptions[0] from e
for line in difflib.ndiff(check1.result(), check2.result()):
if line.startswith("- "):
removed.add(line[2:])
elif line.startswith("+ "):
added.add(line[2:])
return Diff(removed, added)
async def main(*, ruff1: Path, ruff2: Path) -> None:
"""Check two versions of ruff against a corpus of open-source code."""
results = await asyncio.gather(
*[compare(ruff1, ruff2, repo) for repo in REPOSITORIES.values()],
return_exceptions=True,
)
diffs = { name: result for name, result in zip(REPOSITORIES, results, strict=True) }
total_removed = total_added = 0
errors = 0
for diff in diffs.values():
if isinstance(diff, Exception):
errors += 1
else:
total_removed += len(diff.removed)
total_added += len(diff.added)
if total_removed == 0 and total_added == 0 and errors == 0:
print("\u2705 ecosystem check detected no changes.")
else:
changes = f"(+{total_added}, -{total_removed}, {errors} error(s))"
print(f"\u2139\ufe0f ecosystem check **detected changes**. {changes}")
print()
for name, diff in diffs.items():
print(f"<details><summary>{name}</summary>")
print("<p>")
print()
if isinstance(diff, Exception):
print("```")
print(str(diff))
print("```")
elif diff:
diff_str = "\n".join(diff)
print("```diff")
print(diff_str)
print("```")
else:
print("_No changes detected_.")
print()
print("</p>")
print("</details>")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Check two versions of ruff against a corpus of open-source code.",
epilog="scripts/check_ecosystem.py <path/to/ruff1> <path/to/ruff2>",
)
parser.add_argument(
"ruff1",
type=Path,
)
parser.add_argument(
"ruff2",
type=Path,
)
args = parser.parse_args()
asyncio.run(main(ruff1=args.ruff1, ruff2=args.ruff2))