File size: 1,900 Bytes
a964887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import contextlib
import io
import logging
import os
import sys
from urllib.parse import quote

# Silence the HTTP stack before it is imported. It does not print by default —
# urllib3 logs at DEBUG and the root logger sits at WARNING — but any dependency
# that calls logging.basicConfig() at DEBUG or INFO turns that on, and what it
# then emits is the full request URL, which carries the private repository path.
# Space container logs are public on a public Space, so this cannot rest on a
# default staying put.
for _n in ("urllib3", "requests", "urllib3.connectionpool", "charset_normalizer"):
    logging.getLogger(_n).setLevel(logging.CRITICAL)
    logging.getLogger(_n).propagate = False

import requests

try:
    token = os.environ["HF_TOKEN"]
    source = os.environ["AAAS_SOURCE"]
    revision = os.environ.get("AAAS_SOURCE_REVISION", "main")

    url = (
        f"https://huggingface.co/datasets/{quote(source, safe='/')}"
        f"/resolve/{quote(revision, safe='')}/start.py"
    )

    # The request is made with stdout and stderr captured. Nothing the HTTP
    # stack writes is safe to forward to a public log: on a failed lookup it can
    # carry the full request URL, and that URL contains the private repository
    # path. Anything captured here is discarded, not printed.
    _sink_out, _sink_err = io.StringIO(), io.StringIO()
    with contextlib.redirect_stdout(_sink_out), contextlib.redirect_stderr(_sink_err):
        response = requests.get(
            url,
            headers={"Authorization": f"Bearer {token}"},
            timeout=60,
        )
        response.raise_for_status()

    path = "/tmp/.aaas_start.py"
    with open(path, "wb") as f:
        f.write(response.content)

    os.execve(sys.executable, [sys.executable, path], os.environ.copy())

except Exception:
    print("ERROR: application bootstrap unavailable.", flush=True)
    sys.exit(1)