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/web_compile.py CHANGED
@@ -1,227 +1,294 @@
1
- import io
2
- import shutil
3
- import tempfile
4
- import zipfile
5
- from concurrent.futures import ThreadPoolExecutor, as_completed
6
- from dataclasses import dataclass
7
- from pathlib import Path
8
-
9
- import httpx
10
-
11
- from fastled.build_mode import BuildMode
12
- from fastled.compile_server import SERVER_PORT
13
- from fastled.sketch import get_sketch_files
14
-
15
- DEFAULT_HOST = "https://fastled.onrender.com"
16
- ENDPOINT_COMPILED_WASM = "compile/wasm"
17
- _TIMEOUT = 60 * 4 # 2 mins timeout
18
- _AUTH_TOKEN = "oBOT5jbsO4ztgrpNsQwlmFLIKB"
19
-
20
- _THREAD_POOL = ThreadPoolExecutor(max_workers=8)
21
-
22
-
23
- @dataclass
24
- class TestConnectionResult:
25
- host: str
26
- success: bool
27
- ipv4: bool
28
-
29
-
30
- @dataclass
31
- class WebCompileResult:
32
- success: bool
33
- stdout: str
34
- hash_value: str | None
35
- zip_bytes: bytes
36
-
37
- def __bool__(self) -> bool:
38
- return self.success
39
-
40
-
41
- def _sanitize_host(host: str) -> str:
42
- if host.startswith("http"):
43
- return host
44
- is_local_host = "localhost" in host or "127.0.0.1" in host or "0.0.0.0" in host
45
- use_https = not is_local_host
46
- if use_https:
47
- return host if host.startswith("https://") else f"https://{host}"
48
- return host if host.startswith("http://") else f"http://{host}"
49
-
50
-
51
- _CONNECTION_ERROR_MAP: dict[str, TestConnectionResult] = {}
52
-
53
-
54
- def _test_connection(host: str, use_ipv4: bool) -> TestConnectionResult:
55
- key = f"{host}-{use_ipv4}"
56
- maybe_result: TestConnectionResult | None = _CONNECTION_ERROR_MAP.get(key)
57
- if maybe_result is not None:
58
- return maybe_result
59
- transport = httpx.HTTPTransport(local_address="0.0.0.0") if use_ipv4 else None
60
- try:
61
- with httpx.Client(
62
- timeout=_TIMEOUT,
63
- transport=transport,
64
- ) as test_client:
65
- test_response = test_client.get(
66
- f"{host}/healthz", timeout=3, follow_redirects=True
67
- )
68
- result = TestConnectionResult(
69
- host, test_response.status_code == 200, use_ipv4
70
- )
71
- _CONNECTION_ERROR_MAP[key] = result
72
- except Exception:
73
- result = TestConnectionResult(host, False, use_ipv4)
74
- _CONNECTION_ERROR_MAP[key] = result
75
- return result
76
-
77
-
78
- def zip_files(directory: Path) -> bytes | Exception:
79
- print("Zipping files...")
80
- try:
81
- files = get_sketch_files(directory)
82
- if not files:
83
- raise FileNotFoundError(f"No files found in {directory}")
84
- for f in files:
85
- print(f"Adding file: {f}")
86
- # Create in-memory zip file
87
- zip_buffer = io.BytesIO()
88
- with zipfile.ZipFile(
89
- zip_buffer, "w", zipfile.ZIP_DEFLATED, compresslevel=9
90
- ) as zip_file:
91
- for file_path in files:
92
- relative_path = file_path.relative_to(directory)
93
- zip_file.write(file_path, str(Path("wasm") / relative_path))
94
- return zip_buffer.getvalue()
95
- except Exception as e:
96
- return e
97
-
98
-
99
- def web_compile(
100
- directory: Path,
101
- host: str | None = None,
102
- auth_token: str | None = None,
103
- build_mode: BuildMode | None = None,
104
- profile: bool = False,
105
- ) -> WebCompileResult:
106
- host = _sanitize_host(host or DEFAULT_HOST)
107
- print("Compiling on", host)
108
- auth_token = auth_token or _AUTH_TOKEN
109
-
110
- if not directory.exists():
111
- raise FileNotFoundError(f"Directory not found: {directory}")
112
-
113
- zip_bytes = zip_files(directory)
114
- if isinstance(zip_bytes, Exception):
115
- return WebCompileResult(
116
- success=False, stdout=str(zip_bytes), hash_value=None, zip_bytes=b""
117
- )
118
- archive_size = len(zip_bytes)
119
- print(f"Web compiling on {host}...")
120
- try:
121
-
122
- files = {"file": ("wasm.zip", zip_bytes, "application/x-zip-compressed")}
123
- urls = [host]
124
- domain = host.split("://")[-1]
125
- if ":" not in domain:
126
- urls.append(f"{host}:{SERVER_PORT}")
127
- test_connection_result: TestConnectionResult | None = None
128
-
129
- futures: list = []
130
- ip_versions = [True, False] if "localhost" not in host else [True]
131
- for ipv4 in ip_versions:
132
- for url in urls:
133
- f = _THREAD_POOL.submit(_test_connection, url, ipv4)
134
- futures.append(f)
135
-
136
- succeeded = False
137
- for future in as_completed(futures):
138
- result: TestConnectionResult = future.result()
139
-
140
- if result.success:
141
- print(f"Connection successful to {result.host}")
142
- succeeded = True
143
- # host = test_url
144
- test_connection_result = result
145
- break
146
- else:
147
- print(f"Ignoring {result.host} due to connection failure")
148
-
149
- if not succeeded:
150
- print("Connection failed to all endpoints")
151
- return WebCompileResult(
152
- success=False,
153
- stdout="Connection failed",
154
- hash_value=None,
155
- zip_bytes=b"",
156
- )
157
- assert test_connection_result is not None
158
- ipv4_stmt = "IPv4" if test_connection_result.ipv4 else "IPv6"
159
- transport = (
160
- httpx.HTTPTransport(local_address="0.0.0.0")
161
- if test_connection_result.ipv4
162
- else None
163
- )
164
- with httpx.Client(
165
- transport=transport,
166
- timeout=_TIMEOUT,
167
- ) as client:
168
- headers = {
169
- "accept": "application/json",
170
- "authorization": auth_token,
171
- "build": (
172
- build_mode.value.lower()
173
- if build_mode
174
- else BuildMode.QUICK.value.lower()
175
- ),
176
- "profile": "true" if profile else "false",
177
- }
178
-
179
- url = f"{test_connection_result.host}/{ENDPOINT_COMPILED_WASM}"
180
- print(f"Compiling on {url} via {ipv4_stmt}. Zip size: {archive_size} bytes")
181
- response = client.post(
182
- url,
183
- follow_redirects=True,
184
- files=files,
185
- headers=headers,
186
- timeout=_TIMEOUT,
187
- )
188
-
189
- if response.status_code != 200:
190
- json_response = response.json()
191
- detail = json_response.get("detail", "Could not compile")
192
- return WebCompileResult(
193
- success=False, stdout=detail, hash_value=None, zip_bytes=b""
194
- )
195
-
196
- print(f"Response status code: {response}")
197
- # Create a temporary directory to extract the zip
198
- with tempfile.TemporaryDirectory() as extract_dir:
199
- extract_path = Path(extract_dir)
200
-
201
- # Write the response content to a temporary zip file
202
- temp_zip = extract_path / "response.zip"
203
- temp_zip.write_bytes(response.content)
204
-
205
- # Extract the zip
206
- shutil.unpack_archive(temp_zip, extract_path, "zip")
207
-
208
- # Read stdout from out.txt if it exists
209
- stdout_file = extract_path / "out.txt"
210
- hash_file = extract_path / "hash.txt"
211
- stdout = stdout_file.read_text() if stdout_file.exists() else ""
212
- hash_value = hash_file.read_text() if hash_file.exists() else None
213
-
214
- return WebCompileResult(
215
- success=True,
216
- stdout=stdout,
217
- hash_value=hash_value,
218
- zip_bytes=response.content,
219
- )
220
- except KeyboardInterrupt:
221
- print("Keyboard interrupt")
222
- raise
223
- except httpx.HTTPError as e:
224
- print(f"Error: {e}")
225
- return WebCompileResult(
226
- success=False, stdout=str(e), hash_value=None, zip_bytes=b""
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
+ )