@@ -59,6 +59,44 @@ def _load_pointers_from_file(pointers_file: str) -> list[str]:
5959 return [line .strip () for line in content .splitlines () if line .strip ()]
6060
6161
62+ def _build_and_write_result (
63+ pointers_to_delete ,
64+ ods_code ,
65+ matched_pointers ,
66+ mismatched_pointers ,
67+ not_found_pointers ,
68+ pointers_deleted ,
69+ failed_deletes ,
70+ start_time ,
71+ end_time ,
72+ output_file_path ,
73+ ):
74+ result = {
75+ "pointers_to_delete" : len (pointers_to_delete ),
76+ "ods_code" : ods_code ,
77+ "ods_code_matched" : {"count" : len (matched_pointers ), "ids" : matched_pointers },
78+ "ods_code_mismatched" : {
79+ "count" : len (mismatched_pointers ),
80+ "ids" : mismatched_pointers ,
81+ },
82+ "pointer_not_found" : {
83+ "count" : len (not_found_pointers ),
84+ "ids" : not_found_pointers ,
85+ },
86+ "deleted_pointers" : {"count" : len (pointers_deleted ), "ids" : pointers_deleted },
87+ "failed_deletes" : {"count" : len (failed_deletes ), "ids" : failed_deletes },
88+ "deletes-took-secs" : timedelta .total_seconds (end_time - start_time ),
89+ }
90+ try :
91+ _write_result_file (result , output_file_path )
92+ except Exception as exc :
93+ result ["_output_error" ] = (
94+ f"Failed to write result file { output_file_path } : { exc } "
95+ )
96+ _print_summary (result )
97+ return result
98+
99+
62100def _write_result_file (result : Dict [str , Any ], output_file : str ) -> None :
63101 """
64102 Atomically write result dict to output_file as JSON.
@@ -237,25 +275,20 @@ def _delete_pointers_by_id(
237275 script_dir , f"delete_results_{ ods_code } _{ stamp } .json"
238276 )
239277
240- if len (pointers_to_delete ) == 0 :
241- result = {
242- "pointers_to_delete" : 0 ,
243- "ods_code" : ods_code ,
244- "ods_code_matched" : {"count" : 0 , "ids" : []},
245- "ods_code_mismatched" : {"count" : 0 , "ids" : []},
246- "pointer_not_found" : {"count" : 0 , "ids" : []},
247- "deleted_pointers" : {"count" : 0 , "ids" : []},
248- "failed_deletes" : {"count" : 0 , "ids" : []},
249- "deletes-took-secs" : 0 ,
250- }
251- try :
252- _write_result_file (result , output_file_path )
253- except Exception as exc :
254- result ["_output_error" ] = (
255- f"Failed to write result file { output_file_path } : { exc } "
256- )
257- _print_summary (result )
258- return result
278+ if not pointers_to_delete :
279+ end_time = datetime .now (tz = timezone .utc )
280+ return _build_and_write_result (
281+ pointers_to_delete ,
282+ ods_code ,
283+ [],
284+ [],
285+ [],
286+ [],
287+ [],
288+ start_time ,
289+ end_time ,
290+ output_file_path ,
291+ )
259292
260293 print (
261294 f"Validating { len (pointers_to_delete )} pointers against ODS code { ods_code } ..."
@@ -268,33 +301,21 @@ def _delete_pointers_by_id(
268301 f"Validate pointer's ODS code: { len (matched_pointers )} matched, { len (mismatched_pointers )} mismatched"
269302 )
270303
271- if len ( matched_pointers ) == 0 :
304+ if not matched_pointers :
272305 print (f"None of the pointer IDs are a match for ODS code { ods_code } . Exiting." )
273306 end_time = datetime .now (tz = timezone .utc )
274- result = {
275- "pointers_to_delete" : len (pointers_to_delete ),
276- "ods_code" : ods_code ,
277- "ods_code_matched" : {
278- "count" : len (matched_pointers ),
279- "ids" : matched_pointers ,
280- },
281- "ods_code_mismatched" : {
282- "count" : len (mismatched_pointers ),
283- "ids" : mismatched_pointers ,
284- },
285- "pointer_not_found" : {"count" : 0 , "ids" : []},
286- "deleted_pointers" : {"count" : 0 , "ids" : []},
287- "failed_deletes" : {"count" : 0 , "ids" : []},
288- "deletes-took-secs" : timedelta .total_seconds (end_time - start_time ),
289- }
290- try :
291- _write_result_file (result , output_file_path )
292- except Exception as exc :
293- result ["_output_error" ] = (
294- f"Failed to write result file { output_file_path } : { exc } "
295- )
296- _print_summary (result )
297- return result
307+ return _build_and_write_result (
308+ pointers_to_delete ,
309+ ods_code ,
310+ matched_pointers ,
311+ mismatched_pointers ,
312+ [],
313+ [],
314+ [],
315+ start_time ,
316+ end_time ,
317+ output_file_path ,
318+ )
298319
299320 print (f"Checking existence of { len (matched_pointers )} pointers in { table_name } ..." )
300321 existing_pointers , not_found_pointers = _batch_get_existing_pointers (
@@ -305,69 +326,40 @@ def _delete_pointers_by_id(
305326 f"Found { len (existing_pointers )} existing pointers to delete, { len (not_found_pointers )} not found."
306327 )
307328
308- if len ( existing_pointers ) == 0 :
329+ if not existing_pointers :
309330 print ("No pointers found to delete. Exiting." )
310331 end_time = datetime .now (tz = timezone .utc )
311- result = {
312- "pointers_to_delete" : len (pointers_to_delete ),
313- "ods_code" : ods_code ,
314- "ods_code_matched" : {
315- "count" : len (matched_pointers ),
316- "ids" : matched_pointers ,
317- },
318- "ods_code_mismatched" : {
319- "count" : len (mismatched_pointers ),
320- "ids" : mismatched_pointers ,
321- },
322- "pointer_not_found" : {
323- "count" : len (not_found_pointers ),
324- "ids" : not_found_pointers ,
325- },
326- "deleted_pointers" : {"count" : 0 , "ids" : []},
327- "failed_deletes" : {"count" : 0 , "ids" : []},
328- "deletes-took-secs" : timedelta .total_seconds (end_time - start_time ),
329- }
330- try :
331- _write_result_file (result , output_file_path )
332- except Exception as exc :
333- result ["_output_error" ] = (
334- f"Failed to write result file { output_file_path } : { exc } "
335- )
336- _print_summary (result )
337- return result
332+ return _build_and_write_result (
333+ pointers_to_delete ,
334+ ods_code ,
335+ matched_pointers ,
336+ mismatched_pointers ,
337+ not_found_pointers ,
338+ [],
339+ [],
340+ start_time ,
341+ end_time ,
342+ output_file_path ,
343+ )
338344
339345 # Proceed with deletion using BatchWriteItem
340346 pointers_deleted , failed_deletes = _batch_delete_pointers (
341347 table_name , existing_pointers
342348 )
343349
344350 end_time = datetime .now (tz = timezone .utc )
345-
346- result = {
347- "pointers_to_delete" : len (pointers_to_delete ),
348- "ods_code" : ods_code ,
349- "ods_code_matched" : {"count" : len (matched_pointers ), "ids" : matched_pointers },
350- "ods_code_mismatched" : {
351- "count" : len (mismatched_pointers ),
352- "ids" : mismatched_pointers ,
353- },
354- "pointer_not_found" : {
355- "count" : len (not_found_pointers ),
356- "ids" : not_found_pointers ,
357- },
358- "deleted_pointers" : {"count" : len (pointers_deleted ), "ids" : pointers_deleted },
359- "failed_deletes" : {"count" : len (failed_deletes ), "ids" : failed_deletes },
360- "deletes-took-secs" : timedelta .total_seconds (end_time - start_time ),
361- }
362-
363- try :
364- _write_result_file (result , output_file_path )
365- except Exception as exc :
366- result ["_output_error" ] = (
367- f"Failed to write result file { output_file_path } : { exc } "
368- )
369-
370- _print_summary (result )
351+ result = _build_and_write_result (
352+ pointers_to_delete ,
353+ ods_code ,
354+ matched_pointers ,
355+ mismatched_pointers ,
356+ not_found_pointers ,
357+ pointers_deleted ,
358+ failed_deletes ,
359+ start_time ,
360+ end_time ,
361+ output_file_path ,
362+ )
371363 print (" Done" )
372364 return result
373365
0 commit comments