1313import requests
1414from 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
2026args = parser .parse_args ()
2127
28+
2229def 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
3139class LibUpdate :
3240 pr : Optional [str ] = None
3341 done : bool = True
3442
43+
3544def 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+
5060def 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+
5667updated_libs = get_updated_libs ()
5768
5869if not args .cpython .exists ():
@@ -86,12 +97,11 @@ def get_updated_libs() -> dict[str, LibUpdate]:
8697
8798cpython_lib = args .cpython / "Lib"
8899rustpython_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
96106libs = []
97107for path in cpython_lib .iterdir ():
@@ -105,15 +115,25 @@ def get_updated_libs() -> dict[str, LibUpdate]:
105115tests = []
106116cpython_lib_test = cpython_lib / "test"
107117for 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+
117137def 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+
128149def 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+
134156def 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+
144167def 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+
156182def 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
165192class Output :
166193 name : str
167194 pr : Optional [str ]
168195 completed : Optional [bool ]
169196 notes : list [str ]
170197
198+
171199update_libs_output = []
172200add_libs_output = []
173201for 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
193227update_tests_output = []
194228add_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
215255for 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 ("." ))
221261template = env .get_template ("checklist_template.md" )
222262output = 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)
228268print (output )
0 commit comments