File size: 2,145 Bytes
cf8c8d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check out official upstream code at the commits pinned in upstream.lock.json.

Pinning keeps every training script reproducible: a config written against one upstream commit is
always run against that commit.

    python -m common.upstream --list              # show every pinned repository
    python -m common.upstream PaddleOCR olmocr    # clone (or update) and check out the pins
"""
import argparse
import json
import subprocess
import sys
from pathlib import Path

from .paths import REPO_ROOT, UPSTREAM_DIR


def load_lock():
    lock = json.loads((REPO_ROOT / "upstream.lock.json").read_text(encoding="utf-8"))
    lock.pop("_note", None)
    return lock


def fetch(name: str, dest_root: Path = UPSTREAM_DIR) -> Path:
    lock = load_lock()
    if name not in lock:
        raise SystemExit(f"unknown upstream '{name}'. Known: {', '.join(lock)}")
    entry = lock[name]
    dest = dest_root / name
    if not (dest / ".git").exists():
        dest_root.mkdir(parents=True, exist_ok=True)
        # blob:none keeps large repos (PaddleOCR) small until files are checked out
        subprocess.run(["git", "clone", "--filter=blob:none", "--no-checkout", entry["url"], str(dest)], check=True)
    else:
        subprocess.run(["git", "-C", str(dest), "fetch", "origin"], check=True)
    subprocess.run(["git", "-C", str(dest), "-c", "advice.detachedHead=false", "checkout", entry["commit"]], check=True)
    print(f"{name}: {dest} @ {entry['commit'][:10]} (training code: {entry['has_training_code']})")
    return dest


def main(argv=None):
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("names", nargs="*")
    ap.add_argument("--list", action="store_true")
    ap.add_argument("--dest", type=Path, default=UPSTREAM_DIR)
    a = ap.parse_args(argv)
    lock = load_lock()
    if a.list or not a.names:
        for k, v in lock.items():
            print(f"{k:18} training_code={str(v['has_training_code']):5} {v['url']}@{v['commit'][:10]}")
        return
    for n in a.names:
        fetch(n, a.dest)


if __name__ == "__main__":
    sys.exit(main())