|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +from ruamel.yaml import YAML |
| 5 | + |
| 6 | +# Adjust paths |
| 7 | +LANDSCAPE_FILE = 'landscape.yml' |
| 8 | +LOGOS_DIR = 'hosted_logos' |
| 9 | + |
| 10 | +def get_yaml_parser(): |
| 11 | + yaml = YAML() |
| 12 | + yaml.preserve_quotes = True |
| 13 | + yaml.indent(mapping=2, sequence=4, offset=2) |
| 14 | + yaml.width = 4096 |
| 15 | + return yaml |
| 16 | + |
| 17 | +def load_landscape(yaml_parser): |
| 18 | + with open(LANDSCAPE_FILE, 'r', encoding='utf-8') as f: |
| 19 | + return yaml_parser.load(f) |
| 20 | + |
| 21 | +def save_landscape(yaml_parser, data): |
| 22 | + with open(LANDSCAPE_FILE, 'w', encoding='utf-8') as f: |
| 23 | + yaml_parser.dump(data, f) |
| 24 | + |
| 25 | +def normalize_logo_path(path): |
| 26 | + if path and path.startswith('./'): |
| 27 | + return path[2:] |
| 28 | + return path |
| 29 | + |
| 30 | +def process_landscape(data, fix=False): |
| 31 | + referenced_logos = set() |
| 32 | + modified = False |
| 33 | + |
| 34 | + if 'landscape' in data: |
| 35 | + for category in data['landscape']: |
| 36 | + for subcategory in category.get('subcategories', []): |
| 37 | + for item in subcategory.get('items', []): |
| 38 | + logo = item.get('logo') |
| 39 | + if logo: |
| 40 | + normalized = normalize_logo_path(logo) |
| 41 | + if normalized != logo: |
| 42 | + print(f"Normalizing logo: {logo} -> {normalized}") |
| 43 | + if fix: |
| 44 | + item['logo'] = normalized |
| 45 | + modified = True |
| 46 | + referenced_logos.add(normalized) |
| 47 | + else: |
| 48 | + referenced_logos.add(logo) |
| 49 | + return referenced_logos, modified |
| 50 | + |
| 51 | +def get_hosted_logos(): |
| 52 | + if not os.path.exists(LOGOS_DIR): |
| 53 | + print(f"Error: {LOGOS_DIR} dir not found") |
| 54 | + return set() |
| 55 | + return set(os.listdir(LOGOS_DIR)) |
| 56 | + |
| 57 | +def main(): |
| 58 | + parser = argparse.ArgumentParser(description='Cleanup landscape logos') |
| 59 | + parser.add_argument('--fix', action='store_true', help='Apply fixes (delete orphans, update yaml)') |
| 60 | + args = parser.parse_args() |
| 61 | + |
| 62 | + print("Loading landscape...") |
| 63 | + yaml_parser = get_yaml_parser() |
| 64 | + try: |
| 65 | + data = load_landscape(yaml_parser) |
| 66 | + except FileNotFoundError: |
| 67 | + print(f"Error: {LANDSCAPE_FILE} not found. Please run from the repo root.") |
| 68 | + return |
| 69 | + |
| 70 | + referenced_logos, modified = process_landscape(data, fix=args.fix) |
| 71 | + hosted_logos = get_hosted_logos() |
| 72 | + |
| 73 | + print(f"Referenced logos: {len(referenced_logos)}") |
| 74 | + print(f"Hosted logos: {len(hosted_logos)}") |
| 75 | + |
| 76 | + missing_logos = referenced_logos - hosted_logos |
| 77 | + orphaned_logos = hosted_logos - referenced_logos |
| 78 | + |
| 79 | + print(f"Missing logos (referenced but not found): {len(missing_logos)}") |
| 80 | + for logo in missing_logos: |
| 81 | + print(f" MISSING: {logo}") |
| 82 | + |
| 83 | + print(f"Orphaned logos (found but not referenced): {len(orphaned_logos)}") |
| 84 | + |
| 85 | + if args.fix: |
| 86 | + if orphaned_logos: |
| 87 | + print(f"Deleting {len(orphaned_logos)} orphaned logos...") |
| 88 | + for logo in orphaned_logos: |
| 89 | + path = os.path.join(LOGOS_DIR, logo) |
| 90 | + try: |
| 91 | + os.remove(path) |
| 92 | + print(f" Deleted: {logo}") |
| 93 | + except OSError as e: |
| 94 | + print(f" Error deleting {logo}: {e}") |
| 95 | + |
| 96 | + if modified: |
| 97 | + print(f"Saving {LANDSCAPE_FILE}...") |
| 98 | + save_landscape(yaml_parser, data) |
| 99 | + else: |
| 100 | + print("No changes to landscape.yml needed.") |
| 101 | + else: |
| 102 | + if orphaned_logos: |
| 103 | + print("Run with --fix to delete orphaned logos.") |
| 104 | + if modified: # modified will be false if not fix, but conceptually changes are pending |
| 105 | + pass # logic above only modifies if fix is True. |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments