Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/tools/check_deprecations.py
4013 views
1
#!/usr/bin/env python3
2
# /// script
3
# requires-python = ">=3.11"
4
# dependencies = [
5
# "pygithub",
6
# "tqdm",
7
# ]
8
# ///
9
# pyright: strict
10
11
import argparse
12
import os
13
import re
14
from datetime import datetime, timedelta, timezone
15
from pathlib import Path
16
17
import tqdm
18
from github.MainClass import Github
19
20
# Regex pattern to find deprecation instances
21
DEPRECATION_PATTERN = re.compile(r'deprecation\((\d+),')
22
23
24
def get_pr_closed_date(github_token: str, pr_number: int) -> datetime:
25
g = Github(github_token)
26
repo = g.get_repo("sagemath/sage")
27
issue = repo.get_issue(number=pr_number)
28
return issue.closed_at
29
30
31
def search_deprecations(path: str) -> set[tuple[str, int]]:
32
deprecations: set[tuple[str, int]] = set()
33
for filepath in Path(path).rglob('*.py*'):
34
try:
35
with filepath.open('r') as f:
36
content = f.read()
37
matches = DEPRECATION_PATTERN.findall(content)
38
for match in matches:
39
deprecations.add((str(filepath), int(match)))
40
except (PermissionError, UnicodeDecodeError):
41
pass
42
print(f"Found {len(deprecations)} deprecations.")
43
return deprecations
44
45
46
def main():
47
# Get source directory from command line arguments
48
parser = argparse.ArgumentParser()
49
parser.add_argument(
50
"sourcedir", help="Source directory", nargs="?", default=".", type=Path
51
)
52
parser.add_argument(
53
"--token", help="GitHub API token", default=os.getenv('GITHUB_TOKEN'), type=str
54
)
55
options = parser.parse_args()
56
57
deprecations = search_deprecations(options.sourcedir)
58
59
one_year_ago = datetime.now(timezone.utc) - timedelta(days=365 + 90)
60
old_deprecations: set[tuple[str, int, datetime]] = set()
61
for filepath, pr_number in tqdm.tqdm(deprecations):
62
closed_date = get_pr_closed_date(options.token, pr_number)
63
if closed_date and closed_date < one_year_ago:
64
old_deprecations.add((filepath, pr_number, closed_date))
65
66
if old_deprecations:
67
print("Deprecations over one year ago:")
68
for filepath, pr_number, closed_date in old_deprecations:
69
print(
70
f"File: {filepath} ; PR: https://github.com/sagemath/sage/pull/{pr_number} ; Closed Date: {closed_date:%Y-%m-%d}"
71
)
72
else:
73
print("No deprecations over one year ago found.")
74
75
76
if __name__ == '__main__':
77
main()
78
79