fastled 1.1.45__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastled/__init__.py +128 -0
- fastled/app.py +67 -0
- fastled/assets/example.txt +1 -0
- fastled/cli.py +16 -0
- fastled/client_server.py +351 -0
- fastled/compile_server.py +87 -0
- fastled/compile_server_impl.py +251 -0
- fastled/docker_manager.py +665 -0
- fastled/filewatcher.py +202 -0
- fastled/keyboard.py +113 -0
- fastled/live_client.py +69 -0
- fastled/open_browser.py +59 -0
- fastled/parse_args.py +172 -0
- fastled/paths.py +4 -0
- fastled/project_init.py +76 -0
- fastled/select_sketch_directory.py +35 -0
- fastled/settings.py +9 -0
- fastled/sketch.py +97 -0
- fastled/spinner.py +34 -0
- fastled/string_diff.py +42 -0
- fastled/test/examples.py +31 -0
- fastled/types.py +61 -0
- fastled/util.py +10 -0
- fastled/web_compile.py +285 -0
- fastled-1.1.45.dist-info/LICENSE +21 -0
- fastled-1.1.45.dist-info/METADATA +323 -0
- fastled-1.1.45.dist-info/RECORD +30 -0
- fastled-1.1.45.dist-info/WHEEL +5 -0
- fastled-1.1.45.dist-info/entry_points.txt +4 -0
- fastled-1.1.45.dist-info/top_level.txt +1 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
|
3
|
+
from fastled.string_diff import string_diff_paths
|
4
|
+
|
5
|
+
|
6
|
+
def select_sketch_directory(
|
7
|
+
sketch_directories: list[Path], cwd_is_fastled: bool
|
8
|
+
) -> str | None:
|
9
|
+
if cwd_is_fastled:
|
10
|
+
exclude = ["src", "dev", "tests"]
|
11
|
+
for ex in exclude:
|
12
|
+
p = Path(ex)
|
13
|
+
if p in sketch_directories:
|
14
|
+
sketch_directories.remove(p)
|
15
|
+
|
16
|
+
if len(sketch_directories) == 1:
|
17
|
+
print(f"\nUsing sketch directory: {sketch_directories[0]}")
|
18
|
+
return str(sketch_directories[0])
|
19
|
+
elif len(sketch_directories) > 1:
|
20
|
+
print("\nMultiple Directories found, choose one:")
|
21
|
+
for i, sketch_dir in enumerate(sketch_directories):
|
22
|
+
print(f" [{i+1}]: {sketch_dir}")
|
23
|
+
which = input("\nPlease specify a sketch directory: ").strip()
|
24
|
+
try:
|
25
|
+
index = int(which) - 1
|
26
|
+
return str(sketch_directories[index])
|
27
|
+
except (ValueError, IndexError):
|
28
|
+
inputs = [p for p in sketch_directories]
|
29
|
+
top_hits: list[tuple[float, Path]] = string_diff_paths(which, inputs)
|
30
|
+
if len(top_hits) == 1:
|
31
|
+
example = top_hits[0][1]
|
32
|
+
return str(example)
|
33
|
+
else:
|
34
|
+
return select_sketch_directory([p for _, p in top_hits], cwd_is_fastled)
|
35
|
+
return None
|
fastled/settings.py
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import os
|
2
|
+
import platform
|
3
|
+
|
4
|
+
MACHINE = platform.machine().lower()
|
5
|
+
IS_ARM: bool = "arm" in MACHINE or "aarch64" in MACHINE
|
6
|
+
PLATFORM_TAG: str = "-arm64" if IS_ARM else ""
|
7
|
+
CONTAINER_NAME = f"fastled-wasm-compiler{PLATFORM_TAG}"
|
8
|
+
DEFAULT_URL = str(os.environ.get("FASTLED_URL", "https://fastled.onrender.com"))
|
9
|
+
SERVER_PORT = 9021
|
fastled/sketch.py
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
import os
|
2
|
+
from pathlib import Path
|
3
|
+
|
4
|
+
_MAX_FILES_SEARCH_LIMIT = 10000
|
5
|
+
|
6
|
+
|
7
|
+
def find_sketch_directories(directory: Path) -> list[Path]:
|
8
|
+
file_count = 0
|
9
|
+
sketch_directories: list[Path] = []
|
10
|
+
# search all the paths one level deep
|
11
|
+
for path in directory.iterdir():
|
12
|
+
if path.is_dir():
|
13
|
+
dir_name = path.name
|
14
|
+
if str(dir_name).startswith("."):
|
15
|
+
continue
|
16
|
+
file_count += 1
|
17
|
+
if file_count > _MAX_FILES_SEARCH_LIMIT:
|
18
|
+
print(
|
19
|
+
f"More than {_MAX_FILES_SEARCH_LIMIT} files found. Stopping search."
|
20
|
+
)
|
21
|
+
break
|
22
|
+
|
23
|
+
if looks_like_sketch_directory(path, quick=True):
|
24
|
+
sketch_directories.append(path)
|
25
|
+
if dir_name.lower() == "examples":
|
26
|
+
for example in path.iterdir():
|
27
|
+
if example.is_dir():
|
28
|
+
if looks_like_sketch_directory(example, quick=True):
|
29
|
+
sketch_directories.append(example)
|
30
|
+
# make relative to cwd
|
31
|
+
sketch_directories = [p.relative_to(directory) for p in sketch_directories]
|
32
|
+
return sketch_directories
|
33
|
+
|
34
|
+
|
35
|
+
def get_sketch_files(directory: Path) -> list[Path]:
|
36
|
+
file_count = 0
|
37
|
+
files: list[Path] = []
|
38
|
+
for root, dirs, filenames in os.walk(directory):
|
39
|
+
# ignore hidden directories
|
40
|
+
dirs[:] = [d for d in dirs if not d.startswith(".")]
|
41
|
+
# ignore fastled_js directory
|
42
|
+
dirs[:] = [d for d in dirs if "fastled_js" not in d]
|
43
|
+
# ignore hidden files
|
44
|
+
filenames = [f for f in filenames if not f.startswith(".")]
|
45
|
+
outer_break = False
|
46
|
+
for filename in filenames:
|
47
|
+
if "platformio.ini" in filename:
|
48
|
+
continue
|
49
|
+
file_count += 1
|
50
|
+
if file_count > _MAX_FILES_SEARCH_LIMIT:
|
51
|
+
print(
|
52
|
+
f"More than {_MAX_FILES_SEARCH_LIMIT} files found. Stopping search."
|
53
|
+
)
|
54
|
+
outer_break = True
|
55
|
+
break
|
56
|
+
files.append(Path(root) / filename)
|
57
|
+
if outer_break:
|
58
|
+
break
|
59
|
+
|
60
|
+
return files
|
61
|
+
|
62
|
+
|
63
|
+
def looks_like_fastled_repo(directory: Path) -> bool:
|
64
|
+
libprops = directory / "library.properties"
|
65
|
+
if not libprops.exists():
|
66
|
+
return False
|
67
|
+
txt = libprops.read_text(encoding="utf-8", errors="ignore")
|
68
|
+
return "FastLED" in txt
|
69
|
+
|
70
|
+
|
71
|
+
def _lots_and_lots_of_files(directory: Path) -> bool:
|
72
|
+
return len(get_sketch_files(directory)) > 100
|
73
|
+
|
74
|
+
|
75
|
+
def looks_like_sketch_directory(directory: Path, quick=False) -> bool:
|
76
|
+
if looks_like_fastled_repo(directory):
|
77
|
+
print("Directory looks like the FastLED repo")
|
78
|
+
return False
|
79
|
+
|
80
|
+
if not quick:
|
81
|
+
if _lots_and_lots_of_files(directory):
|
82
|
+
return False
|
83
|
+
|
84
|
+
# walk the path and if there are over 30 files, return False
|
85
|
+
# at the root of the directory there should either be an ino file or a src directory
|
86
|
+
# or some cpp files
|
87
|
+
# if there is a platformio.ini file, return True
|
88
|
+
ino_file_at_root = list(directory.glob("*.ino"))
|
89
|
+
if ino_file_at_root:
|
90
|
+
return True
|
91
|
+
cpp_file_at_root = list(directory.glob("*.cpp"))
|
92
|
+
if cpp_file_at_root:
|
93
|
+
return True
|
94
|
+
platformini_file = list(directory.glob("platformio.ini"))
|
95
|
+
if platformini_file:
|
96
|
+
return True
|
97
|
+
return False
|
fastled/spinner.py
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
import _thread
|
2
|
+
import threading
|
3
|
+
import time
|
4
|
+
import warnings
|
5
|
+
|
6
|
+
from progress.spinner import Spinner as SpinnerImpl
|
7
|
+
|
8
|
+
|
9
|
+
class Spinner:
|
10
|
+
def __init__(self, message: str = ""):
|
11
|
+
self.spinner = SpinnerImpl(message)
|
12
|
+
self.event = threading.Event()
|
13
|
+
self.thread = threading.Thread(target=self._spin, daemon=True)
|
14
|
+
self.thread.start()
|
15
|
+
|
16
|
+
def _spin(self) -> None:
|
17
|
+
try:
|
18
|
+
while not self.event.is_set():
|
19
|
+
self.spinner.next()
|
20
|
+
time.sleep(0.1)
|
21
|
+
except KeyboardInterrupt:
|
22
|
+
_thread.interrupt_main()
|
23
|
+
except Exception as e:
|
24
|
+
warnings.warn(f"Spinner thread failed: {e}")
|
25
|
+
|
26
|
+
def stop(self) -> None:
|
27
|
+
self.event.set()
|
28
|
+
self.thread.join()
|
29
|
+
|
30
|
+
def __enter__(self):
|
31
|
+
return self
|
32
|
+
|
33
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
34
|
+
self.stop()
|
fastled/string_diff.py
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
|
3
|
+
from rapidfuzz import fuzz
|
4
|
+
|
5
|
+
|
6
|
+
# Returns the min distance strings. If there is a tie, it returns
|
7
|
+
# all the strings that have the same min distance.
|
8
|
+
# Returns a tuple of index and string.
|
9
|
+
def string_diff(
|
10
|
+
input_string: str, string_list: list[str], ignore_case=True
|
11
|
+
) -> list[tuple[float, str]]:
|
12
|
+
|
13
|
+
def normalize(s: str) -> str:
|
14
|
+
return s.lower() if ignore_case else s
|
15
|
+
|
16
|
+
# distances = [
|
17
|
+
# #Levenshtein.distance(normalize(input_string), normalize(s)) for s in string_list
|
18
|
+
# fuzz.partial_ratio(normalize(input_string), normalize(s)) for s in string_list
|
19
|
+
# ]
|
20
|
+
distances: list[float] = []
|
21
|
+
for s in string_list:
|
22
|
+
dist = fuzz.token_sort_ratio(normalize(input_string), normalize(s))
|
23
|
+
distances.append(1.0 / (dist + 1.0))
|
24
|
+
min_distance = min(distances)
|
25
|
+
out: list[tuple[float, str]] = []
|
26
|
+
for i, d in enumerate(distances):
|
27
|
+
if d == min_distance:
|
28
|
+
out.append((i, string_list[i]))
|
29
|
+
|
30
|
+
return out
|
31
|
+
|
32
|
+
|
33
|
+
def string_diff_paths(
|
34
|
+
input_string: str | Path, path_list: list[Path], ignore_case=True
|
35
|
+
) -> list[tuple[float, Path]]:
|
36
|
+
string_list = [str(p) for p in path_list]
|
37
|
+
tmp = string_diff(str(input_string), string_list, ignore_case)
|
38
|
+
out: list[tuple[float, Path]] = []
|
39
|
+
for i, j in tmp:
|
40
|
+
p = Path(j)
|
41
|
+
out.append((i, p))
|
42
|
+
return out
|
fastled/test/examples.py
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
from tempfile import TemporaryDirectory
|
2
|
+
|
3
|
+
|
4
|
+
def test_examples(
|
5
|
+
examples: list[str] | None = None, host: str | None = None
|
6
|
+
) -> dict[str, Exception]:
|
7
|
+
"""Test the examples in the given directory."""
|
8
|
+
from fastled import Api
|
9
|
+
|
10
|
+
out: dict[str, Exception] = {}
|
11
|
+
examples = Api.get_examples() if examples is None else examples
|
12
|
+
with TemporaryDirectory() as tmpdir:
|
13
|
+
for example in examples:
|
14
|
+
print(f"Initializing example: {example}")
|
15
|
+
sketch_dir = Api.project_init(example, outputdir=tmpdir, host=host)
|
16
|
+
print(f"Project initialized at: {sketch_dir}")
|
17
|
+
print(f"Compiling example: {example}")
|
18
|
+
result = Api.web_compile(sketch_dir, host=host)
|
19
|
+
if not result.success:
|
20
|
+
out[example] = Exception(result.stdout)
|
21
|
+
return out
|
22
|
+
|
23
|
+
|
24
|
+
def unit_test() -> None:
|
25
|
+
out = test_examples()
|
26
|
+
if out:
|
27
|
+
raise RuntimeError(f"Failed tests: {out}")
|
28
|
+
|
29
|
+
|
30
|
+
if __name__ == "__main__":
|
31
|
+
unit_test()
|
fastled/types.py
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
import argparse
|
2
|
+
from dataclasses import dataclass
|
3
|
+
from enum import Enum
|
4
|
+
from typing import Any
|
5
|
+
|
6
|
+
|
7
|
+
@dataclass
|
8
|
+
class CompileResult:
|
9
|
+
success: bool
|
10
|
+
stdout: str
|
11
|
+
hash_value: str | None
|
12
|
+
zip_bytes: bytes
|
13
|
+
|
14
|
+
def __bool__(self) -> bool:
|
15
|
+
return self.success
|
16
|
+
|
17
|
+
def to_dict(self) -> dict[str, Any]:
|
18
|
+
return self.__dict__.copy()
|
19
|
+
|
20
|
+
|
21
|
+
class CompileServerError(Exception):
|
22
|
+
"""Error class for failing to instantiate CompileServer."""
|
23
|
+
|
24
|
+
pass
|
25
|
+
|
26
|
+
|
27
|
+
class BuildMode(Enum):
|
28
|
+
DEBUG = "DEBUG"
|
29
|
+
QUICK = "QUICK"
|
30
|
+
RELEASE = "RELEASE"
|
31
|
+
|
32
|
+
@classmethod
|
33
|
+
def from_string(cls, mode_str: str) -> "BuildMode":
|
34
|
+
try:
|
35
|
+
return cls[mode_str.upper()]
|
36
|
+
except KeyError:
|
37
|
+
valid_modes = [mode.name for mode in cls]
|
38
|
+
raise ValueError(f"BUILD_MODE must be one of {valid_modes}, got {mode_str}")
|
39
|
+
|
40
|
+
@staticmethod
|
41
|
+
def from_args(args: argparse.Namespace) -> "BuildMode":
|
42
|
+
if args.debug:
|
43
|
+
return BuildMode.DEBUG
|
44
|
+
elif args.release:
|
45
|
+
return BuildMode.RELEASE
|
46
|
+
else:
|
47
|
+
return BuildMode.QUICK
|
48
|
+
|
49
|
+
|
50
|
+
class Platform(Enum):
|
51
|
+
WASM = "WASM"
|
52
|
+
|
53
|
+
@classmethod
|
54
|
+
def from_string(cls, platform_str: str) -> "Platform":
|
55
|
+
try:
|
56
|
+
return cls[platform_str.upper()]
|
57
|
+
except KeyError:
|
58
|
+
valid_modes = [mode.name for mode in cls]
|
59
|
+
raise ValueError(
|
60
|
+
f"Platform must be one of {valid_modes}, got {platform_str}"
|
61
|
+
)
|
fastled/util.py
ADDED
fastled/web_compile.py
ADDED
@@ -0,0 +1,285 @@
|
|
1
|
+
import _thread
|
2
|
+
import io
|
3
|
+
import json
|
4
|
+
import os
|
5
|
+
import shutil
|
6
|
+
import tempfile
|
7
|
+
import zipfile
|
8
|
+
from concurrent.futures import Future, ThreadPoolExecutor, as_completed
|
9
|
+
from dataclasses import dataclass
|
10
|
+
from pathlib import Path
|
11
|
+
|
12
|
+
import httpx
|
13
|
+
|
14
|
+
from fastled.settings import SERVER_PORT
|
15
|
+
from fastled.sketch import get_sketch_files
|
16
|
+
from fastled.types import BuildMode, CompileResult
|
17
|
+
from fastled.util import hash_file
|
18
|
+
|
19
|
+
DEFAULT_HOST = "https://fastled.onrender.com"
|
20
|
+
ENDPOINT_COMPILED_WASM = "compile/wasm"
|
21
|
+
_TIMEOUT = 60 * 4 # 2 mins timeout
|
22
|
+
_AUTH_TOKEN = "oBOT5jbsO4ztgrpNsQwlmFLIKB"
|
23
|
+
ENABLE_EMBEDDED_DATA = True
|
24
|
+
_EXECUTOR = ThreadPoolExecutor(max_workers=8)
|
25
|
+
|
26
|
+
|
27
|
+
@dataclass
|
28
|
+
class ConnectionResult:
|
29
|
+
host: str
|
30
|
+
success: bool
|
31
|
+
ipv4: bool
|
32
|
+
|
33
|
+
|
34
|
+
def _sanitize_host(host: str) -> str:
|
35
|
+
if host.startswith("http"):
|
36
|
+
return host
|
37
|
+
is_local_host = "localhost" in host or "127.0.0.1" in host or "0.0.0.0" in host
|
38
|
+
use_https = not is_local_host
|
39
|
+
if use_https:
|
40
|
+
return host if host.startswith("https://") else f"https://{host}"
|
41
|
+
return host if host.startswith("http://") else f"http://{host}"
|
42
|
+
|
43
|
+
|
44
|
+
def _test_connection(host: str, use_ipv4: bool) -> ConnectionResult:
|
45
|
+
# Function static cache
|
46
|
+
host = _sanitize_host(host)
|
47
|
+
transport = httpx.HTTPTransport(local_address="0.0.0.0") if use_ipv4 else None
|
48
|
+
try:
|
49
|
+
with httpx.Client(
|
50
|
+
timeout=_TIMEOUT,
|
51
|
+
transport=transport,
|
52
|
+
) as test_client:
|
53
|
+
test_response = test_client.get(
|
54
|
+
f"{host}/healthz", timeout=3, follow_redirects=True
|
55
|
+
)
|
56
|
+
result = ConnectionResult(host, test_response.status_code == 200, use_ipv4)
|
57
|
+
except KeyboardInterrupt:
|
58
|
+
_thread.interrupt_main()
|
59
|
+
|
60
|
+
except TimeoutError:
|
61
|
+
result = ConnectionResult(host, False, use_ipv4)
|
62
|
+
except Exception:
|
63
|
+
result = ConnectionResult(host, False, use_ipv4)
|
64
|
+
return result
|
65
|
+
|
66
|
+
|
67
|
+
def _file_info(file_path: Path) -> str:
|
68
|
+
hash_txt = hash_file(file_path)
|
69
|
+
file_size = file_path.stat().st_size
|
70
|
+
json_str = json.dumps({"hash": hash_txt, "size": file_size})
|
71
|
+
return json_str
|
72
|
+
|
73
|
+
|
74
|
+
@dataclass
|
75
|
+
class ZipResult:
|
76
|
+
zip_bytes: bytes
|
77
|
+
zip_embedded_bytes: bytes | None
|
78
|
+
success: bool
|
79
|
+
error: str | None
|
80
|
+
|
81
|
+
|
82
|
+
def zip_files(directory: Path, build_mode: BuildMode) -> ZipResult | Exception:
|
83
|
+
print("Zipping files...")
|
84
|
+
try:
|
85
|
+
files = get_sketch_files(directory)
|
86
|
+
if not files:
|
87
|
+
raise FileNotFoundError(f"No files found in {directory}")
|
88
|
+
for f in files:
|
89
|
+
print(f"Adding file: {f}")
|
90
|
+
# Create in-memory zip file
|
91
|
+
has_embedded_zip = False
|
92
|
+
zip_embedded_buffer = io.BytesIO()
|
93
|
+
zip_buffer = io.BytesIO()
|
94
|
+
with zipfile.ZipFile(
|
95
|
+
zip_embedded_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
96
|
+
) as emebedded_zip_file:
|
97
|
+
with zipfile.ZipFile(
|
98
|
+
zip_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
99
|
+
) as zip_file:
|
100
|
+
for file_path in files:
|
101
|
+
relative_path = file_path.relative_to(directory)
|
102
|
+
achive_path = str(Path("wasm") / relative_path)
|
103
|
+
if str(relative_path).startswith("data") and ENABLE_EMBEDDED_DATA:
|
104
|
+
_file_info_str = _file_info(file_path)
|
105
|
+
zip_file.writestr(
|
106
|
+
achive_path + ".embedded.json", _file_info_str
|
107
|
+
)
|
108
|
+
emebedded_zip_file.write(file_path, relative_path)
|
109
|
+
has_embedded_zip = True
|
110
|
+
else:
|
111
|
+
zip_file.write(file_path, achive_path)
|
112
|
+
# write build mode into the file as build.txt so that sketches are fingerprinted
|
113
|
+
# based on the build mode. Otherwise the same sketch with different build modes
|
114
|
+
# will have the same fingerprint.
|
115
|
+
zip_file.writestr(
|
116
|
+
str(Path("wasm") / "build_mode.txt"), build_mode.value
|
117
|
+
)
|
118
|
+
result = ZipResult(
|
119
|
+
zip_bytes=zip_buffer.getvalue(),
|
120
|
+
zip_embedded_bytes=(
|
121
|
+
zip_embedded_buffer.getvalue() if has_embedded_zip else None
|
122
|
+
),
|
123
|
+
success=True,
|
124
|
+
error=None,
|
125
|
+
)
|
126
|
+
return result
|
127
|
+
except Exception as e:
|
128
|
+
return e
|
129
|
+
|
130
|
+
|
131
|
+
def find_good_connection(
|
132
|
+
urls: list[str], filter_out_bad=True, use_ipv6: bool = True
|
133
|
+
) -> ConnectionResult | None:
|
134
|
+
futures: list[Future] = []
|
135
|
+
for url in urls:
|
136
|
+
|
137
|
+
f = _EXECUTOR.submit(_test_connection, url, use_ipv4=True)
|
138
|
+
futures.append(f)
|
139
|
+
if use_ipv6 and "localhost" not in url:
|
140
|
+
f_v6 = _EXECUTOR.submit(_test_connection, url, use_ipv4=False)
|
141
|
+
futures.append(f_v6)
|
142
|
+
|
143
|
+
try:
|
144
|
+
# Return first successful result
|
145
|
+
for future in as_completed(futures):
|
146
|
+
result: ConnectionResult = future.result()
|
147
|
+
if result.success or not filter_out_bad:
|
148
|
+
return result
|
149
|
+
finally:
|
150
|
+
# Cancel any remaining futures
|
151
|
+
for future in futures:
|
152
|
+
future.cancel()
|
153
|
+
return None
|
154
|
+
|
155
|
+
|
156
|
+
def web_compile(
|
157
|
+
directory: Path | str,
|
158
|
+
host: str | None = None,
|
159
|
+
auth_token: str | None = None,
|
160
|
+
build_mode: BuildMode | None = None,
|
161
|
+
profile: bool = False,
|
162
|
+
) -> CompileResult:
|
163
|
+
if isinstance(directory, str):
|
164
|
+
directory = Path(directory)
|
165
|
+
host = _sanitize_host(host or DEFAULT_HOST)
|
166
|
+
build_mode = build_mode or BuildMode.QUICK
|
167
|
+
print("Compiling on", host)
|
168
|
+
auth_token = auth_token or _AUTH_TOKEN
|
169
|
+
if not directory.exists():
|
170
|
+
raise FileNotFoundError(f"Directory not found: {directory}")
|
171
|
+
zip_result = zip_files(directory, build_mode=build_mode)
|
172
|
+
if isinstance(zip_result, Exception):
|
173
|
+
return CompileResult(
|
174
|
+
success=False, stdout=str(zip_result), hash_value=None, zip_bytes=b""
|
175
|
+
)
|
176
|
+
zip_bytes = zip_result.zip_bytes
|
177
|
+
archive_size = len(zip_bytes)
|
178
|
+
print(f"Web compiling on {host}...")
|
179
|
+
try:
|
180
|
+
host = _sanitize_host(host)
|
181
|
+
urls = [host]
|
182
|
+
domain = host.split("://")[-1]
|
183
|
+
if ":" not in domain:
|
184
|
+
urls.append(f"{host}:{SERVER_PORT}")
|
185
|
+
|
186
|
+
connection_result = find_good_connection(urls)
|
187
|
+
if connection_result is None:
|
188
|
+
print("Connection failed to all endpoints")
|
189
|
+
return CompileResult(
|
190
|
+
success=False,
|
191
|
+
stdout="Connection failed",
|
192
|
+
hash_value=None,
|
193
|
+
zip_bytes=b"",
|
194
|
+
)
|
195
|
+
|
196
|
+
ipv4_stmt = "IPv4" if connection_result.ipv4 else "IPv6"
|
197
|
+
transport = (
|
198
|
+
httpx.HTTPTransport(local_address="0.0.0.0")
|
199
|
+
if connection_result.ipv4
|
200
|
+
else None
|
201
|
+
)
|
202
|
+
with httpx.Client(
|
203
|
+
transport=transport,
|
204
|
+
timeout=_TIMEOUT,
|
205
|
+
) as client:
|
206
|
+
headers = {
|
207
|
+
"accept": "application/json",
|
208
|
+
"authorization": auth_token,
|
209
|
+
"build": (
|
210
|
+
build_mode.value.lower()
|
211
|
+
if build_mode
|
212
|
+
else BuildMode.QUICK.value.lower()
|
213
|
+
),
|
214
|
+
"profile": "true" if profile else "false",
|
215
|
+
}
|
216
|
+
|
217
|
+
url = f"{connection_result.host}/{ENDPOINT_COMPILED_WASM}"
|
218
|
+
print(f"Compiling on {url} via {ipv4_stmt}. Zip size: {archive_size} bytes")
|
219
|
+
files = {"file": ("wasm.zip", zip_bytes, "application/x-zip-compressed")}
|
220
|
+
response = client.post(
|
221
|
+
url,
|
222
|
+
follow_redirects=True,
|
223
|
+
files=files,
|
224
|
+
headers=headers,
|
225
|
+
timeout=_TIMEOUT,
|
226
|
+
)
|
227
|
+
|
228
|
+
if response.status_code != 200:
|
229
|
+
json_response = response.json()
|
230
|
+
detail = json_response.get("detail", "Could not compile")
|
231
|
+
return CompileResult(
|
232
|
+
success=False, stdout=detail, hash_value=None, zip_bytes=b""
|
233
|
+
)
|
234
|
+
|
235
|
+
print(f"Response status code: {response}")
|
236
|
+
# Create a temporary directory to extract the zip
|
237
|
+
with tempfile.TemporaryDirectory() as extract_dir:
|
238
|
+
extract_path = Path(extract_dir)
|
239
|
+
|
240
|
+
# Write the response content to a temporary zip file
|
241
|
+
temp_zip = extract_path / "response.zip"
|
242
|
+
temp_zip.write_bytes(response.content)
|
243
|
+
|
244
|
+
# Extract the zip
|
245
|
+
shutil.unpack_archive(temp_zip, extract_path, "zip")
|
246
|
+
|
247
|
+
if zip_result.zip_embedded_bytes:
|
248
|
+
# extract the embedded bytes, which were not sent to the server
|
249
|
+
temp_zip.write_bytes(zip_result.zip_embedded_bytes)
|
250
|
+
shutil.unpack_archive(temp_zip, extract_path, "zip")
|
251
|
+
|
252
|
+
# we don't need the temp zip anymore
|
253
|
+
temp_zip.unlink()
|
254
|
+
|
255
|
+
# Read stdout from out.txt if it exists
|
256
|
+
stdout_file = extract_path / "out.txt"
|
257
|
+
hash_file = extract_path / "hash.txt"
|
258
|
+
stdout = stdout_file.read_text() if stdout_file.exists() else ""
|
259
|
+
hash_value = hash_file.read_text() if hash_file.exists() else None
|
260
|
+
|
261
|
+
# now rezip the extracted files since we added the embedded json files
|
262
|
+
out_buffer = io.BytesIO()
|
263
|
+
with zipfile.ZipFile(
|
264
|
+
out_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
265
|
+
) as out_zip:
|
266
|
+
for root, _, _files in os.walk(extract_path):
|
267
|
+
for file in _files:
|
268
|
+
file_path = Path(root) / file
|
269
|
+
relative_path = file_path.relative_to(extract_path)
|
270
|
+
out_zip.write(file_path, relative_path)
|
271
|
+
|
272
|
+
return CompileResult(
|
273
|
+
success=True,
|
274
|
+
stdout=stdout,
|
275
|
+
hash_value=hash_value,
|
276
|
+
zip_bytes=out_buffer.getvalue(),
|
277
|
+
)
|
278
|
+
except KeyboardInterrupt:
|
279
|
+
print("Keyboard interrupt")
|
280
|
+
raise
|
281
|
+
except httpx.HTTPError as e:
|
282
|
+
print(f"Error: {e}")
|
283
|
+
return CompileResult(
|
284
|
+
success=False, stdout=str(e), hash_value=None, zip_bytes=b""
|
285
|
+
)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 zackees
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|