forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ecosystem.py
executable file
·549 lines (469 loc) · 18.3 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env python3
"""
**DEPRECATED** This script is being replaced by the ruff-ecosystem package.
Check two versions of ruff against a corpus of open-source code.
Example usage:
scripts/check_ecosystem.py <path/to/ruff1> <path/to/ruff2>
"""
from __future__ import annotations
import argparse
import asyncio
import difflib
import heapq
import json
import logging
import re
import tempfile
import time
from asyncio.subprocess import PIPE, create_subprocess_exec
from contextlib import asynccontextmanager, nullcontext
from pathlib import Path
from signal import SIGINT, SIGTERM
from typing import TYPE_CHECKING, NamedTuple, Self, TypeVar
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterator, Sequence
logger = logging.getLogger(__name__)
class Repository(NamedTuple):
"""A GitHub repository at a specific ref."""
org: str
repo: str
ref: str | None
select: str = ""
ignore: str = ""
exclude: str = ""
# Generating fixes is slow and verbose
show_fixes: bool = False
@asynccontextmanager
async def clone(self: Self, checkout_dir: Path) -> AsyncIterator[Path]:
"""Shallow clone this repository to a temporary directory."""
if checkout_dir.exists():
logger.debug(f"Reusing {self.org}:{self.repo}")
yield await self._get_commit(checkout_dir)
return
logger.debug(f"Cloning {self.org}:{self.repo}")
git_clone_command = [
"git",
"clone",
"--config",
"advice.detachedHead=false",
"--quiet",
"--depth",
"1",
"--no-tags",
]
if self.ref:
git_clone_command.extend(["--branch", self.ref])
git_clone_command.extend(
[
f"https://github.com/{self.org}/{self.repo}",
checkout_dir,
],
)
git_clone_process = await create_subprocess_exec(
*git_clone_command,
env={"GIT_TERMINAL_PROMPT": "0"},
)
status_code = await git_clone_process.wait()
logger.debug(
f"Finished cloning {self.org}/{self.repo} with status {status_code}",
)
yield await self._get_commit(checkout_dir)
def url_for(self: Self, commit_sha: str, path: str, lnum: int | None = None) -> str:
"""
Return the GitHub URL for the given commit, path, and line number, if given.
"""
# Default to main branch
url = f"https://github.com/{self.org}/{self.repo}/blob/{commit_sha}/{path}"
if lnum:
url += f"#L{lnum}"
return url
async def _get_commit(self: Self, checkout_dir: Path) -> str:
"""Return the commit sha for the repository in the checkout directory."""
git_sha_process = await create_subprocess_exec(
*["git", "rev-parse", "HEAD"],
cwd=checkout_dir,
stdout=PIPE,
)
git_sha_stdout, _ = await git_sha_process.communicate()
assert (
await git_sha_process.wait() == 0
), f"Failed to retrieve commit sha at {checkout_dir}"
return git_sha_stdout.decode().strip()
# Repositories to check
# We check most repositories with the default ruleset instead of all rules to avoid
# noisy reports when new rules are added; see https://github.com/astral-sh/ruff/pull/3590
REPOSITORIES: list[Repository] = [
Repository("DisnakeDev", "disnake", "master"),
Repository("PostHog", "HouseWatch", "main"),
Repository("RasaHQ", "rasa", "main"),
Repository("Snowflake-Labs", "snowcli", "main"),
Repository("aiven", "aiven-client", "main"),
Repository("alteryx", "featuretools", "main"),
Repository("apache", "airflow", "main", select="ALL"),
Repository("apache", "superset", "master", select="ALL"),
Repository("aws", "aws-sam-cli", "develop"),
Repository("binary-husky", "gpt_academic", "master"),
Repository("bloomberg", "pytest-memray", "main"),
Repository("bokeh", "bokeh", "branch-3.3", select="ALL"),
# Disabled due to use of explicit `select` with `E999`, which is no longer
# supported in `--preview`.
# See: https://github.com/astral-sh/ruff/pull/12129
# Repository("demisto", "content", "master"),
Repository("docker", "docker-py", "main"),
Repository("facebookresearch", "chameleon", "main"),
Repository("freedomofpress", "securedrop", "develop"),
Repository("fronzbot", "blinkpy", "dev"),
Repository("ibis-project", "ibis", "master"),
Repository("ing-bank", "probatus", "main"),
Repository("jrnl-org", "jrnl", "develop"),
Repository("langchain-ai", "langchain", "main"),
Repository("latchbio", "latch", "main"),
Repository("lnbits", "lnbits", "main"),
Repository("milvus-io", "pymilvus", "master"),
Repository("mlflow", "mlflow", "master"),
Repository("model-bakers", "model_bakery", "main"),
Repository("pandas-dev", "pandas", "main"),
Repository("prefecthq", "prefect", "main"),
Repository("pypa", "build", "main"),
Repository("pypa", "cibuildwheel", "main"),
Repository("pypa", "pip", "main"),
Repository("pypa", "setuptools", "main"),
Repository("python", "mypy", "master"),
Repository("python", "typeshed", "main", select="PYI"),
Repository("python-poetry", "poetry", "master"),
Repository("qdrant", "qdrant-client", "master"),
Repository("reflex-dev", "reflex", "main"),
Repository("rotki", "rotki", "develop"),
Repository("scikit-build", "scikit-build", "main"),
Repository("scikit-build", "scikit-build-core", "main"),
Repository("sphinx-doc", "sphinx", "master"),
Repository("spruceid", "siwe-py", "main"),
Repository("tiangolo", "fastapi", "master"),
Repository("yandex", "ch-backup", "main"),
Repository("zulip", "zulip", "main", select="ALL"),
]
SUMMARY_LINE_RE = re.compile(r"^(Found \d+ error.*)|(.*potentially fixable with.*)$")
class RuffError(Exception):
"""An error reported by ruff."""
async def check(
*,
ruff: Path,
path: Path,
name: str,
select: str = "",
ignore: str = "",
exclude: str = "",
show_fixes: bool = False,
) -> Sequence[str]:
"""Run the given ruff binary against the specified path."""
logger.debug(f"Checking {name} with {ruff}")
ruff_args = ["check", "--no-cache", "--exit-zero"]
if select:
ruff_args.extend(["--select", select])
if ignore:
ruff_args.extend(["--ignore", ignore])
if exclude:
ruff_args.extend(["--exclude", exclude])
if show_fixes:
ruff_args.extend(["--show-fixes"])
start = time.time()
proc = await create_subprocess_exec(
ruff.absolute(),
*ruff_args,
".",
stdout=PIPE,
stderr=PIPE,
cwd=path,
)
result, err = await proc.communicate()
end = time.time()
logger.debug(f"Finished checking {name} with {ruff} in {end - start:.2f}")
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]
source_sha: 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(sorted(self.removed), sorted(self.added)):
if line in self.removed:
yield f"- {line}"
else:
yield f"+ {line}"
async def compare(
ruff1: Path,
ruff2: Path,
repo: Repository,
checkouts: Path | None = None,
) -> Diff | None:
"""Check a specific repository against two versions of ruff."""
removed, added = set(), set()
# By the default, the git clone are transient, but if the user provides a
# directory for permanent storage we keep it there
if checkouts:
location_context = nullcontext(checkouts)
else:
location_context = tempfile.TemporaryDirectory()
with location_context as checkout_parent:
assert ":" not in repo.org
assert ":" not in repo.repo
checkout_dir = Path(checkout_parent).joinpath(f"{repo.org}:{repo.repo}")
async with repo.clone(checkout_dir) as checkout_sha:
try:
async with asyncio.TaskGroup() as tg:
check1 = tg.create_task(
check(
ruff=ruff1,
path=checkout_dir,
name=f"{repo.org}/{repo.repo}",
select=repo.select,
ignore=repo.ignore,
exclude=repo.exclude,
show_fixes=repo.show_fixes,
),
)
check2 = tg.create_task(
check(
ruff=ruff2,
path=checkout_dir,
name=f"{repo.org}/{repo.repo}",
select=repo.select,
ignore=repo.ignore,
exclude=repo.exclude,
show_fixes=repo.show_fixes,
),
)
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, checkout_sha)
def read_projects_jsonl(projects_jsonl: Path) -> dict[tuple[str, str], Repository]:
"""Read either of the two formats of https://github.com/akx/ruff-usage-aggregate."""
repositories = {}
for line in projects_jsonl.read_text().splitlines():
data = json.loads(line)
# Check the input format.
if "items" in data:
for item in data["items"]:
# Pick only the easier case for now.
if item["path"] != "pyproject.toml":
continue
repository = item["repository"]
assert re.fullmatch(r"[a-zA-Z0-9_.-]+", repository["name"]), repository[
"name"
]
# GitHub doesn't give us any branch or pure rev info. This would give
# us the revision, but there's no way with git to just do
# `git clone --depth 1` with a specific ref.
# `ref = item["url"].split("?ref=")[1]` would be exact
repositories[(repository["owner"], repository["repo"])] = Repository(
repository["owner"]["login"],
repository["name"],
None,
select=repository.get("select"),
ignore=repository.get("ignore"),
exclude=repository.get("exclude"),
)
else:
assert "owner" in data, "Unknown ruff-usage-aggregate format"
# Pick only the easier case for now.
if data["path"] != "pyproject.toml":
continue
repositories[(data["owner"], data["repo"])] = Repository(
data["owner"],
data["repo"],
data.get("ref"),
select=data.get("select"),
ignore=data.get("ignore"),
exclude=data.get("exclude"),
)
return repositories
DIFF_LINE_RE = re.compile(
r"^(?P<pre>[+-]) (?P<inner>(?P<path>[^:]+):(?P<lnum>\d+):\d+:) (?P<post>.*)$",
)
T = TypeVar("T")
async def main(
*,
ruff1: Path,
ruff2: Path,
projects_jsonl: Path | None,
checkouts: Path | None = None,
) -> None:
"""Check two versions of ruff against a corpus of open-source code."""
if projects_jsonl:
repositories = read_projects_jsonl(projects_jsonl)
else:
repositories = {(repo.org, repo.repo): repo for repo in REPOSITORIES}
logger.debug(f"Checking {len(repositories)} projects")
# https://stackoverflow.com/a/61478547/3549270
# Otherwise doing 3k repositories can take >8GB RAM
semaphore = asyncio.Semaphore(50)
async def limited_parallelism(coroutine: T) -> T:
async with semaphore:
return await coroutine
results = await asyncio.gather(
*[
limited_parallelism(compare(ruff1, ruff2, repo, checkouts))
for repo in repositories.values()
],
return_exceptions=True,
)
diffs = dict(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:
rule_changes: dict[str, tuple[int, int]] = {}
changes = f"(+{total_added}, -{total_removed}, {errors} error(s))"
print(f"\u2139\ufe0f ecosystem check **detected changes**. {changes}")
print()
for (org, repo), diff in diffs.items():
if isinstance(diff, Exception):
changes = "error"
print(f"<details><summary>{repo} ({changes})</summary>")
repo = repositories[(org, repo)]
print(
f"https://github.com/{repo.org}/{repo.repo} ref {repo.ref} "
f"select {repo.select} ignore {repo.ignore} exclude {repo.exclude}",
)
print("<p>")
print()
print("```")
print(str(diff))
print("```")
print()
print("</p>")
print("</details>")
elif diff:
changes = f"+{len(diff.added)}, -{len(diff.removed)}"
print(f"<details><summary>{repo} ({changes})</summary>")
print("<p>")
print()
repo = repositories[(org, repo)]
diff_lines = list(diff)
print("<pre>")
for line in diff_lines:
match = DIFF_LINE_RE.match(line)
if match is None:
print(line)
continue
pre, inner, path, lnum, post = match.groups()
url = repo.url_for(diff.source_sha, path, int(lnum))
print(f"{pre} <a href='{url}'>{inner}</a> {post}")
print("</pre>")
print()
print("</p>")
print("</details>")
# Count rule changes
for line in diff_lines:
# Find rule change for current line or construction
# + <rule>/<path>:<line>:<column>: <rule_code> <message>
matches = re.search(r": ([A-Z]{1,4}[0-9]{3,4})", line)
if matches is None:
# Handle case where there are no regex matches e.g.
# + "?application=AIRFLOW&authenticator=TEST_AUTH&role=TEST_ROLE&warehouse=TEST_WAREHOUSE" # noqa: E501
# Which was found in local testing
continue
rule_code = matches.group(1)
# Get current additions and removals for this rule
current_changes = rule_changes.get(rule_code, (0, 0))
# Check if addition or removal depending on the first character
if line[0] == "+":
current_changes = (current_changes[0] + 1, current_changes[1])
elif line[0] == "-":
current_changes = (current_changes[0], current_changes[1] + 1)
rule_changes[rule_code] = current_changes
else:
continue
if len(rule_changes.keys()) > 0:
print(f"Rules changed: {len(rule_changes.keys())}")
print()
print("| Rule | Changes | Additions | Removals |")
print("| ---- | ------- | --------- | -------- |")
for rule, (additions, removals) in sorted(
rule_changes.items(),
key=lambda x: (x[1][0] + x[1][1]),
reverse=True,
):
print(f"| {rule} | {additions + removals} | {additions} | {removals} |")
logger.debug(f"Finished {len(repositories)} repositories")
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(
"--projects",
type=Path,
help=(
"Optional JSON files to use over the default repositories. "
"Supports both github_search_*.jsonl and known-github-tomls.jsonl."
),
)
parser.add_argument(
"--checkouts",
type=Path,
help=(
"Location for the git checkouts, in case you want to save them"
" (defaults to temporary directory)"
),
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Activate debug logging",
)
parser.add_argument(
"ruff1",
type=Path,
)
parser.add_argument(
"ruff2",
type=Path,
)
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
if args.checkouts:
args.checkouts.mkdir(exist_ok=True, parents=True)
main_task = asyncio.ensure_future(
main(
ruff1=args.ruff1,
ruff2=args.ruff2,
projects_jsonl=args.projects,
checkouts=args.checkouts,
),
)
# https://stackoverflow.com/a/58840987/3549270
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, main_task.cancel)
try:
loop.run_until_complete(main_task)
finally:
loop.close()