Skip to content

Commit 0095941

Browse files
authored
ruff format (#5845)
1 parent 6b65345 commit 0095941

File tree

2 files changed

+85
-26
lines changed

2 files changed

+85
-26
lines changed

scripts/find_eq.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
# --cpython: Path to cpython source code
44
# --print-diff: Print the diff between the files
55
# --color: Output color
6-
# --files: Optional globbing pattern to match files in cpython source code
6+
# --files: Optional globbing pattern to match files in cpython source code
77
# --checklist: output as checklist
88

99
import argparse
1010
import difflib
1111
import pathlib
1212

13-
parser = argparse.ArgumentParser(description="Find equivalent files in cpython and rustpython")
14-
parser.add_argument("--cpython", type=pathlib.Path, required=True, help="Path to cpython source code")
15-
parser.add_argument("--print-diff", action="store_true", help="Print the diff between the files")
13+
parser = argparse.ArgumentParser(
14+
description="Find equivalent files in cpython and rustpython"
15+
)
16+
parser.add_argument(
17+
"--cpython", type=pathlib.Path, required=True, help="Path to cpython source code"
18+
)
19+
parser.add_argument(
20+
"--print-diff", action="store_true", help="Print the diff between the files"
21+
)
1622
parser.add_argument("--color", action="store_true", help="Output color")
17-
parser.add_argument("--files", type=str, default="*.py", help="Optional globbing pattern to match files in cpython source code")
23+
parser.add_argument(
24+
"--files",
25+
type=str,
26+
default="*.py",
27+
help="Optional globbing pattern to match files in cpython source code",
28+
)
1829

1930
args = parser.parse_args()
2031

@@ -27,7 +38,9 @@
2738

2839
cpython_lib = args.cpython / "Lib"
2940
rustpython_lib = pathlib.Path(__file__).parent.parent / "Lib"
30-
assert rustpython_lib.exists(), "RustPython lib directory does not exist, ensure the find_eq.py script is located in the right place"
41+
assert rustpython_lib.exists(), (
42+
"RustPython lib directory does not exist, ensure the find_eq.py script is located in the right place"
43+
)
3144

3245
# walk through the cpython lib directory
3346
cpython_files = []
@@ -48,7 +61,13 @@
4861
with open(rustpython_lib / path, "r") as rustpython_file:
4962
rustpython_code = rustpython_file.read()
5063
# compare the files
51-
diff = difflib.unified_diff(cpython_code.splitlines(), rustpython_code.splitlines(), lineterm="", fromfile=str(path), tofile=str(path))
64+
diff = difflib.unified_diff(
65+
cpython_code.splitlines(),
66+
rustpython_code.splitlines(),
67+
lineterm="",
68+
fromfile=str(path),
69+
tofile=str(path),
70+
)
5271
# print the diff if there are differences
5372
diff = list(diff)
5473
if len(diff) > 0:

scripts/generate_checklist.py

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@
1313
import requests
1414
from jinja2 import Environment, FileSystemLoader
1515

16-
parser = argparse.ArgumentParser(description="Find equivalent files in cpython and rustpython")
17-
parser.add_argument("--cpython", type=pathlib.Path, required=True, help="Path to cpython source code")
18-
parser.add_argument("--notes", type=pathlib.Path, required=False, help="Path to notes file")
16+
parser = argparse.ArgumentParser(
17+
description="Find equivalent files in cpython and rustpython"
18+
)
19+
parser.add_argument(
20+
"--cpython", type=pathlib.Path, required=True, help="Path to cpython source code"
21+
)
22+
parser.add_argument(
23+
"--notes", type=pathlib.Path, required=False, help="Path to notes file"
24+
)
1925

2026
args = parser.parse_args()
2127

28+
2229
def check_pr(pr_id: str) -> bool:
2330
if pr_id.startswith("#"):
2431
pr_id = pr_id[1:]
@@ -27,11 +34,13 @@ def check_pr(pr_id: str) -> bool:
2734
response = requests.get(req).json()
2835
return response["merged_at"] is not None
2936

37+
3038
@dataclasses.dataclass
3139
class LibUpdate:
3240
pr: Optional[str] = None
3341
done: bool = True
3442

43+
3544
def parse_updated_lib_issue(issue_body: str) -> dict[str, LibUpdate]:
3645
lines = issue_body.splitlines()
3746
updated_libs = {}
@@ -47,12 +56,14 @@ def parse_updated_lib_issue(issue_body: str) -> dict[str, LibUpdate]:
4756
updated_libs[out[0]] = LibUpdate(out[1], check_pr(out[1]))
4857
return updated_libs
4958

59+
5060
def get_updated_libs() -> dict[str, LibUpdate]:
5161
issue_id = "5736"
5262
req = f"https://api.github.com/repos/RustPython/RustPython/issues/{issue_id}"
5363
response = requests.get(req).json()
5464
return parse_updated_lib_issue(response["body"])
5565

66+
5667
updated_libs = get_updated_libs()
5768

5869
if not args.cpython.exists():
@@ -86,12 +97,11 @@ def get_updated_libs() -> dict[str, LibUpdate]:
8697

8798
cpython_lib = args.cpython / "Lib"
8899
rustpython_lib = pathlib.Path(__file__).parent.parent / "Lib"
89-
assert rustpython_lib.exists(), "RustPython lib directory does not exist, ensure the find_eq.py script is located in the right place"
100+
assert rustpython_lib.exists(), (
101+
"RustPython lib directory does not exist, ensure the find_eq.py script is located in the right place"
102+
)
90103

91-
ignored_objs = [
92-
"__pycache__",
93-
"test"
94-
]
104+
ignored_objs = ["__pycache__", "test"]
95105
# loop through the top-level directories in the cpython lib directory
96106
libs = []
97107
for path in cpython_lib.iterdir():
@@ -105,15 +115,25 @@ def get_updated_libs() -> dict[str, LibUpdate]:
105115
tests = []
106116
cpython_lib_test = cpython_lib / "test"
107117
for path in cpython_lib_test.iterdir():
108-
if path.is_dir() and path.name not in ignored_objs and path.name.startswith("test_"):
118+
if (
119+
path.is_dir()
120+
and path.name not in ignored_objs
121+
and path.name.startswith("test_")
122+
):
109123
# add the directory name to the list of libraries
110124
tests.append(path.name)
111-
elif path.is_file() and path.name.endswith(".py") and path.name not in ignored_objs and path.name.startswith("test_"):
125+
elif (
126+
path.is_file()
127+
and path.name.endswith(".py")
128+
and path.name not in ignored_objs
129+
and path.name.startswith("test_")
130+
):
112131
# add the file name to the list of libraries
113132
file_name = path.name.replace("test_", "")
114133
if file_name not in libs and file_name.replace(".py", "") not in libs:
115134
tests.append(path.name)
116135

136+
117137
def check_diff(file1, file2):
118138
try:
119139
with open(file1, "r") as f1, open(file2, "r") as f2:
@@ -125,12 +145,14 @@ def check_diff(file1, file2):
125145
except UnicodeDecodeError:
126146
return False
127147

148+
128149
def check_completion_pr(display_name):
129150
for lib in updated_libs:
130151
if lib == str(display_name):
131152
return updated_libs[lib].done, updated_libs[lib].pr
132153
return False, None
133154

155+
134156
def check_test_completion(rustpython_path, cpython_path):
135157
if rustpython_path.exists() and rustpython_path.is_file():
136158
if cpython_path.exists() and cpython_path.is_file():
@@ -141,18 +163,22 @@ def check_test_completion(rustpython_path, cpython_path):
141163
return True
142164
return False
143165

166+
144167
def check_lib_completion(rustpython_path, cpython_path):
145168
test_name = "test_" + rustpython_path.name
146169
rustpython_test_path = rustpython_lib / "test" / test_name
147170
cpython_test_path = cpython_lib / "test" / test_name
148-
if cpython_test_path.exists() and not check_test_completion(rustpython_test_path, cpython_test_path):
171+
if cpython_test_path.exists() and not check_test_completion(
172+
rustpython_test_path, cpython_test_path
173+
):
149174
return False
150175
if rustpython_path.exists() and rustpython_path.is_file():
151176
if check_diff(rustpython_path, cpython_path) > 0:
152177
return False
153178
return True
154179
return False
155180

181+
156182
def handle_notes(display_path) -> list[str]:
157183
if str(display_path) in notes:
158184
res = notes[str(display_path)]
@@ -161,13 +187,15 @@ def handle_notes(display_path) -> list[str]:
161187
return res
162188
return []
163189

190+
164191
@dataclasses.dataclass
165192
class Output:
166193
name: str
167194
pr: Optional[str]
168195
completed: Optional[bool]
169196
notes: list[str]
170197

198+
171199
update_libs_output = []
172200
add_libs_output = []
173201
for path in libs:
@@ -183,12 +211,18 @@ class Output:
183211
# check if the file exists in the rustpython lib directory
184212
if rustpython_path.exists() and rustpython_path.is_file():
185213
completed = check_lib_completion(rustpython_path, cpython_path)
186-
update_libs_output.append(Output(str(display_path), pr, completed, handle_notes(display_path)))
214+
update_libs_output.append(
215+
Output(str(display_path), pr, completed, handle_notes(display_path))
216+
)
187217
else:
188218
if pr is not None and completed:
189-
update_libs_output.append(Output(str(display_path), pr, None, handle_notes(display_path)))
219+
update_libs_output.append(
220+
Output(str(display_path), pr, None, handle_notes(display_path))
221+
)
190222
else:
191-
add_libs_output.append(Output(str(display_path), pr, None, handle_notes(display_path)))
223+
add_libs_output.append(
224+
Output(str(display_path), pr, None, handle_notes(display_path))
225+
)
192226

193227
update_tests_output = []
194228
add_tests_output = []
@@ -205,24 +239,30 @@ class Output:
205239
# check if the file exists in the rustpython lib directory
206240
if rustpython_path.exists() and rustpython_path.is_file():
207241
completed = check_lib_completion(rustpython_path, cpython_path)
208-
update_tests_output.append(Output(str(display_path), pr, completed, handle_notes(display_path)))
242+
update_tests_output.append(
243+
Output(str(display_path), pr, completed, handle_notes(display_path))
244+
)
209245
else:
210246
if pr is not None and completed:
211-
update_tests_output.append(Output(str(display_path), pr, None, handle_notes(display_path)))
247+
update_tests_output.append(
248+
Output(str(display_path), pr, None, handle_notes(display_path))
249+
)
212250
else:
213-
add_tests_output.append(Output(str(display_path), pr, None, handle_notes(display_path)))
251+
add_tests_output.append(
252+
Output(str(display_path), pr, None, handle_notes(display_path))
253+
)
214254

215255
for note in notes:
216256
# add a warning for each note that is not attached to a file
217257
for n in notes[note]:
218258
warnings.warn(f"Unattached Note: {note} - {n}")
219259

220-
env = Environment(loader=FileSystemLoader('.'))
260+
env = Environment(loader=FileSystemLoader("."))
221261
template = env.get_template("checklist_template.md")
222262
output = template.render(
223263
update_libs=update_libs_output,
224264
add_libs=add_libs_output,
225265
update_tests=update_tests_output,
226-
add_tests=add_tests_output
266+
add_tests=add_tests_output,
227267
)
228268
print(output)

0 commit comments

Comments
 (0)