File size: 2,696 Bytes
912d2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load the example manifests into a running SCQC server and upload local assets.

  python scripts/make_demo_project.py --server http://127.0.0.1:8000 --token admin-dev
"""
from __future__ import annotations

import argparse
import json
import mimetypes
import sys
import urllib.error
import urllib.request
import uuid
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
EXAMPLES = ROOT / "examples"


def call(method: str, url: str, token: str, body: bytes | None = None, ctype: str = "application/json"):
    req = urllib.request.Request(url, data=body, method=method,
                                 headers={"X-SCQC-Token": token, "Content-Type": ctype})
    try:
        with urllib.request.urlopen(req, timeout=60) as r:
            return r.status, json.loads(r.read() or b"{}")
    except urllib.error.HTTPError as e:
        return e.code, json.loads(e.read() or b"{}")


def upload_file(url: str, token: str, path: Path):
    boundary = uuid.uuid4().hex
    mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
    body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{path.name}\"\r\n"
            f"Content-Type: {mime}\r\n\r\n").encode() + path.read_bytes() + f"\r\n--{boundary}--\r\n".encode()
    return call("PUT", url, token, body, f"multipart/form-data; boundary={boundary}")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--server", default="http://127.0.0.1:8000")
    ap.add_argument("--token", default="admin-dev")
    ap.add_argument("manifests", nargs="*", default=[str(p) for p in sorted(EXAMPLES.glob("manifest_*.json"))])
    args = ap.parse_args()

    ok = True
    for mpath in map(Path, args.manifests):
        data = json.loads(mpath.read_text(encoding="utf-8"))
        status, res = call("POST", f"{args.server}/api/projects?replace=true", args.token,
                           json.dumps(data, ensure_ascii=False).encode())
        if status >= 300:
            ok = False
            print(f"✗ {mpath.name}: HTTP {status}")
            for e in res.get("errors", [res]):
                print("   ", e)
            continue
        print(f"✓ {data['project_id']}: {res['n_tasks']} tasks")
        for aid, asset in data.get("assets", {}).items():
            local = (mpath.parent / asset["url"]) if not asset["url"].startswith("http") else None
            if local and local.exists():
                s, r = upload_file(f"{args.server}/api/projects/{data['project_id']}/assets/{aid}", args.token, local)
                print(f"   asset {aid}: {'uploaded' if s < 300 else f'HTTP {s} {r}'}")
    sys.exit(0 if ok else 1)


if __name__ == "__main__":
    main()