File size: 3,541 Bytes
adf68aa
c786dae
 
adf68aa
 
c786dae
adf68aa
c786dae
adf68aa
c786dae
 
adf68aa
 
c786dae
 
adf68aa
 
 
 
c786dae
adf68aa
c786dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf68aa
 
 
 
 
 
c786dae
adf68aa
c786dae
adf68aa
 
 
c786dae
 
 
adf68aa
 
 
 
 
 
 
 
 
 
 
 
 
c786dae
 
adf68aa
 
 
 
 
 
 
 
 
 
 
 
 
c786dae
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
#!/bin/bash
# vendor.sh: (re)build src/ (legion's World API library) and requirements.txt in this directory
# from a legion checkout. The world's own code (pybot.py and the modules next to it) is never
# touched here.
#
#   ./vendor.sh LEGION_DIR            check + vendor at the commit in source-lock.json;
#                                    fails (nothing written) if any file's sha256 differs
#   ./vendor.sh LEGION_DIR --lock REV re-pin: vendor REV and rewrite source-lock.json
#
# Reads LEGION_DIR with `git archive` only (never checks out or switches branches there).
# Needs git, python3, and uv (requirements.txt is `uv export` of legion's own uv.lock).
set -euo pipefail
die() { echo "vendor.sh: $*" >&2; exit 1; }
[ $# -ge 1 ] || die "usage: vendor.sh LEGION_DIR [--lock REV]"
LEGION=$1
ROOT=$(cd "$(dirname "$0")" && pwd)
LOCK=$ROOT/source-lock.json

# What the world imports, transitively (pybot.py, repository.py, sandbox.py, snapshot.py and
# legion.world.server.serve).
FILES=(
  src/legion/__init__.py
  src/legion/buckets.py
  src/legion/isolation.py
  src/legion/models.py
  src/legion/registry.py
  src/legion/tools/__init__.py
  src/legion/tools/loader.py
  src/legion/worker/__init__.py
  src/legion/worker/sync.py
  src/legion/world/__init__.py
  src/legion/world/admin.py
  src/legion/world/lanes.py
  src/legion/world/protocol.py
  src/legion/world/server.py
  src/legion/world/store.py
  src/legion/world/workspace.py
)

if [ "${2:-}" = --lock ]; then
  [ -n "${3:-}" ] || die "--lock needs a revision"
  REV=$3
else
  REV=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["legion_commit"])' "$LOCK")
fi
COMMIT=$(git -C "$LEGION" rev-parse --verify "$REV^{commit}") || die "$REV is not a commit in $LEGION"

TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT
mkdir -p "$TMP/world" "$TMP/proj"
git -C "$LEGION" archive "$COMMIT" "${FILES[@]}" | tar -x -C "$TMP/world"
git -C "$LEGION" archive "$COMMIT" pyproject.toml uv.lock | tar -x -C "$TMP/proj"
# The world needs legion's core and `controller` deps (fastapi, uvicorn), not Harbor or the board
# client (msgboard is a git dependency, which pip's --require-hashes refuses): pruned.
(cd "$TMP/proj" && uv export -q --frozen --no-emit-project --extra controller --no-dev --no-header \
   --prune harbor --prune podman-compose --prune msgboard -o "$TMP/requirements.txt")

python3 - "$TMP" "$LOCK" "$COMMIT" "${2:-}" <<'PY'
import hashlib, json, sys
from pathlib import Path
tmp, lock, commit, mode = Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3], sys.argv[4]
sha = lambda p: hashlib.sha256(p.read_bytes()).hexdigest()
got = {p.relative_to(tmp / "world").as_posix(): sha(p)
       for p in sorted((tmp / "world").rglob("*")) if p.is_file()}
got = {"files": got, "requirements.txt": sha(tmp / "requirements.txt")}
if mode == "--lock":
    lock.write_text(json.dumps({"legion_repo": "https://github.com/huggingface/legion",
                                "legion_commit": commit, **got}, indent=2) + "\n")
    sys.exit(0)
want = json.loads(lock.read_text())
bad = sorted(k for k in want["files"].keys() | got["files"].keys()
             if want["files"].get(k) != got["files"].get(k))
if want["requirements.txt"] != got["requirements.txt"]:
    bad.append("requirements.txt")
if bad:
    sys.exit("vendor.sh: sha256 mismatch against source-lock.json: " + ", ".join(bad))
PY

rm -rf "$ROOT/src"
mv "$TMP/world/src" "$ROOT/src"
cp "$TMP/requirements.txt" "$ROOT/requirements.txt"
echo "vendored legion $COMMIT: $(find "$ROOT/src" -type f | wc -l) files"