fastled 1.1.7__py2.py3-none-any.whl → 1.1.16__py2.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 +3 -0
- fastled/app.py +117 -53
- fastled/compile_server.py +156 -251
- fastled/docker_manager.py +389 -135
- fastled/filewatcher.py +196 -146
- fastled/keyboard.py +91 -0
- fastled/open_browser.py +5 -1
- fastled/util.py +10 -0
- fastled/web_compile.py +294 -227
- fastled-1.1.16.dist-info/METADATA +198 -0
- fastled-1.1.16.dist-info/RECORD +20 -0
- fastled/check_cpp_syntax.py +0 -34
- fastled-1.1.7.dist-info/METADATA +0 -120
- fastled-1.1.7.dist-info/RECORD +0 -19
- {fastled-1.1.7.dist-info → fastled-1.1.16.dist-info}/LICENSE +0 -0
- {fastled-1.1.7.dist-info → fastled-1.1.16.dist-info}/WHEEL +0 -0
- {fastled-1.1.7.dist-info → fastled-1.1.16.dist-info}/entry_points.txt +0 -0
- {fastled-1.1.7.dist-info → fastled-1.1.16.dist-info}/top_level.txt +0 -0
fastled/web_compile.py
CHANGED
@@ -1,227 +1,294 @@
|
|
1
|
-
import
|
2
|
-
import
|
3
|
-
import
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
import
|
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
|
-
def
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
if
|
47
|
-
return host
|
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
|
-
return result
|
76
|
-
|
77
|
-
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
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.build_mode import BuildMode
|
15
|
+
from fastled.compile_server import SERVER_PORT
|
16
|
+
from fastled.sketch import get_sketch_files
|
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
|
+
@dataclass
|
35
|
+
class WebCompileResult:
|
36
|
+
success: bool
|
37
|
+
stdout: str
|
38
|
+
hash_value: str | None
|
39
|
+
zip_bytes: bytes
|
40
|
+
|
41
|
+
def __bool__(self) -> bool:
|
42
|
+
return self.success
|
43
|
+
|
44
|
+
|
45
|
+
def _sanitize_host(host: str) -> str:
|
46
|
+
if host.startswith("http"):
|
47
|
+
return host
|
48
|
+
is_local_host = "localhost" in host or "127.0.0.1" in host or "0.0.0.0" in host
|
49
|
+
use_https = not is_local_host
|
50
|
+
if use_https:
|
51
|
+
return host if host.startswith("https://") else f"https://{host}"
|
52
|
+
return host if host.startswith("http://") else f"http://{host}"
|
53
|
+
|
54
|
+
|
55
|
+
def _test_connection(host: str, use_ipv4: bool) -> ConnectionResult:
|
56
|
+
# Function static cache
|
57
|
+
host = _sanitize_host(host)
|
58
|
+
transport = httpx.HTTPTransport(local_address="0.0.0.0") if use_ipv4 else None
|
59
|
+
try:
|
60
|
+
with httpx.Client(
|
61
|
+
timeout=_TIMEOUT,
|
62
|
+
transport=transport,
|
63
|
+
) as test_client:
|
64
|
+
test_response = test_client.get(
|
65
|
+
f"{host}/healthz", timeout=3, follow_redirects=True
|
66
|
+
)
|
67
|
+
result = ConnectionResult(host, test_response.status_code == 200, use_ipv4)
|
68
|
+
except KeyboardInterrupt:
|
69
|
+
_thread.interrupt_main()
|
70
|
+
|
71
|
+
except TimeoutError:
|
72
|
+
result = ConnectionResult(host, False, use_ipv4)
|
73
|
+
except Exception:
|
74
|
+
result = ConnectionResult(host, False, use_ipv4)
|
75
|
+
return result
|
76
|
+
|
77
|
+
|
78
|
+
def _file_info(file_path: Path) -> str:
|
79
|
+
hash_txt = hash_file(file_path)
|
80
|
+
file_size = file_path.stat().st_size
|
81
|
+
json_str = json.dumps({"hash": hash_txt, "size": file_size})
|
82
|
+
return json_str
|
83
|
+
|
84
|
+
|
85
|
+
@dataclass
|
86
|
+
class ZipResult:
|
87
|
+
zip_bytes: bytes
|
88
|
+
zip_embedded_bytes: bytes | None
|
89
|
+
success: bool
|
90
|
+
error: str | None
|
91
|
+
|
92
|
+
|
93
|
+
def zip_files(directory: Path, build_mode: BuildMode) -> ZipResult | Exception:
|
94
|
+
print("Zipping files...")
|
95
|
+
try:
|
96
|
+
files = get_sketch_files(directory)
|
97
|
+
if not files:
|
98
|
+
raise FileNotFoundError(f"No files found in {directory}")
|
99
|
+
for f in files:
|
100
|
+
print(f"Adding file: {f}")
|
101
|
+
# Create in-memory zip file
|
102
|
+
has_embedded_zip = False
|
103
|
+
zip_embedded_buffer = io.BytesIO()
|
104
|
+
zip_buffer = io.BytesIO()
|
105
|
+
with zipfile.ZipFile(
|
106
|
+
zip_embedded_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
107
|
+
) as emebedded_zip_file:
|
108
|
+
with zipfile.ZipFile(
|
109
|
+
zip_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
110
|
+
) as zip_file:
|
111
|
+
for file_path in files:
|
112
|
+
relative_path = file_path.relative_to(directory)
|
113
|
+
achive_path = str(Path("wasm") / relative_path)
|
114
|
+
if str(relative_path).startswith("data") and ENABLE_EMBEDDED_DATA:
|
115
|
+
_file_info_str = _file_info(file_path)
|
116
|
+
zip_file.writestr(
|
117
|
+
achive_path + ".embedded.json", _file_info_str
|
118
|
+
)
|
119
|
+
emebedded_zip_file.write(file_path, relative_path)
|
120
|
+
has_embedded_zip = True
|
121
|
+
else:
|
122
|
+
zip_file.write(file_path, achive_path)
|
123
|
+
# write build mode into the file as build.txt so that sketches are fingerprinted
|
124
|
+
# based on the build mode. Otherwise the same sketch with different build modes
|
125
|
+
# will have the same fingerprint.
|
126
|
+
zip_file.writestr(
|
127
|
+
str(Path("wasm") / "build_mode.txt"), build_mode.value
|
128
|
+
)
|
129
|
+
result = ZipResult(
|
130
|
+
zip_bytes=zip_buffer.getvalue(),
|
131
|
+
zip_embedded_bytes=(
|
132
|
+
zip_embedded_buffer.getvalue() if has_embedded_zip else None
|
133
|
+
),
|
134
|
+
success=True,
|
135
|
+
error=None,
|
136
|
+
)
|
137
|
+
return result
|
138
|
+
except Exception as e:
|
139
|
+
return e
|
140
|
+
|
141
|
+
|
142
|
+
def find_good_connection(
|
143
|
+
urls: list[str], filter_out_bad=True, use_ipv6: bool = True
|
144
|
+
) -> ConnectionResult | None:
|
145
|
+
futures: list[Future] = []
|
146
|
+
for url in urls:
|
147
|
+
|
148
|
+
f = _EXECUTOR.submit(_test_connection, url, use_ipv4=True)
|
149
|
+
futures.append(f)
|
150
|
+
if use_ipv6 and "localhost" not in url:
|
151
|
+
f_v6 = _EXECUTOR.submit(_test_connection, url, use_ipv4=False)
|
152
|
+
futures.append(f_v6)
|
153
|
+
|
154
|
+
try:
|
155
|
+
# Return first successful result
|
156
|
+
for future in as_completed(futures):
|
157
|
+
result: ConnectionResult = future.result()
|
158
|
+
if result.success or not filter_out_bad:
|
159
|
+
return result
|
160
|
+
finally:
|
161
|
+
# Cancel any remaining futures
|
162
|
+
for future in futures:
|
163
|
+
future.cancel()
|
164
|
+
return None
|
165
|
+
|
166
|
+
|
167
|
+
def web_compile(
|
168
|
+
directory: Path,
|
169
|
+
host: str | None = None,
|
170
|
+
auth_token: str | None = None,
|
171
|
+
build_mode: BuildMode | None = None,
|
172
|
+
profile: bool = False,
|
173
|
+
) -> WebCompileResult:
|
174
|
+
host = _sanitize_host(host or DEFAULT_HOST)
|
175
|
+
build_mode = build_mode or BuildMode.QUICK
|
176
|
+
print("Compiling on", host)
|
177
|
+
auth_token = auth_token or _AUTH_TOKEN
|
178
|
+
if not directory.exists():
|
179
|
+
raise FileNotFoundError(f"Directory not found: {directory}")
|
180
|
+
zip_result = zip_files(directory, build_mode=build_mode)
|
181
|
+
if isinstance(zip_result, Exception):
|
182
|
+
return WebCompileResult(
|
183
|
+
success=False, stdout=str(zip_result), hash_value=None, zip_bytes=b""
|
184
|
+
)
|
185
|
+
zip_bytes = zip_result.zip_bytes
|
186
|
+
archive_size = len(zip_bytes)
|
187
|
+
print(f"Web compiling on {host}...")
|
188
|
+
try:
|
189
|
+
host = _sanitize_host(host)
|
190
|
+
urls = [host]
|
191
|
+
domain = host.split("://")[-1]
|
192
|
+
if ":" not in domain:
|
193
|
+
urls.append(f"{host}:{SERVER_PORT}")
|
194
|
+
|
195
|
+
connection_result = find_good_connection(urls)
|
196
|
+
if connection_result is None:
|
197
|
+
print("Connection failed to all endpoints")
|
198
|
+
return WebCompileResult(
|
199
|
+
success=False,
|
200
|
+
stdout="Connection failed",
|
201
|
+
hash_value=None,
|
202
|
+
zip_bytes=b"",
|
203
|
+
)
|
204
|
+
|
205
|
+
ipv4_stmt = "IPv4" if connection_result.ipv4 else "IPv6"
|
206
|
+
transport = (
|
207
|
+
httpx.HTTPTransport(local_address="0.0.0.0")
|
208
|
+
if connection_result.ipv4
|
209
|
+
else None
|
210
|
+
)
|
211
|
+
with httpx.Client(
|
212
|
+
transport=transport,
|
213
|
+
timeout=_TIMEOUT,
|
214
|
+
) as client:
|
215
|
+
headers = {
|
216
|
+
"accept": "application/json",
|
217
|
+
"authorization": auth_token,
|
218
|
+
"build": (
|
219
|
+
build_mode.value.lower()
|
220
|
+
if build_mode
|
221
|
+
else BuildMode.QUICK.value.lower()
|
222
|
+
),
|
223
|
+
"profile": "true" if profile else "false",
|
224
|
+
}
|
225
|
+
|
226
|
+
url = f"{connection_result.host}/{ENDPOINT_COMPILED_WASM}"
|
227
|
+
print(f"Compiling on {url} via {ipv4_stmt}. Zip size: {archive_size} bytes")
|
228
|
+
files = {"file": ("wasm.zip", zip_bytes, "application/x-zip-compressed")}
|
229
|
+
response = client.post(
|
230
|
+
url,
|
231
|
+
follow_redirects=True,
|
232
|
+
files=files,
|
233
|
+
headers=headers,
|
234
|
+
timeout=_TIMEOUT,
|
235
|
+
)
|
236
|
+
|
237
|
+
if response.status_code != 200:
|
238
|
+
json_response = response.json()
|
239
|
+
detail = json_response.get("detail", "Could not compile")
|
240
|
+
return WebCompileResult(
|
241
|
+
success=False, stdout=detail, hash_value=None, zip_bytes=b""
|
242
|
+
)
|
243
|
+
|
244
|
+
print(f"Response status code: {response}")
|
245
|
+
# Create a temporary directory to extract the zip
|
246
|
+
with tempfile.TemporaryDirectory() as extract_dir:
|
247
|
+
extract_path = Path(extract_dir)
|
248
|
+
|
249
|
+
# Write the response content to a temporary zip file
|
250
|
+
temp_zip = extract_path / "response.zip"
|
251
|
+
temp_zip.write_bytes(response.content)
|
252
|
+
|
253
|
+
# Extract the zip
|
254
|
+
shutil.unpack_archive(temp_zip, extract_path, "zip")
|
255
|
+
|
256
|
+
if zip_result.zip_embedded_bytes:
|
257
|
+
# extract the embedded bytes, which were not sent to the server
|
258
|
+
temp_zip.write_bytes(zip_result.zip_embedded_bytes)
|
259
|
+
shutil.unpack_archive(temp_zip, extract_path, "zip")
|
260
|
+
|
261
|
+
# we don't need the temp zip anymore
|
262
|
+
temp_zip.unlink()
|
263
|
+
|
264
|
+
# Read stdout from out.txt if it exists
|
265
|
+
stdout_file = extract_path / "out.txt"
|
266
|
+
hash_file = extract_path / "hash.txt"
|
267
|
+
stdout = stdout_file.read_text() if stdout_file.exists() else ""
|
268
|
+
hash_value = hash_file.read_text() if hash_file.exists() else None
|
269
|
+
|
270
|
+
# now rezip the extracted files since we added the embedded json files
|
271
|
+
out_buffer = io.BytesIO()
|
272
|
+
with zipfile.ZipFile(
|
273
|
+
out_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
|
274
|
+
) as out_zip:
|
275
|
+
for root, _, _files in os.walk(extract_path):
|
276
|
+
for file in _files:
|
277
|
+
file_path = Path(root) / file
|
278
|
+
relative_path = file_path.relative_to(extract_path)
|
279
|
+
out_zip.write(file_path, relative_path)
|
280
|
+
|
281
|
+
return WebCompileResult(
|
282
|
+
success=True,
|
283
|
+
stdout=stdout,
|
284
|
+
hash_value=hash_value,
|
285
|
+
zip_bytes=out_buffer.getvalue(),
|
286
|
+
)
|
287
|
+
except KeyboardInterrupt:
|
288
|
+
print("Keyboard interrupt")
|
289
|
+
raise
|
290
|
+
except httpx.HTTPError as e:
|
291
|
+
print(f"Error: {e}")
|
292
|
+
return WebCompileResult(
|
293
|
+
success=False, stdout=str(e), hash_value=None, zip_bytes=b""
|
294
|
+
)
|