persona

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

FormatterWinget.py (1999B)


      1 import json
      2 import os
      3 from datetime import datetime
      4 
      5 # --- Configuration ---
      6 INPUT_FILE = "winget.json"
      7 REMOVE_LIST_FILE = "wingetRemoveList.json"
      8 OUTPUT_DIR = "winget"
      9 # ---------------------
     10 
     11 # Ensure the output directory exists
     12 if not os.path.exists(OUTPUT_DIR):
     13     os.makedirs(OUTPUT_DIR)
     14     print(f"Created directory: {OUTPUT_DIR}")
     15 
     16 # Read the main winget file
     17 try:
     18     with open(INPUT_FILE, "r", encoding="utf-8") as fs:
     19         wingetJson = json.load(fs)
     20 except FileNotFoundError:
     21     print(f"Error: Input file not found at '{INPUT_FILE}'")
     22     exit(1)
     23 except json.JSONDecodeError:
     24     print(f"Error: Could not decode JSON from '{INPUT_FILE}'")
     25     exit(1)
     26 
     27 # Read the remove list file
     28 try:
     29     with open(REMOVE_LIST_FILE, "r", encoding="utf-8") as fl:
     30         wingetRemoveListJson = json.load(fl)
     31 except FileNotFoundError:
     32     print(f"Error: Remove list file not found at '{REMOVE_LIST_FILE}'")
     33     exit(1)
     34 except json.JSONDecodeError:
     35     print(f"Error: Could not decode JSON from '{REMOVE_LIST_FILE}'")
     36     exit(1)
     37 
     38 remove_identifiers = set(wingetRemoveListJson.get("wingetRemoveList", []))
     39 original_packages = wingetJson.get("Sources", [{}])[0].get("Packages", [])
     40 packages_to_keep = []
     41 removed_count = 0
     42 
     43 for package in original_packages:
     44     if package.get("PackageIdentifier") in remove_identifiers:
     45         print(f"Removing: {package.get('PackageIdentifier')}")
     46         removed_count += 1
     47     else:
     48         packages_to_keep.append(package)
     49 
     50 if removed_count == 0:
     51     print("No packages were removed from the list.")
     52 
     53 wingetJson["Sources"][0]["Packages"] = packages_to_keep
     54 
     55 # Generate the output filename with the current date
     56 today_str = datetime.now().strftime("%Y%m%d")
     57 output_filename = os.path.join(OUTPUT_DIR, f"winget-{today_str}.json")
     58 
     59 # Write the modified data to the new file
     60 with open(output_filename, "w", encoding="utf-8") as f:
     61     json.dump(wingetJson, f, indent=4, ensure_ascii=False)
     62 
     63 print(f"\nSuccessfully created formatted winget file at: {output_filename}")