fastled 1.0.8__py2.py3-none-any.whl → 1.0.10__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/app.py CHANGED
@@ -1,376 +1,383 @@
1
- """
2
- Uses the latest wasm compiler image to compile the FastLED sketch.
3
- """
4
-
5
- import argparse
6
- import os
7
- import platform
8
- import shutil
9
- import subprocess
10
- import sys
11
- import tempfile
12
- import time
13
- from dataclasses import dataclass
14
- from pathlib import Path
15
-
16
- from fastled import __version__
17
- from fastled.build_mode import BuildMode, get_build_mode
18
- from fastled.compile_server import CompileServer, looks_like_fastled_repo
19
- from fastled.docker_manager import DockerManager
20
- from fastled.filewatcher import FileChangedNotifier
21
- from fastled.open_browser import open_browser_thread
22
- from fastled.sketch import looks_like_sketch_directory
23
- from fastled.web_compile import web_compile
24
-
25
- machine = platform.machine().lower()
26
- IS_ARM: bool = "arm" in machine or "aarch64" in machine
27
- PLATFORM_TAG: str = "-arm64" if IS_ARM else ""
28
- CONTAINER_NAME = f"fastled-wasm-compiler{PLATFORM_TAG}"
29
- DEFAULT_URL = "https://fastled.onrender.com"
30
-
31
-
32
- @dataclass
33
- class CompiledResult:
34
- """Dataclass to hold the result of the compilation."""
35
-
36
- success: bool
37
- fastled_js: str
38
- hash_value: str | None
39
-
40
-
41
- DOCKER = DockerManager(container_name=CONTAINER_NAME)
42
-
43
-
44
- def parse_args() -> argparse.Namespace:
45
- """Parse command-line arguments."""
46
- parser = argparse.ArgumentParser(description=f"FastLED WASM Compiler {__version__}")
47
- parser.add_argument(
48
- "--version", action="version", version=f"%(prog)s {__version__}"
49
- )
50
- parser.add_argument(
51
- "directory",
52
- type=str,
53
- nargs="?",
54
- default=None,
55
- help="Directory containing the FastLED sketch to compile",
56
- )
57
- parser.add_argument(
58
- "--just-compile",
59
- action="store_true",
60
- help="Just compile, skip opening the browser and watching for changes.",
61
- )
62
- parser.add_argument(
63
- "--web",
64
- "-w",
65
- type=str,
66
- nargs="?",
67
- # const does not seem to be working as expected
68
- const=DEFAULT_URL, # Default value when --web is specified without value
69
- help="Use web compiler. Optional URL can be provided (default: https://fastled.onrender.com)",
70
- )
71
- parser.add_argument(
72
- "-i",
73
- "--interactive",
74
- action="store_true",
75
- help="Run in interactive mode (Not available with --web)",
76
- )
77
- parser.add_argument(
78
- "--profile",
79
- action="store_true",
80
- help="Enable profiling for web compilation",
81
- )
82
- build_mode = parser.add_mutually_exclusive_group()
83
- build_mode.add_argument("--debug", action="store_true", help="Build in debug mode")
84
- build_mode.add_argument(
85
- "--quick",
86
- action="store_true",
87
- default=True,
88
- help="Build in quick mode (default)",
89
- )
90
- build_mode.add_argument(
91
- "--release", action="store_true", help="Build in release mode"
92
- )
93
- build_mode.add_argument(
94
- "--server",
95
- action="store_true",
96
- help="Run the server in the current directory, volume mapping fastled if we are in the repo",
97
- )
98
-
99
- build_mode.add_argument(
100
- "--force-compile",
101
- action="store_true",
102
- help="Skips the test to see if the current directory is a valid FastLED sketch directory",
103
- )
104
-
105
- args = parser.parse_args()
106
- if args.server and args.web:
107
- parser.error("--server and --web are mutually exclusive")
108
- if args.directory is None and not args.server:
109
- # does current directory look like a sketch?
110
- maybe_sketch_dir = Path(os.getcwd())
111
- if looks_like_sketch_directory(maybe_sketch_dir):
112
- args.directory = str(maybe_sketch_dir)
113
- else:
114
- print(
115
- "\nYou either need to specify a sketch directory or run in --server mode."
116
- )
117
- sys.exit(1)
118
- return args
119
-
120
-
121
- def run_web_compiler(
122
- directory: Path,
123
- host: str,
124
- build_mode: BuildMode,
125
- profile: bool,
126
- last_hash_value: str | None,
127
- ) -> CompiledResult:
128
- input_dir = Path(directory)
129
- output_dir = input_dir / "fastled_js"
130
- start = time.time()
131
- web_result = web_compile(
132
- directory=input_dir, host=host, build_mode=build_mode, profile=profile
133
- )
134
- diff = time.time() - start
135
- if not web_result.success:
136
- print("\nWeb compilation failed:")
137
- print(f"Time taken: {diff:.2f} seconds")
138
- print(web_result.stdout)
139
- return CompiledResult(success=False, fastled_js="", hash_value=None)
140
-
141
- def print_results() -> None:
142
- hash_value = (
143
- web_result.hash_value
144
- if web_result.hash_value is not None
145
- else "NO HASH VALUE"
146
- )
147
- print(
148
- f"\nWeb compilation successful\n Time: {diff:.2f}\n output: {output_dir}\n hash: {hash_value}\n zip size: {len(web_result.zip_bytes)} bytes"
149
- )
150
-
151
- # now check to see if the hash value is the same as the last hash value
152
- if last_hash_value is not None and last_hash_value == web_result.hash_value:
153
- print("\nSkipping redeploy: No significant changes found.")
154
- print_results()
155
- return CompiledResult(
156
- success=True, fastled_js=str(output_dir), hash_value=web_result.hash_value
157
- )
158
-
159
- # Extract zip contents to fastled_js directory
160
- output_dir.mkdir(exist_ok=True)
161
- with tempfile.TemporaryDirectory() as temp_dir:
162
- temp_path = Path(temp_dir)
163
- temp_zip = temp_path / "result.zip"
164
- temp_zip.write_bytes(web_result.zip_bytes)
165
-
166
- # Clear existing contents
167
- shutil.rmtree(output_dir, ignore_errors=True)
168
- output_dir.mkdir(exist_ok=True)
169
-
170
- # Extract zip contents
171
- shutil.unpack_archive(temp_zip, output_dir, "zip")
172
-
173
- print(web_result.stdout)
174
- print_results()
175
- return CompiledResult(
176
- success=True, fastled_js=str(output_dir), hash_value=web_result.hash_value
177
- )
178
-
179
-
180
- def _try_start_server_or_get_url(args: argparse.Namespace) -> str | CompileServer:
181
- if args.web:
182
- if isinstance(args.web, str):
183
- return args.web
184
- if isinstance(args.web, bool):
185
- return DEFAULT_URL
186
- return args.web
187
- else:
188
- try:
189
- compile_server = CompileServer()
190
- print("Waiting for the local compiler to start...")
191
- if not compile_server.wait_for_startup():
192
- print("Failed to start local compiler.")
193
- raise RuntimeError("Failed to start local compiler.")
194
- return compile_server
195
- except KeyboardInterrupt:
196
- raise
197
- except RuntimeError:
198
- print("Failed to start local compile server, using web compiler instead.")
199
- return DEFAULT_URL
200
-
201
-
202
- def run_client(args: argparse.Namespace) -> int:
203
- compile_server: CompileServer | None = None
204
- open_web_browser = not args.just_compile
205
- profile = args.profile
206
- if not args.force_compile and not looks_like_sketch_directory(Path(args.directory)):
207
- print(
208
- "Error: Not a valid FastLED sketch directory, if you are sure it is, use --force-compile"
209
- )
210
- return 1
211
-
212
- # If not explicitly using web compiler, check Docker installation
213
- if not args.web and not DOCKER.is_docker_installed():
214
- print(
215
- "\nDocker is not installed on this system - switching to web compiler instead."
216
- )
217
- args.web = True
218
-
219
- url: str
220
- try:
221
- try:
222
- url_or_server: str | CompileServer = _try_start_server_or_get_url(args)
223
- if isinstance(url_or_server, str):
224
- print(f"Found URL: {url_or_server}")
225
- url = url_or_server
226
- else:
227
- compile_server = url_or_server
228
- print(f"Server started at {compile_server.url()}")
229
- url = compile_server.url()
230
- except KeyboardInterrupt:
231
- print("\nExiting from first try...")
232
- if compile_server:
233
- compile_server.stop()
234
- return 1
235
- except Exception as e:
236
- print(f"Error: {e}")
237
- return 1
238
- build_mode: BuildMode = get_build_mode(args)
239
-
240
- def compile_function(
241
- url: str = url,
242
- build_mode: BuildMode = build_mode,
243
- profile: bool = profile,
244
- last_hash_value: str | None = None,
245
- ) -> CompiledResult:
246
- return run_web_compiler(
247
- args.directory,
248
- host=url,
249
- build_mode=build_mode,
250
- profile=profile,
251
- last_hash_value=last_hash_value,
252
- )
253
-
254
- result: CompiledResult = compile_function(last_hash_value=None)
255
- last_compiled_result: CompiledResult = result
256
-
257
- if not result.success:
258
- print("\nCompilation failed.")
259
- return 1
260
-
261
- browser_proc: subprocess.Popen | None = None
262
- if open_web_browser:
263
- browser_proc = open_browser_thread(Path(args.directory) / "fastled_js")
264
- else:
265
- print(
266
- "\nCompilation successful. Run without --just-compile to open in browser and watch for changes."
267
- )
268
- if compile_server:
269
- print("Shutting down compile server...")
270
- compile_server.stop()
271
- return 0
272
-
273
- if args.just_compile:
274
- if compile_server:
275
- compile_server.stop()
276
- if browser_proc:
277
- browser_proc.kill()
278
- return 0 if result.success else 1
279
- except KeyboardInterrupt:
280
- print("\nExiting from main")
281
- if compile_server:
282
- compile_server.stop()
283
- return 1
284
-
285
- # Watch mode
286
- print("\nWatching for changes. Press Ctrl+C to stop...")
287
- watcher = FileChangedNotifier(args.directory, excluded_patterns=["fastled_js"])
288
- watcher.start()
289
-
290
- try:
291
- while True:
292
- try:
293
- changed_files = watcher.get_all_changes()
294
- except KeyboardInterrupt:
295
- print("\nExiting from watcher...")
296
- raise
297
- except Exception as e:
298
- print(f"Error getting changes: {e}")
299
- changed_files = []
300
- if changed_files:
301
- print(f"\nChanges detected in {changed_files}")
302
- last_hash_value = last_compiled_result.hash_value
303
- result = compile_function(last_hash_value=last_hash_value)
304
- if not result.success:
305
- print("\nRecompilation failed.")
306
- else:
307
- print("\nRecompilation successful.")
308
- time.sleep(0.3)
309
- except KeyboardInterrupt:
310
- watcher.stop()
311
- print("\nStopping watch mode...")
312
- return 0
313
- except Exception as e:
314
- watcher.stop()
315
- print(f"Error: {e}")
316
- return 1
317
- finally:
318
- watcher.stop()
319
- if compile_server:
320
- compile_server.stop()
321
- if browser_proc:
322
- browser_proc.kill()
323
-
324
-
325
- def run_server(args: argparse.Namespace) -> int:
326
- interactive = args.interactive
327
- compile_server = CompileServer(interactive=interactive)
328
- if not interactive:
329
- print(f"Server started at {compile_server.url()}")
330
- compile_server.wait_for_startup()
331
- try:
332
- while True:
333
- if not compile_server.proceess_running():
334
- print("Server process is not running. Exiting...")
335
- return 1
336
- time.sleep(1)
337
- except KeyboardInterrupt:
338
- print("\nExiting from server...")
339
- return 1
340
- finally:
341
- compile_server.stop()
342
- return 0
343
-
344
-
345
- def main() -> int:
346
- args = parse_args()
347
- target_dir = Path(args.directory)
348
- cwd_is_target_dir = target_dir == Path(os.getcwd())
349
- force_server = cwd_is_target_dir and looks_like_fastled_repo(target_dir)
350
- auto_server = (args.server or args.interactive or cwd_is_target_dir) and (
351
- not args.web and not args.just_compile
352
- )
353
- if auto_server or force_server:
354
- print("Running in server only mode.")
355
- return run_server(args)
356
- else:
357
- print("Running in client/server mode.")
358
- return run_client(args)
359
-
360
-
361
- if __name__ == "__main__":
362
- try:
363
- project_root = Path(__file__).resolve().parent.parent.parent
364
- print(f"Project root: {project_root}")
365
- os.chdir(project_root)
366
- os.chdir("../fastled")
367
- sys.argv.append("examples/wasm")
368
- sys.argv.append("--server")
369
- sys.argv.append("--interactive")
370
- sys.exit(main())
371
- except KeyboardInterrupt:
372
- print("\nExiting from main...")
373
- sys.exit(1)
374
- except Exception as e:
375
- print(f"Error: {e}")
376
- sys.exit(1)
1
+ """
2
+ Uses the latest wasm compiler image to compile the FastLED sketch.
3
+ """
4
+
5
+ import argparse
6
+ import os
7
+ import platform
8
+ import shutil
9
+ import subprocess
10
+ import sys
11
+ import tempfile
12
+ import time
13
+ from dataclasses import dataclass
14
+ from pathlib import Path
15
+
16
+ from fastled import __version__
17
+ from fastled.build_mode import BuildMode, get_build_mode
18
+ from fastled.compile_server import CompileServer
19
+ from fastled.docker_manager import DockerManager
20
+ from fastled.filewatcher import create_file_watcher_process
21
+ from fastled.open_browser import open_browser_thread
22
+ from fastled.sketch import looks_like_sketch_directory
23
+ from fastled.web_compile import web_compile
24
+
25
+ machine = platform.machine().lower()
26
+ IS_ARM: bool = "arm" in machine or "aarch64" in machine
27
+ PLATFORM_TAG: str = "-arm64" if IS_ARM else ""
28
+ CONTAINER_NAME = f"fastled-wasm-compiler{PLATFORM_TAG}"
29
+ DEFAULT_URL = "https://fastled.onrender.com"
30
+
31
+
32
+ @dataclass
33
+ class CompiledResult:
34
+ """Dataclass to hold the result of the compilation."""
35
+
36
+ success: bool
37
+ fastled_js: str
38
+ hash_value: str | None
39
+
40
+
41
+ DOCKER = DockerManager(container_name=CONTAINER_NAME)
42
+
43
+
44
+ def parse_args() -> argparse.Namespace:
45
+ """Parse command-line arguments."""
46
+ parser = argparse.ArgumentParser(description=f"FastLED WASM Compiler {__version__}")
47
+ parser.add_argument(
48
+ "--version", action="version", version=f"%(prog)s {__version__}"
49
+ )
50
+ parser.add_argument(
51
+ "directory",
52
+ type=str,
53
+ nargs="?",
54
+ default=None,
55
+ help="Directory containing the FastLED sketch to compile",
56
+ )
57
+ parser.add_argument(
58
+ "--just-compile",
59
+ action="store_true",
60
+ help="Just compile, skip opening the browser and watching for changes.",
61
+ )
62
+ parser.add_argument(
63
+ "--web",
64
+ "-w",
65
+ type=str,
66
+ nargs="?",
67
+ # const does not seem to be working as expected
68
+ const=DEFAULT_URL, # Default value when --web is specified without value
69
+ help="Use web compiler. Optional URL can be provided (default: https://fastled.onrender.com)",
70
+ )
71
+ parser.add_argument(
72
+ "-i",
73
+ "--interactive",
74
+ action="store_true",
75
+ help="Run in interactive mode (Not available with --web)",
76
+ )
77
+ parser.add_argument(
78
+ "--profile",
79
+ action="store_true",
80
+ help="Enable profiling for web compilation",
81
+ )
82
+ build_mode = parser.add_mutually_exclusive_group()
83
+ build_mode.add_argument("--debug", action="store_true", help="Build in debug mode")
84
+ build_mode.add_argument(
85
+ "--quick",
86
+ action="store_true",
87
+ default=True,
88
+ help="Build in quick mode (default)",
89
+ )
90
+ build_mode.add_argument(
91
+ "--release", action="store_true", help="Build in release mode"
92
+ )
93
+ build_mode.add_argument(
94
+ "--localhost",
95
+ action="store_true",
96
+ help="Use localhost for web compilation from an instance of fastled --server",
97
+ )
98
+ build_mode.add_argument(
99
+ "--server",
100
+ action="store_true",
101
+ help="Run the server in the current directory, volume mapping fastled if we are in the repo",
102
+ )
103
+
104
+ build_mode.add_argument(
105
+ "--force-compile",
106
+ action="store_true",
107
+ help="Skips the test to see if the current directory is a valid FastLED sketch directory",
108
+ )
109
+
110
+ args = parser.parse_args()
111
+ if args.localhost:
112
+ args.web = "localhost"
113
+ if args.web is not None:
114
+ args.web = args.web if args.web == "" else args.web
115
+ if args.server and args.web:
116
+ parser.error("--server and --web are mutually exclusive")
117
+ if args.directory is None and not args.server:
118
+ # does current directory look like a sketch?
119
+ maybe_sketch_dir = Path(os.getcwd())
120
+ if looks_like_sketch_directory(maybe_sketch_dir):
121
+ args.directory = str(maybe_sketch_dir)
122
+ else:
123
+ print(
124
+ "\nYou either need to specify a sketch directory or run in --server mode."
125
+ )
126
+ sys.exit(1)
127
+ return args
128
+
129
+
130
+ def run_web_compiler(
131
+ directory: Path,
132
+ host: str,
133
+ build_mode: BuildMode,
134
+ profile: bool,
135
+ last_hash_value: str | None,
136
+ ) -> CompiledResult:
137
+ input_dir = Path(directory)
138
+ output_dir = input_dir / "fastled_js"
139
+ start = time.time()
140
+ web_result = web_compile(
141
+ directory=input_dir, host=host, build_mode=build_mode, profile=profile
142
+ )
143
+ diff = time.time() - start
144
+ if not web_result.success:
145
+ print("\nWeb compilation failed:")
146
+ print(f"Time taken: {diff:.2f} seconds")
147
+ print(web_result.stdout)
148
+ return CompiledResult(success=False, fastled_js="", hash_value=None)
149
+
150
+ def print_results() -> None:
151
+ hash_value = (
152
+ web_result.hash_value
153
+ if web_result.hash_value is not None
154
+ else "NO HASH VALUE"
155
+ )
156
+ print(
157
+ f"\nWeb compilation successful\n Time: {diff:.2f}\n output: {output_dir}\n hash: {hash_value}\n zip size: {len(web_result.zip_bytes)} bytes"
158
+ )
159
+
160
+ # now check to see if the hash value is the same as the last hash value
161
+ if last_hash_value is not None and last_hash_value == web_result.hash_value:
162
+ print("\nSkipping redeploy: No significant changes found.")
163
+ print_results()
164
+ return CompiledResult(
165
+ success=True, fastled_js=str(output_dir), hash_value=web_result.hash_value
166
+ )
167
+
168
+ # Extract zip contents to fastled_js directory
169
+ output_dir.mkdir(exist_ok=True)
170
+ with tempfile.TemporaryDirectory() as temp_dir:
171
+ temp_path = Path(temp_dir)
172
+ temp_zip = temp_path / "result.zip"
173
+ temp_zip.write_bytes(web_result.zip_bytes)
174
+
175
+ # Clear existing contents
176
+ shutil.rmtree(output_dir, ignore_errors=True)
177
+ output_dir.mkdir(exist_ok=True)
178
+
179
+ # Extract zip contents
180
+ shutil.unpack_archive(temp_zip, output_dir, "zip")
181
+
182
+ print(web_result.stdout)
183
+ print_results()
184
+ return CompiledResult(
185
+ success=True, fastled_js=str(output_dir), hash_value=web_result.hash_value
186
+ )
187
+
188
+
189
+ def _try_start_server_or_get_url(args: argparse.Namespace) -> str | CompileServer:
190
+ if args.web:
191
+ if isinstance(args.web, str):
192
+ return args.web
193
+ if isinstance(args.web, bool):
194
+ return DEFAULT_URL
195
+ return args.web
196
+ else:
197
+ try:
198
+ compile_server = CompileServer()
199
+ print("Waiting for the local compiler to start...")
200
+ if not compile_server.wait_for_startup():
201
+ print("Failed to start local compiler.")
202
+ raise RuntimeError("Failed to start local compiler.")
203
+ return compile_server
204
+ except KeyboardInterrupt:
205
+ raise
206
+ except RuntimeError:
207
+ print("Failed to start local compile server, using web compiler instead.")
208
+ return DEFAULT_URL
209
+
210
+
211
+ def run_client(args: argparse.Namespace) -> int:
212
+ compile_server: CompileServer | None = None
213
+ open_web_browser = not args.just_compile
214
+ profile = args.profile
215
+ if not args.force_compile and not looks_like_sketch_directory(Path(args.directory)):
216
+ print(
217
+ "Error: Not a valid FastLED sketch directory, if you are sure it is, use --force-compile"
218
+ )
219
+ return 1
220
+
221
+ # If not explicitly using web compiler, check Docker installation
222
+ if not args.web and not DOCKER.is_docker_installed():
223
+ print(
224
+ "\nDocker is not installed on this system - switching to web compiler instead."
225
+ )
226
+ args.web = True
227
+
228
+ url: str
229
+ try:
230
+ try:
231
+ url_or_server: str | CompileServer = _try_start_server_or_get_url(args)
232
+ if isinstance(url_or_server, str):
233
+ print(f"Found URL: {url_or_server}")
234
+ url = url_or_server
235
+ else:
236
+ compile_server = url_or_server
237
+ print(f"Server started at {compile_server.url()}")
238
+ url = compile_server.url()
239
+ except KeyboardInterrupt:
240
+ print("\nExiting from first try...")
241
+ if compile_server:
242
+ compile_server.stop()
243
+ return 1
244
+ except Exception as e:
245
+ print(f"Error: {e}")
246
+ return 1
247
+ build_mode: BuildMode = get_build_mode(args)
248
+
249
+ def compile_function(
250
+ url: str = url,
251
+ build_mode: BuildMode = build_mode,
252
+ profile: bool = profile,
253
+ last_hash_value: str | None = None,
254
+ ) -> CompiledResult:
255
+ return run_web_compiler(
256
+ args.directory,
257
+ host=url,
258
+ build_mode=build_mode,
259
+ profile=profile,
260
+ last_hash_value=last_hash_value,
261
+ )
262
+
263
+ result: CompiledResult = compile_function(last_hash_value=None)
264
+ last_compiled_result: CompiledResult = result
265
+
266
+ if not result.success:
267
+ print("\nCompilation failed.")
268
+ return 1
269
+
270
+ browser_proc: subprocess.Popen | None = None
271
+ if open_web_browser:
272
+ browser_proc = open_browser_thread(Path(args.directory) / "fastled_js")
273
+ else:
274
+ print(
275
+ "\nCompilation successful. Run without --just-compile to open in browser and watch for changes."
276
+ )
277
+ if compile_server:
278
+ print("Shutting down compile server...")
279
+ compile_server.stop()
280
+ return 0
281
+
282
+ if args.just_compile:
283
+ if compile_server:
284
+ compile_server.stop()
285
+ if browser_proc:
286
+ browser_proc.kill()
287
+ return 0 if result.success else 1
288
+ except KeyboardInterrupt:
289
+ print("\nExiting from main")
290
+ if compile_server:
291
+ compile_server.stop()
292
+ return 1
293
+
294
+ # Watch mode
295
+ print("\nWatching for changes. Press Ctrl+C to stop...")
296
+ # watcher = FileChangedNotifier(args.directory, excluded_patterns=["fastled_js"])
297
+ # watcher.start()
298
+
299
+ from multiprocessing import Process, Queue
300
+
301
+ proc: Process
302
+ queue: Queue
303
+ proc, queue = create_file_watcher_process(
304
+ args.directory, excluded_patterns=["fastled_js"]
305
+ )
306
+
307
+ try:
308
+ while True:
309
+ try:
310
+ size = queue.qsize()
311
+ changed_files = []
312
+ for i in range(size):
313
+ changed_files.append(queue.get())
314
+ except KeyboardInterrupt:
315
+ print("\nExiting from watcher...")
316
+ raise
317
+ except Exception as e:
318
+ print(f"Error getting changes: {e}")
319
+ changed_files = []
320
+ if changed_files:
321
+ print(f"\nChanges detected in {changed_files}")
322
+ last_hash_value = last_compiled_result.hash_value
323
+ result = compile_function(last_hash_value=last_hash_value)
324
+ if not result.success:
325
+ print("\nRecompilation failed.")
326
+ else:
327
+ print("\nRecompilation successful.")
328
+ time.sleep(0.3)
329
+ except KeyboardInterrupt:
330
+ print("\nStopping watch mode...")
331
+ return 0
332
+ except Exception as e:
333
+ print(f"Error: {e}")
334
+ return 1
335
+ finally:
336
+ proc.terminate()
337
+ if compile_server:
338
+ compile_server.stop()
339
+ if browser_proc:
340
+ browser_proc.kill()
341
+
342
+
343
+ def run_server(args: argparse.Namespace) -> int:
344
+ interactive = args.interactive
345
+ compile_server = CompileServer(interactive=interactive)
346
+ if not interactive:
347
+ print(f"Server started at {compile_server.url()}")
348
+ compile_server.wait_for_startup()
349
+ try:
350
+ while True:
351
+ if not compile_server.proceess_running():
352
+ print("Server process is not running. Exiting...")
353
+ return 1
354
+ time.sleep(1)
355
+ except KeyboardInterrupt:
356
+ print("\nExiting from server...")
357
+ return 1
358
+ finally:
359
+ compile_server.stop()
360
+ return 0
361
+
362
+
363
+ def main() -> int:
364
+ args = parse_args()
365
+ if args.server:
366
+ print("Running in server only mode.")
367
+ return run_server(args)
368
+ else:
369
+ print("Running in client/server mode.")
370
+ return run_client(args)
371
+
372
+
373
+ if __name__ == "__main__":
374
+ try:
375
+ sys.argv.append("examples/SdCard")
376
+ sys.argv.append("--local")
377
+ sys.exit(main())
378
+ except KeyboardInterrupt:
379
+ print("\nExiting from main...")
380
+ sys.exit(1)
381
+ except Exception as e:
382
+ print(f"Error: {e}")
383
+ sys.exit(1)