summaryrefslogtreecommitdiffstats
path: root/scripts/oe-go-mod-fetcher-hybrid.py
blob: 9704cca0340ea8de41470ccbf8ef299737b280b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#!/usr/bin/env python3

# SPDX-License-Identifier: GPL-2.0-only
#
# go-dep processor
#
# Copyright (C) 2025 Bruce Ashfield
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
oe-go-mod-fetcher-hybrid.py - Convert go-mod-vcs format to hybrid gomod:// + git:// format.

This script reads existing go-mod-git.inc and go-mod-cache.inc files and converts
them to a hybrid format that uses:
- gomod:// for modules fetched from proxy.golang.org (fast, but no VCS control)
- git:// for modules where you want SRCREV control (auditable, but slower)

Usage:
    # List all modules and their sizes
    oe-go-mod-fetcher-hybrid.py --recipedir ./recipes-containers/k3s --list

    # Show size-based recommendations
    oe-go-mod-fetcher-hybrid.py --recipedir ./recipes-containers/k3s --recommend

    # Convert specific modules to gomod:// (rest stay as git://)
    oe-go-mod-fetcher-hybrid.py --recipedir ./recipes-containers/k3s \\
        --gomod "github.com/spf13,golang.org/x,google.golang.org"

    # Convert specific modules to git:// (rest become gomod://)
    oe-go-mod-fetcher-hybrid.py --recipedir ./recipes-containers/k3s \\
        --git "github.com/containerd,github.com/rancher"

    # Use a config file
    oe-go-mod-fetcher-hybrid.py --recipedir ./recipes-containers/k3s \\
        --config hybrid-config.json
"""

import argparse
import json
import re
import sys
import subprocess
import os
import hashlib
import urllib.request
import urllib.error
import concurrent.futures
from pathlib import Path
from collections import defaultdict
from typing import Optional


# Default configuration - used if data/hybrid-config.json is not found
DEFAULT_CONFIG = {
    "vcs_priority_prefixes": [
        "github.com/containerd",
        "github.com/rancher",
        "github.com/k3s-io",
        "k8s.io",
        "sigs.k8s.io",
    ],
    "size_threshold_bytes": 1048576,  # 1MB
    "default_git_prefixes": [
        "github.com/containerd",
        "k8s.io",
        "sigs.k8s.io",
    ],
}


def load_hybrid_config() -> dict:
    """
    Load hybrid mode configuration from data/hybrid-config.json.

    Falls back to DEFAULT_CONFIG if the file doesn't exist.
    The config file is looked for relative to this script's location.
    """
    script_dir = Path(__file__).parent
    config_path = script_dir / "data" / "hybrid-config.json"

    if config_path.exists():
        try:
            with open(config_path) as f:
                config = json.load(f)
            # Merge with defaults for any missing keys
            for key, value in DEFAULT_CONFIG.items():
                if key not in config:
                    config[key] = value
            return config
        except (json.JSONDecodeError, IOError) as e:
            print(f"Warning: Could not load {config_path}: {e}", file=sys.stderr)
            print("Using default configuration", file=sys.stderr)

    return DEFAULT_CONFIG.copy()


def fetch_gomod_checksum(module: str, version: str) -> Optional[str]:
    """
    Fetch SHA256 checksum for a module from proxy.golang.org.

    The checksum is calculated by downloading the .zip file and hashing it.
    """
    # Escape capital letters in module path (Go proxy convention)
    escaped_module = re.sub(r'([A-Z])', lambda m: '!' + m.group(1).lower(), module)
    escaped_version = re.sub(r'([A-Z])', lambda m: '!' + m.group(1).lower(), version)

    url = f"https://proxy.golang.org/{escaped_module}/@v/{escaped_version}.zip"

    try:
        req = urllib.request.Request(url, headers={'User-Agent': 'oe-go-mod-fetcher-hybrid/1.0'})
        with urllib.request.urlopen(req, timeout=30) as response:
            data = response.read()
            return hashlib.sha256(data).hexdigest()
    except urllib.error.HTTPError as e:
        print(f"    WARNING: Failed to fetch {module}@{version}: HTTP {e.code}", file=sys.stderr)
        return None
    except urllib.error.URLError as e:
        print(f"    WARNING: Failed to fetch {module}@{version}: {e.reason}", file=sys.stderr)
        return None
    except Exception as e:
        print(f"    WARNING: Failed to fetch {module}@{version}: {e}", file=sys.stderr)
        return None


def fetch_checksums_parallel(modules: list[dict], max_workers: int = 8) -> dict[str, str]:
    """
    Fetch checksums for multiple modules in parallel.

    Returns dict mapping "module@version" -> "sha256sum"
    """
    checksums = {}

    def fetch_one(mod):
        key = f"{mod['module']}@{mod['version']}"
        checksum = fetch_gomod_checksum(mod['module'], mod['version'])
        return key, checksum

    print(f"Fetching checksums for {len(modules)} modules from proxy.golang.org...")

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
        futures = {executor.submit(fetch_one, mod): mod for mod in modules}
        completed = 0
        for future in concurrent.futures.as_completed(futures):
            key, checksum = future.result()
            completed += 1
            if checksum:
                checksums[key] = checksum
            # Progress indicator
            if completed % 20 == 0 or completed == len(modules):
                print(f"  Progress: {completed}/{len(modules)} modules")

    return checksums


def parse_go_mod_cache_inc(cache_inc_path: Path) -> list[dict]:
    """Parse GO_MODULE_CACHE_DATA from go-mod-cache.inc."""
    content = cache_inc_path.read_text()

    # Find the JSON array in GO_MODULE_CACHE_DATA
    match = re.search(r"GO_MODULE_CACHE_DATA\s*=\s*'(\[.*\])'", content, re.DOTALL)
    if not match:
        raise ValueError(f"Could not find GO_MODULE_CACHE_DATA in {cache_inc_path}")

    json_str = match.group(1).replace('\\\n', '')
    return json.loads(json_str)


def parse_go_mod_git_inc(git_inc_path: Path) -> dict[str, dict]:
    """Parse SRC_URI entries from go-mod-git.inc to extract commit and repo info."""
    content = git_inc_path.read_text()

    # Map vcs_hash -> {repo, commit, full_entry}
    vcs_to_info = {}

    # Pattern: git://host/path;...;rev=COMMIT;...;destsuffix=vcs_cache/VCS_HASH
    for line in content.split('\n'):
        if not line.startswith('SRC_URI +='):
            continue

        # Extract the git:// URL part
        match = re.search(r'git://([^;]+);([^"]*);destsuffix=vcs_cache/([a-f0-9]+)', line)
        if match:
            repo_path = match.group(1)
            params = match.group(2)
            vcs_hash = match.group(3)

            # Extract rev from params
            rev_match = re.search(r'rev=([a-f0-9]+)', params)
            commit = rev_match.group(1) if rev_match else ''

            vcs_to_info[vcs_hash] = {
                'repo': f"https://{repo_path}",
                'commit': commit,
                'full_line': line.strip()
            }

    return vcs_to_info


def get_repo_sizes(vcs_info: dict, workdir: Optional[Path] = None) -> dict[str, int]:
    """Get sizes of VCS cache directories if they exist."""
    sizes = {}

    if workdir is None:
        return sizes

    # Try common locations for vcs_cache
    for subpath in ['sources/vcs_cache', 'vcs_cache']:
        vcs_cache_dir = workdir / subpath
        if vcs_cache_dir.exists():
            break
    else:
        return sizes

    for vcs_hash in vcs_info.keys():
        cache_path = vcs_cache_dir / vcs_hash
        if cache_path.exists():
            try:
                result = subprocess.run(
                    ['du', '-sb', str(cache_path)],
                    capture_output=True, text=True, timeout=10
                )
                if result.returncode == 0:
                    size = int(result.stdout.split()[0])
                    sizes[vcs_hash] = size
            except (subprocess.TimeoutExpired, ValueError):
                pass

    return sizes


def get_discovery_sizes(modules: list[dict], discovery_cache: Optional[Path] = None) -> dict[str, int]:
    """Get sizes of modules from discovery cache .zip files."""
    sizes = {}

    if discovery_cache is None or not discovery_cache.exists():
        return sizes

    for mod in modules:
        module_path = mod.get('module', '')
        version = mod.get('version', '')
        vcs_hash = mod.get('vcs_hash', '')

        if not module_path or not version or not vcs_hash:
            continue

        # Build path to .zip file: discovery_cache/<module>/@v/<version>.zip
        zip_path = discovery_cache / module_path / '@v' / f'{version}.zip'

        if zip_path.exists():
            try:
                size = zip_path.stat().st_size
                # Accumulate size by vcs_hash (same repo may have multiple modules)
                sizes[vcs_hash] = sizes.get(vcs_hash, 0) + size
            except OSError:
                pass

    return sizes


def format_size(size_bytes: int) -> str:
    """Format bytes as human readable."""
    for unit in ['B', 'KB', 'MB', 'GB']:
        if size_bytes < 1024:
            return f"{size_bytes:.1f} {unit}"
        size_bytes /= 1024
    return f"{size_bytes:.1f} TB"


def list_modules(modules: list[dict], vcs_info: dict, sizes: dict) -> None:
    """List all modules with their info."""
    # Group by module path prefix
    by_prefix = defaultdict(list)
    for mod in modules:
        parts = mod['module'].split('/')
        if len(parts) >= 2:
            prefix = '/'.join(parts[:2])
        else:
            prefix = mod['module']
        by_prefix[prefix].append(mod)

    print(f"\n{'Module':<60} {'Version':<25} {'Size':>12}")
    print("=" * 100)

    total_size = 0
    for prefix in sorted(by_prefix.keys()):
        prefix_size = 0
        for mod in sorted(by_prefix[prefix], key=lambda m: m['module']):
            vcs_hash = mod.get('vcs_hash', '')
            size = sizes.get(vcs_hash, 0)
            prefix_size += size
            total_size += size

            size_str = format_size(size) if size > 0 else '-'
            print(f"  {mod['module']:<58} {mod['version']:<25} {size_str:>12}")

        if len(by_prefix[prefix]) > 1:
            print(f"  {'[subtotal]':<58} {'':<25} {format_size(prefix_size):>12}")
        print()

    print("=" * 100)
    print(f"Total: {len(modules)} modules, {format_size(total_size)}")


def recommend_conversion(modules: list[dict], vcs_info: dict, sizes: dict, recipedir: Path = None) -> None:
    """Recommend modules to convert based on size.

    Configuration is loaded from data/hybrid-config.json if it exists,
    otherwise defaults are used. This allows easy customization of:
    - vcs_priority_prefixes: modules to suggest keeping as git://
    - size_threshold_bytes: threshold for suggesting gomod:// conversion
    - default_git_prefixes: fallback prefixes if no matches found
    """
    # Load configuration from external file (or use defaults)
    config = load_hybrid_config()
    vcs_priority_patterns = config.get('vcs_priority_prefixes', DEFAULT_CONFIG['vcs_priority_prefixes'])
    size_threshold = config.get('size_threshold_bytes', DEFAULT_CONFIG['size_threshold_bytes'])
    default_git_prefixes = config.get('default_git_prefixes', DEFAULT_CONFIG['default_git_prefixes'])

    # Calculate sizes per prefix
    prefix_sizes = defaultdict(lambda: {'size': 0, 'count': 0, 'modules': []})

    for mod in modules:
        parts = mod['module'].split('/')
        if len(parts) >= 2:
            prefix = '/'.join(parts[:2])
        else:
            prefix = mod['module']

        vcs_hash = mod.get('vcs_hash', '')
        size = sizes.get(vcs_hash, 0)

        prefix_sizes[prefix]['size'] += size
        prefix_sizes[prefix]['count'] += 1
        prefix_sizes[prefix]['modules'].append(mod['module'])

    # Sort by size descending
    sorted_prefixes = sorted(prefix_sizes.items(), key=lambda x: x[1]['size'], reverse=True)

    total_size = sum(p['size'] for p in prefix_sizes.values())

    print("\n" + "=" * 80)
    print("GO MODULE HYBRID CONVERSION RECOMMENDATIONS")
    print("=" * 80)

    print(f"\n{'Prefix':<45} {'Count':>8} {'Size':>12} {'% Total':>10}")
    print("-" * 80)

    gomod_candidates = []
    git_candidates = []

    for prefix, info in sorted_prefixes[:25]:  # Top 25
        pct = (info['size'] / total_size * 100) if total_size > 0 else 0

        print(f"{prefix:<45} {info['count']:>8} {format_size(info['size']):>12} {pct:>9.1f}%")

        # Check if this is a VCS priority prefix
        is_vcs_priority = any(prefix.startswith(p) or prefix == p for p in vcs_priority_patterns)

        if is_vcs_priority:
            git_candidates.append(prefix)
        elif info['size'] > size_threshold:
            gomod_candidates.append(prefix)

    print("-" * 80)
    print(f"{'Total':<45} {len(modules):>8} {format_size(total_size):>12}")

    if gomod_candidates:
        print("\n" + "=" * 80)
        print("LARGEST MODULE PREFIXES (top candidates for gomod:// proxy fetch):")
        print("=" * 80)
        print("\n  " + ",".join(gomod_candidates[:10]))

        # Calculate potential savings
        gomod_size = sum(prefix_sizes[p]['size'] for p in gomod_candidates)
        if total_size > 0:
            print(f"\n  These account for {format_size(gomod_size)} ({gomod_size/total_size*100:.0f}% of total)")

    print("\n" + "=" * 80)
    print("SUGGESTED --git PREFIXES (keep as git:// for VCS control):")
    print("=" * 80)

    if git_candidates:
        print("\n  " + ",".join(git_candidates))
    else:
        print("\n  (none identified)")

    print("\n  NOTE: With --git, ALL other modules automatically become gomod://")
    print("        (not just the large ones listed above)")

    # Output conversion command
    print("\n" + "=" * 80)
    print("TO CONVERT TO HYBRID FORMAT:")
    print("=" * 80)
    print()

    # Get script path (relative to this script's location)
    script_path = Path(__file__).resolve()

    # Use default_git_prefixes from config as fallback
    fallback_git = ','.join(default_git_prefixes)

    if recipedir:
        print(f"  python3 {script_path} \\")
        print(f"    --recipedir {recipedir} \\")
        if git_candidates:
            print(f"    --git \"{','.join(git_candidates)}\"")
        else:
            print(f"    --git \"{fallback_git}\"")
    else:
        print(f"  python3 {script_path} \\")
        print(f"    --recipedir <your-recipe-directory> \\")
        if git_candidates:
            print(f"    --git \"{','.join(git_candidates)}\"")
        else:
            print(f"    --git \"{fallback_git}\"")


def fetch_gomod_checksum(module: str, version: str) -> Optional[str]:
    """Fetch SHA256 checksum for a module from proxy.golang.org."""
    import urllib.request
    import hashlib

    # Escape module path (uppercase letters)
    escaped = re.sub(r'([A-Z])', lambda m: '!' + m.group(1).lower(), module)

    url = f"https://proxy.golang.org/{escaped}/@v/{version}.zip"

    try:
        with urllib.request.urlopen(url, timeout=30) as response:
            content = response.read()
            return hashlib.sha256(content).hexdigest()
    except Exception as e:
        print(f"  Warning: Could not fetch checksum for {module}@{version}: {e}", file=sys.stderr)
        return None


def generate_hybrid_files(
    modules: list[dict],
    vcs_info: dict,
    git_prefixes: list[str],
    gomod_prefixes: list[str],
    output_dir: Path,
    fetch_checksums: bool = False
) -> None:
    """Generate hybrid include files."""

    # Ensure output directory exists
    output_dir.mkdir(parents=True, exist_ok=True)

    git_modules = []
    gomod_modules = []

    # Classify modules
    for mod in modules:
        mod_path = mod['module']

        # Check if explicitly marked as git://
        is_git = any(mod_path.startswith(prefix) for prefix in git_prefixes)

        # Check if explicitly marked as gomod://
        is_gomod = any(mod_path.startswith(prefix) for prefix in gomod_prefixes)

        if is_git and is_gomod:
            print(f"Warning: {mod_path} matches both git and gomod prefixes, using git://",
                  file=sys.stderr)
            is_gomod = False

        # Default: if git_prefixes specified, everything else is gomod
        # If gomod_prefixes specified, everything else is git
        if git_prefixes and not is_git and not is_gomod:
            is_gomod = True
        elif gomod_prefixes and not is_git and not is_gomod:
            is_git = True
        elif not git_prefixes and not gomod_prefixes:
            # No prefixes specified - default to gomod for all
            is_gomod = True

        if is_gomod:
            gomod_modules.append(mod)
        else:
            git_modules.append(mod)

    print(f"\nClassification:")
    print(f"  gomod:// (proxy): {len(gomod_modules)} modules")
    print(f"  git:// (VCS):     {len(git_modules)} modules")

    # Fetch checksums in parallel (always, unless --no-checksums)
    checksum_map = {}
    if fetch_checksums and gomod_modules:
        checksum_map = fetch_checksums_parallel(gomod_modules)
        if len(checksum_map) < len(gomod_modules):
            missing = len(gomod_modules) - len(checksum_map)
            print(f"  WARNING: Failed to fetch {missing} checksums", file=sys.stderr)

    # Generate gomod include file
    gomod_lines = [
        "# Generated by oe-go-mod-fetcher-hybrid.py",
        "# Go modules fetched from proxy.golang.org (fast path)",
        "#",
        "# These modules are fetched as pre-built zip files from the Go proxy.",
        "# They do not provide VCS commit-level provenance but are much faster.",
        "",
        "inherit go-mod",
        ""
    ]

    for mod in sorted(gomod_modules, key=lambda m: m['module']):
        key = f"{mod['module']}@{mod['version']}"
        if key in checksum_map:
            # Include checksum inline to avoid BitBake variable flag name issues
            # (e.g., ~ character in git.sr.ht/~sbinet/gg causes parse errors)
            gomod_lines.append(f'SRC_URI += "gomod://{mod["module"]};version={mod["version"]};sha256sum={checksum_map[key]}"')
        else:
            gomod_lines.append(f'SRC_URI += "gomod://{mod["module"]};version={mod["version"]}"')

    gomod_file = output_dir / 'go-mod-hybrid-gomod.inc'
    gomod_file.write_text('\n'.join(gomod_lines) + '\n')
    print(f"\nWrote {gomod_file}")

    if not fetch_checksums and gomod_modules:
        print(f"  WARNING: Checksums not fetched (use default or --fetch-checksums)")
        print(f"           BitBake will fail on first fetch and show required checksums")

    # Generate git include file
    git_lines = [
        "# Generated by oe-go-mod-fetcher-hybrid.py",
        "# Go modules fetched from git repositories (VCS path)",
        "#",
        "# These modules are fetched directly from their git repositories.",
        "# They provide full VCS provenance and allow easy SRCREV bumping.",
        ""
    ]

    # Track added vcs_hashes to avoid duplicates when multiple modules
    # share the same git repo/commit (e.g., errdefs and errdefs/pkg)
    added_vcs_hashes = set()
    for mod in sorted(git_modules, key=lambda m: m['module']):
        vcs_hash = mod.get('vcs_hash', '')
        if vcs_hash in vcs_info and vcs_hash not in added_vcs_hashes:
            git_lines.append(vcs_info[vcs_hash]['full_line'])
            added_vcs_hashes.add(vcs_hash)

    git_file = output_dir / 'go-mod-hybrid-git.inc'
    git_file.write_text('\n'.join(git_lines) + '\n')
    print(f"Wrote {git_file}")

    # Generate cache metadata file for git modules
    cache_lines = [
        "# Generated by oe-go-mod-fetcher-hybrid.py",
        "# Metadata for git-fetched modules (VCS path)",
        "# Used by go-mod-vcs.bbclass to build module cache from git checkouts",
        "",
        "inherit go-mod-vcs",
        "",
    ]

    # Format GO_MODULE_CACHE_DATA with one entry per line for readability
    # (matches go-mod-cache.inc format: '[\
    # {entry1},\
    # {entry2}]')
    cache_lines.append("# Module metadata for cache building (one module per line)")
    if git_modules:
        cache_lines.append("GO_MODULE_CACHE_DATA = '[\\")
        for i, mod in enumerate(sorted(git_modules, key=lambda m: m['module'])):
            entry = json.dumps(mod, separators=(',', ':'))  # Compact single-line JSON per entry
            if i < len(git_modules) - 1:
                cache_lines.append(f"{entry},\\")
            else:
                cache_lines.append(f"{entry}]'")
    else:
        cache_lines.append("GO_MODULE_CACHE_DATA = '[]'")

    cache_file = output_dir / 'go-mod-hybrid-cache.inc'
    cache_file.write_text('\n'.join(cache_lines) + '\n')
    print(f"Wrote {cache_file}")

    # Print usage instructions
    print("\n" + "=" * 70)
    print("NEXT STEPS:")
    print("=" * 70)
    print("""
1. Update your recipe to enable mode switching:

   # GO_MOD_FETCH_MODE: "vcs" (all git://) or "hybrid" (gomod:// + git://)
   GO_MOD_FETCH_MODE ?= "vcs"

   # VCS mode: all modules via git://
   include ${@ "go-mod-git.inc" if d.getVar("GO_MOD_FETCH_MODE") == "vcs" else ""}
   include ${@ "go-mod-cache.inc" if d.getVar("GO_MOD_FETCH_MODE") == "vcs" else ""}

   # Hybrid mode: gomod:// for most, git:// for selected
   include ${@ "go-mod-hybrid-gomod.inc" if d.getVar("GO_MOD_FETCH_MODE") == "hybrid" else ""}
   include ${@ "go-mod-hybrid-git.inc" if d.getVar("GO_MOD_FETCH_MODE") == "hybrid" else ""}
   include ${@ "go-mod-hybrid-cache.inc" if d.getVar("GO_MOD_FETCH_MODE") == "hybrid" else ""}

2. Run bitbake once in hybrid mode to fetch gomod:// checksums:

   GO_MOD_FETCH_MODE = "hybrid"   # in local.conf
   bitbake <recipe>

3. Copy the checksums from the error log into go-mod-hybrid-gomod.inc

4. Build again - or switch back to VCS mode anytime:

   GO_MOD_FETCH_MODE = "vcs"      # full VCS provenance
   GO_MOD_FETCH_MODE = "hybrid"   # faster proxy fetch
""")


def main():
    parser = argparse.ArgumentParser(
        description='Convert go-mod-vcs format to hybrid gomod:// + git:// format',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__
    )

    parser.add_argument('--recipedir', type=Path, required=True,
                        help='Recipe directory containing go-mod-git.inc and go-mod-cache.inc')

    parser.add_argument('--workdir', type=Path, default=None,
                        help='BitBake workdir containing vcs_cache (for size calculations)')

    parser.add_argument('--discovery-cache', type=Path, default=None,
                        help='Discovery cache directory containing module .zip files (for size calculations)')

    # Actions
    parser.add_argument('--list', action='store_true',
                        help='List all modules with sizes')

    parser.add_argument('--recommend', action='store_true',
                        help='Show size-based recommendations for conversion')

    # Conversion options
    parser.add_argument('--git', type=str, default='',
                        help='Comma-separated module prefixes to keep as git:// (rest become gomod://)')

    parser.add_argument('--gomod', type=str, default='',
                        help='Comma-separated module prefixes to convert to gomod:// (rest stay git://)')

    parser.add_argument('--config', type=Path, default=None,
                        help='JSON config file with git/gomod prefix lists')

    parser.add_argument('--no-checksums', action='store_true',
                        help='Skip fetching SHA256 checksums (not recommended)')

    parser.add_argument('--output-dir', type=Path, default=None,
                        help='Output directory for hybrid files (default: recipedir)')

    args = parser.parse_args()

    # Validate inputs
    cache_inc = args.recipedir / 'go-mod-cache.inc'
    git_inc = args.recipedir / 'go-mod-git.inc'

    if not cache_inc.exists():
        print(f"Error: {cache_inc} not found", file=sys.stderr)
        sys.exit(1)

    if not git_inc.exists():
        print(f"Error: {git_inc} not found", file=sys.stderr)
        sys.exit(1)

    # Parse existing files
    print(f"Loading {cache_inc}...")
    modules = parse_go_mod_cache_inc(cache_inc)
    print(f"  Found {len(modules)} modules")

    print(f"Loading {git_inc}...")
    vcs_info = parse_go_mod_git_inc(git_inc)
    print(f"  Found {len(vcs_info)} VCS entries")

    # Get sizes from discovery cache and/or workdir
    sizes = {}
    if args.discovery_cache:
        print(f"Calculating sizes from discovery cache {args.discovery_cache}...")
        sizes = get_discovery_sizes(modules, args.discovery_cache)
        print(f"  Got sizes for {len(sizes)} modules from discovery cache")

    if args.workdir:
        print(f"Calculating sizes from {args.workdir}...")
        vcs_sizes = get_repo_sizes(vcs_info, args.workdir)
        print(f"  Got sizes for {len(vcs_sizes)} repos from vcs_cache")
        # Merge vcs_sizes into sizes (vcs_cache sizes override discovery if both exist)
        for k, v in vcs_sizes.items():
            sizes[k] = v

    # Handle actions
    if args.list:
        list_modules(modules, vcs_info, sizes)
        return

    if args.recommend:
        recommend_conversion(modules, vcs_info, sizes, args.recipedir)
        return

    # Handle conversion
    git_prefixes = [p.strip() for p in args.git.split(',') if p.strip()]
    gomod_prefixes = [p.strip() for p in args.gomod.split(',') if p.strip()]

    if args.config:
        if args.config.exists():
            config = json.loads(args.config.read_text())
            git_prefixes.extend(config.get('git', []))
            gomod_prefixes.extend(config.get('gomod', []))
        else:
            print(f"Error: Config file {args.config} not found", file=sys.stderr)
            sys.exit(1)

    if not git_prefixes and not gomod_prefixes:
        print("Error: Specify --git, --gomod, --list, or --recommend", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    output_dir = args.output_dir or args.recipedir

    generate_hybrid_files(
        modules=modules,
        vcs_info=vcs_info,
        git_prefixes=git_prefixes,
        gomod_prefixes=gomod_prefixes,
        output_dir=output_dir,
        fetch_checksums=not args.no_checksums  # Default: fetch checksums
    )


if __name__ == '__main__':
    main()