Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/tools/check_deprecations.py
7363 views
1
#!/usr/bin/env python3
2
# /// script
3
# requires-python = ">=3.12"
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|deprecated_function_alias)\((\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
if isinstance(match, tuple):
40
match = match[1]
41
deprecations.add((str(filepath), int(match)))
42
except (PermissionError, UnicodeDecodeError):
43
pass
44
print(f"Found {len(deprecations)} deprecations.")
45
return deprecations
46
47
48
def main():
49
# Get source directory from command line arguments
50
parser = argparse.ArgumentParser()
51
parser.add_argument(
52
"sourcedir", help="Source directory", nargs="?", default=".", type=Path
53
)
54
parser.add_argument(
55
"--token", help="GitHub API token", default=os.getenv('GITHUB_TOKEN'), type=str
56
)
57
parser.add_argument(
58
"--verbose", help="increase verbosity", action="store_true"
59
)
60
options = parser.parse_args()
61
62
deprecations = search_deprecations(options.sourcedir)
63
if options.verbose:
64
for filepath, pr_number in deprecations:
65
print(
66
f"File: {filepath} ; PR: https://github.com/sagemath/sage/pull/{pr_number}"
67
)
68
69
one_year_ago = datetime.now(timezone.utc) - timedelta(days=365 + 90)
70
old_deprecations: set[tuple[str, int, datetime]] = set()
71
for filepath, pr_number in tqdm.tqdm(deprecations):
72
closed_date = get_pr_closed_date(options.token, pr_number)
73
if closed_date and closed_date < one_year_ago:
74
if options.verbose:
75
print(
76
f"File: {filepath} ; PR: https://github.com/sagemath/sage/pull/{pr_number} ; Closed Date: {closed_date:%Y-%m-%d}"
77
)
78
old_deprecations.add((filepath, pr_number, closed_date))
79
80
if old_deprecations:
81
print("Deprecations over one year ago:")
82
for filepath, pr_number, closed_date in old_deprecations:
83
print(
84
f"File: {filepath} ; PR: https://github.com/sagemath/sage/pull/{pr_number} ; Closed Date: {closed_date:%Y-%m-%d}"
85
)
86
else:
87
print("No deprecations over one year ago found.")
88
89
90
if __name__ == '__main__':
91
main()
92
93