@@ -158,20 +158,42 @@ def process_single_file(file_path: str, save_der: bool = True) -> None:
158158 logging .error (f"Error processing { file_path } : { e } " )
159159
160160
161- def process_directory (directory_path : str , output_json : str ) -> None :
162- """Process all .bin files in a directory and create a JSON mapping."""
161+ def process_directory (directory_path : str , output_json : str , sort_output : bool = False ) -> None :
162+ """Process all .bin files in a directory and create/update a JSON mapping.
163+
164+ Args:
165+ directory_path: Path to directory containing .bin files
166+ output_json: Output JSON file path
167+ sort_output: If True, sort entries alphabetically by KEKUpdate path
168+ If False (default), use incremental mode - only add new entries
169+ """
163170 directory = Path (directory_path )
164171
165172 if not directory .is_dir ():
166173 raise ValueError (f"'{ directory_path } ' is not a valid directory" )
167174
168- results = {}
175+ # Load existing map if it exists (for incremental mode)
176+ existing_map = {}
177+ existing_keys = set ()
178+ if Path (output_json ).exists () and not sort_output :
179+ try :
180+ with open (output_json , "r" , encoding = "utf-8" ) as f :
181+ existing_map = json .load (f )
182+ existing_keys = set (existing_map .keys ())
183+ logging .info (f"Loaded existing map with { len (existing_map )} entries" )
184+ except Exception as e :
185+ logging .warning (f"Failed to load existing map: { e } . Starting fresh." )
186+
187+ results = existing_map .copy () if not sort_output else {}
169188
170189 # Find all .bin files recursively
171190 bin_files = list (directory .rglob ("*.bin" ))
172191 logging .info (f"Found { len (bin_files )} .bin files to process" )
173192
174193 bin_files .sort ()
194+ processed_count = 0
195+ skipped_count = 0
196+
175197 for bin_file in bin_files :
176198 try :
177199 certificate , sha1_thumb , _ = process_auth_file (str (bin_file ))
@@ -182,6 +204,12 @@ def process_directory(directory_path: str, output_json: str) -> None:
182204 # Use SHA1 thumbprint as key (matching kek_update_map.json format)
183205 key = sha1_thumb .hex ()
184206
207+ # In incremental mode, skip if already in map
208+ if not sort_output and key in existing_keys :
209+ logging .debug (f"Skipped (already exists): { relative_path } " )
210+ skipped_count += 1
211+ continue
212+
185213 cert_info = get_certificate_info (certificate )
186214
187215 results [key ] = {
@@ -190,6 +218,7 @@ def process_directory(directory_path: str, output_json: str) -> None:
190218 }
191219
192220 logging .info (f"Processed: { relative_path } " )
221+ processed_count += 1
193222
194223 except Exception as e :
195224 try :
@@ -199,12 +228,25 @@ def process_directory(directory_path: str, output_json: str) -> None:
199228 logging .warning (f"Failed to process { bin_file } : { e } " )
200229 continue
201230
231+ # Sort by KEKUpdate path if requested
232+ if sort_output :
233+ sorted_results = {}
234+ for key in sorted (results .keys (),
235+ key = lambda k : results [k ]['KEKUpdate' ]):
236+ sorted_results [key ] = results [key ]
237+ results = sorted_results
238+ logging .info ("Sorted results alphabetically by KEKUpdate path" )
239+
202240 # Write results to JSON file with Unicode characters preserved
203241 with open (output_json , "w" , encoding = "utf-8" ) as f :
204242 json .dump (results , f , indent = 4 , ensure_ascii = False )
205243
206244 logging .info (f"JSON mapping saved to '{ output_json } '" )
207- logging .info (f"Successfully processed { len (results )} files" )
245+ if sort_output :
246+ logging .info (f"Successfully processed { len (results )} files (sort mode)" )
247+ else :
248+ logging .info (f"Successfully processed { processed_count } new files, skipped { skipped_count } existing" )
249+
208250
209251
210252def main () -> None :
@@ -228,6 +270,10 @@ def main() -> None:
228270 parser .add_argument ("path" , nargs = "?" , help = "Path to the authenticated file" )
229271 parser .add_argument ("--directory" , "-d" , help = "Process all .bin files in directory" )
230272 parser .add_argument ("--output" , "-o" , help = "Output JSON file for directory mode" , default = None )
273+ parser .add_argument ("--sort" , "-s" , action = "store_true" ,
274+ help = "Sort entries alphabetically by KEKUpdate path. "
275+ "By default (without this flag), uses incremental mode which "
276+ "only adds new entries and preserves existing order." )
231277 args = parser .parse_args ()
232278
233279 if args .directory :
@@ -237,7 +283,7 @@ def main() -> None:
237283 args .output = os .path .join (args .directory , "kek_update_map.json" )
238284
239285 try :
240- process_directory (args .directory , args .output )
286+ process_directory (args .directory , args .output , args . sort )
241287 except Exception as e :
242288 logging .error (f"Error: { e } " )
243289 elif args .path :
0 commit comments