-
Notifications
You must be signed in to change notification settings - Fork 48
/
binary_entrypoint.py
85 lines (60 loc) · 2.1 KB
/
binary_entrypoint.py
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
import multiprocessing
import os
import sys
from itertools import cycle
from pathlib import Path
from shutil import get_terminal_size
from threading import Thread
from time import perf_counter, sleep
from typing import Any
import certifi
from colorama.ansitowin32 import StreamWrapper
SCRIPT_ROOT = Path(__file__).parent
def init():
multiprocessing.freeze_support()
start_time = perf_counter()
fix_ssl_certificate_errors_on_macos()
fix_corelib_path(SCRIPT_ROOT)
main = import_protostar_main()
main(SCRIPT_ROOT, start_time)
def fix_corelib_path(script_root: Path):
# FIXME: this is a temporary fix for corelib detection, bindings need to consume this value explicitly
os.environ["CARGO_MANIFEST_DIR"] = str(script_root / "cairo" / "corelib")
def fix_ssl_certificate_errors_on_macos():
os.environ["SSL_CERT_FILE"] = certifi.where()
def import_protostar_main():
with ProtostarInitializingIndicator(disabled=not is_terminal()):
# pylint: disable="import-outside-toplevel"
from protostar import main
return main
def is_terminal() -> bool:
return StreamWrapper(sys.stdout, sys.stdin).isatty()
class ProtostarInitializingIndicator:
def __init__(self, disabled: bool):
self._thread = Thread(target=self._animate, daemon=True)
self.steps = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
self.interval = 1 / len(self.steps)
self.done = False
self._disabled = disabled
def start(self):
if self._disabled:
return
self._thread.start()
def _animate(self):
for step in cycle(self.steps):
if self.done:
break
print(f"\r{step}", flush=True, end="")
sleep(self.interval)
def stop(self):
if self._disabled:
return
self.done = True
cols = get_terminal_size((80, 20)).columns
print("\r" + " " * cols, end="\r", flush=True)
def __enter__(self):
self.start()
def __exit__(self, *args: Any, **kwargs: Any):
self.stop()
if __name__ == "__main__":
init()