fastled 1.1.15__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 CHANGED
@@ -1,3 +1,3 @@
1
1
  """FastLED Wasm Compiler package."""
2
2
 
3
- __version__ = "1.1.15"
3
+ __version__ = "1.1.16"
fastled/app.py CHANGED
@@ -44,9 +44,6 @@ class CompiledResult:
44
44
  hash_value: str | None
45
45
 
46
46
 
47
- DOCKER = DockerManager(container_name=CONTAINER_NAME)
48
-
49
-
50
47
  def parse_args() -> argparse.Namespace:
51
48
  """Parse command-line arguments."""
52
49
  parser = argparse.ArgumentParser(description=f"FastLED WASM Compiler {__version__}")
@@ -252,7 +249,7 @@ def run_client(args: argparse.Namespace) -> int:
252
249
  return 1
253
250
 
254
251
  # If not explicitly using web compiler, check Docker installation
255
- if not args.web and not DOCKER.is_docker_installed():
252
+ if not args.web and not DockerManager.is_docker_installed():
256
253
  print(
257
254
  "\nDocker is not installed on this system - switching to web compiler instead."
258
255
  )
@@ -304,9 +301,7 @@ def run_client(args: argparse.Namespace) -> int:
304
301
  if open_web_browser:
305
302
  browser_proc = open_browser_thread(Path(args.directory) / "fastled_js")
306
303
  else:
307
- print(
308
- "\nCompilation successful. Run without --just-compile to open in browser and watch for changes."
309
- )
304
+ print("\nCompilation successful.")
310
305
  if compile_server:
311
306
  print("Shutting down compile server...")
312
307
  compile_server.stop()
@@ -432,9 +427,8 @@ def main() -> int:
432
427
 
433
428
  if __name__ == "__main__":
434
429
  try:
435
- # os.chdir("../fastled")
436
- sys.argv.append("examples/SdCard")
437
- sys.argv.append("--localhost")
430
+ os.chdir("../fastled")
431
+ sys.argv.append("--server")
438
432
  sys.exit(main())
439
433
  except KeyboardInterrupt:
440
434
  print("\nExiting from main...")
fastled/compile_server.py CHANGED
@@ -1,13 +1,13 @@
1
- import socket
2
1
  import subprocess
3
2
  import time
4
3
  from pathlib import Path
5
4
 
6
5
  import httpx
7
6
 
8
- from fastled.docker_manager import DockerManager
7
+ from fastled.docker_manager import DISK_CACHE, DockerManager, RunningContainer
9
8
  from fastled.sketch import looks_like_fastled_repo
10
9
 
10
+ _IMAGE_NAME = "niteris/fastled-wasm"
11
11
  _DEFAULT_CONTAINER_NAME = "fastled-wasm-compiler"
12
12
 
13
13
  SERVER_PORT = 9021
@@ -15,19 +15,6 @@ SERVER_PORT = 9021
15
15
  SERVER_OPTIONS = ["--allow-shutdown", "--no-auto-update"]
16
16
 
17
17
 
18
- def find_available_port(start_port: int = SERVER_PORT) -> int:
19
- """Find an available port starting from the given port."""
20
- port = start_port
21
- end_port = start_port + 1000
22
- while True:
23
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
24
- if s.connect_ex(("localhost", port)) != 0:
25
- return port
26
- port += 1
27
- if port >= end_port:
28
- raise RuntimeError("No available ports found")
29
-
30
-
31
18
  class CompileServer:
32
19
  def __init__(
33
20
  self,
@@ -44,11 +31,10 @@ class CompileServer:
44
31
  fastled_src_dir = cwd / "src"
45
32
 
46
33
  self.container_name = container_name
47
- self.docker = DockerManager(container_name=container_name)
48
- self.running = False
49
- self.running_process: subprocess.Popen | None = None
34
+ self.docker = DockerManager()
50
35
  self.fastled_src_dir: Path | None = fastled_src_dir
51
36
  self.interactive = interactive
37
+ self.running_container: RunningContainer | None = None
52
38
  self._port = self._start()
53
39
  # fancy print
54
40
  if not interactive:
@@ -57,6 +43,16 @@ class CompileServer:
57
43
  print(msg)
58
44
  print("#" * len(msg) + "\n")
59
45
 
46
+ @property
47
+ def running(self) -> bool:
48
+ if not self._port:
49
+ return False
50
+ if not DockerManager.is_docker_installed():
51
+ return False
52
+ if not DockerManager.is_running():
53
+ return False
54
+ return self.docker.is_container_running(self.container_name)
55
+
60
56
  def using_fastled_src_dir_volume(self) -> bool:
61
57
  return self.fastled_src_dir is not None
62
58
 
@@ -86,56 +82,44 @@ class CompileServer:
86
82
  except Exception:
87
83
  pass
88
84
  time.sleep(0.1)
89
- if not self.running:
85
+ if not self.docker.is_container_running(self.container_name):
90
86
  return False
91
87
  return False
92
88
 
93
89
  def _start(self) -> int:
94
90
  print("Compiling server starting")
95
- self.running = True
91
+
96
92
  # Ensure Docker is running
97
93
  with self.docker.get_lock():
98
94
  if not self.docker.is_running():
99
95
  if not self.docker.start():
100
96
  print("Docker could not be started. Exiting.")
101
97
  raise RuntimeError("Docker could not be started. Exiting.")
98
+ from datetime import datetime, timezone
102
99
 
103
- # Clean up any existing container with the same name
104
- try:
105
- container_exists = (
106
- subprocess.run(
107
- ["docker", "inspect", self.container_name],
108
- capture_output=True,
109
- text=True,
110
- ).returncode
111
- == 0
112
- )
113
- if container_exists:
114
- print("Cleaning up existing container")
115
- subprocess.run(
116
- ["docker", "rm", "-f", self.container_name],
117
- check=False,
118
- )
119
- except KeyboardInterrupt:
120
- raise
121
- except Exception as e:
122
- print(f"Warning: Failed to remove existing container: {e}")
100
+ now = datetime.now(timezone.utc)
101
+ now_str = now.strftime("%Y-%m-%d %H %Z")
102
+ prev_date_str = DISK_CACHE.get("last-update")
103
+
104
+ upgrade = False
105
+ if prev_date_str != now_str:
106
+ print("New day, upgrading Docker image")
107
+ upgrade = True
123
108
 
124
- print("Ensuring Docker image exists at latest version")
125
- if not self.docker.ensure_image_exists():
126
- print("Failed to ensure Docker image exists.")
127
- raise RuntimeError("Failed to ensure Docker image exists")
109
+ self.docker.validate_or_download_image(
110
+ image_name=_IMAGE_NAME, tag="main", upgrade=upgrade
111
+ )
112
+ DISK_CACHE.put("last-update", now_str)
128
113
 
129
114
  print("Docker image now validated")
130
- port = find_available_port()
131
- print(f"Found an available port: {port}")
115
+ port = SERVER_PORT
132
116
  if self.interactive:
133
117
  server_command = ["/bin/bash"]
134
118
  else:
135
119
  server_command = ["python", "/js/run.py", "server"] + SERVER_OPTIONS
136
120
  server_cmd_str = subprocess.list2cmdline(server_command)
137
121
  print(f"Started Docker container with command: {server_cmd_str}")
138
- ports = {port: 80}
122
+ ports = {80: port}
139
123
  volumes = None
140
124
  if self.fastled_src_dir:
141
125
  print(
@@ -144,78 +128,29 @@ class CompileServer:
144
128
  volumes = {
145
129
  str(self.fastled_src_dir): {"bind": "/host/fastled/src", "mode": "ro"}
146
130
  }
147
- self.running_process = self.docker.run_container(
148
- server_command, ports=ports, volumes=volumes, tty=self.interactive
131
+
132
+ cmd_str = subprocess.list2cmdline(server_command)
133
+
134
+ self.docker.run_container(
135
+ image_name=_IMAGE_NAME,
136
+ tag="main",
137
+ container_name=self.container_name,
138
+ command=cmd_str,
139
+ ports=ports,
140
+ volumes=volumes,
149
141
  )
142
+ self.running_container = self.docker.attach_and_run(self.container_name)
143
+ assert self.running_container is not None, "Container should be running"
144
+
150
145
  print("Compile server starting")
151
- time.sleep(3)
152
- if self.running_process.poll() is not None:
153
- print("Server failed to start")
154
- self.running = False
155
- raise RuntimeError("Server failed to start")
156
146
  return port
157
147
 
158
148
  def proceess_running(self) -> bool:
159
- if self.running_process is None:
160
- return False
161
- return self.running_process.poll() is None
149
+ return self.docker.is_container_running(self.container_name)
162
150
 
163
151
  def stop(self) -> None:
164
- print(f"Stopping server on port {self._port}")
165
- # # attempt to send a shutdown signal to the server
166
- # try:
167
- # httpx.get(f"http://localhost:{self._port}/shutdown", timeout=2)
168
- # # except Exception:
169
- # except Exception as e:
170
- # print(f"Failed to send shutdown signal: {e}")
171
- # pass
172
- try:
173
- # Stop the Docker container
174
- cp: subprocess.CompletedProcess
175
- cp = subprocess.run(
176
- ["docker", "stop", self.container_name],
177
- capture_output=True,
178
- text=True,
179
- check=True,
180
- )
181
- if cp.returncode != 0:
182
- print(f"Failed to stop Docker container: {cp.stderr}")
183
-
184
- cp = subprocess.run(
185
- ["docker", "rm", self.container_name],
186
- capture_output=True,
187
- text=True,
188
- check=True,
189
- )
190
- if cp.returncode != 0:
191
- print(f"Failed to remove Docker container: {cp.stderr}")
192
-
193
- # Close the stdout pipe
194
- if self.running_process and self.running_process.stdout:
195
- self.running_process.stdout.close()
196
-
197
- # Wait for the process to fully terminate with a timeout
198
- self.running_process.wait(timeout=10)
199
- if self.running_process.returncode is None:
200
- # kill
201
- self.running_process.kill()
202
- if self.running_process.returncode is not None:
203
- print(
204
- f"Server stopped with return code {self.running_process.returncode}"
205
- )
206
-
207
- except subprocess.TimeoutExpired:
208
- # Force kill if it doesn't stop gracefully
209
- if self.running_process:
210
- self.running_process.kill()
211
- self.running_process.wait()
212
- except KeyboardInterrupt:
213
- if self.running_process:
214
- self.running_process.kill()
215
- self.running_process.wait()
216
- except Exception as e:
217
- print(f"Error stopping Docker container: {e}")
218
- finally:
219
- self.running_process = None
220
- self.running = False
152
+ # print(f"Stopping server on port {self._port}")
153
+ if self.running_container:
154
+ self.running_container.stop()
155
+ self.docker.suspend_container(self.container_name)
221
156
  print("Compile server stopped")
fastled/docker_manager.py CHANGED
@@ -1,17 +1,36 @@
1
- """Docker management functionality for FastLED WASM compiler."""
1
+ """
2
+ New abstraction for Docker management with improved Ctrl+C handling.
3
+ """
2
4
 
5
+ import _thread
3
6
  import subprocess
4
7
  import sys
8
+ import threading
5
9
  import time
10
+ import traceback
11
+ import warnings
12
+ from datetime import datetime, timezone
6
13
  from pathlib import Path
7
14
 
8
- import docker # type: ignore
15
+ import docker
16
+ from appdirs import user_data_dir
17
+ from disklru import DiskLRUCache
18
+ from docker.client import DockerClient
19
+ from docker.models.containers import Container
20
+ from docker.models.images import Image
9
21
  from filelock import FileLock
10
22
 
11
- TAG = "main"
23
+ CONFIG_DIR = Path(user_data_dir("fastled", "fastled"))
24
+ CONFIG_DIR.mkdir(parents=True, exist_ok=True)
25
+ DB_FILE = CONFIG_DIR / "db.db"
26
+ DISK_CACHE = DiskLRUCache(str(DB_FILE), 10)
12
27
 
13
- _HERE = Path(__file__).parent
14
- _FILE_LOCK = FileLock(str(_HERE / "fled.lock"))
28
+
29
+ # Docker uses datetimes in UTC but without the timezone info. If we pass in a tz
30
+ # then it will throw an exception.
31
+ def _utc_now_no_tz() -> datetime:
32
+ now = datetime.now(timezone.utc)
33
+ return now.replace(tzinfo=None)
15
34
 
16
35
 
17
36
  def _win32_docker_location() -> str | None:
@@ -26,11 +45,54 @@ def _win32_docker_location() -> str | None:
26
45
  return None
27
46
 
28
47
 
48
+ _HERE = Path(__file__).parent
49
+ _FILE_LOCK = FileLock(str(_HERE / "fled.lock"))
50
+
51
+
52
+ class RunningContainer:
53
+ def __init__(self, container, first_run=False):
54
+ self.container = container
55
+ self.first_run = first_run
56
+ self.running = True
57
+ self.thread = threading.Thread(target=self._log_monitor)
58
+ self.thread.daemon = True
59
+ self.thread.start()
60
+
61
+ def _log_monitor(self):
62
+ from_date = _utc_now_no_tz() if not self.first_run else None
63
+ to_date = _utc_now_no_tz()
64
+
65
+ while self.running:
66
+ try:
67
+ for log in self.container.logs(
68
+ follow=False, since=from_date, until=to_date, stream=True
69
+ ):
70
+ print(log.decode("utf-8"), end="")
71
+ time.sleep(0.1)
72
+ from_date = to_date
73
+ to_date = _utc_now_no_tz()
74
+ except KeyboardInterrupt:
75
+ print("Monitoring logs interrupted by user.")
76
+ _thread.interrupt_main()
77
+ break
78
+ except Exception as e:
79
+ print(f"Error monitoring logs: {e}")
80
+ break
81
+
82
+ def stop(self) -> None:
83
+ """Stop monitoring the container logs"""
84
+ self.running = False
85
+ self.thread.join()
86
+
87
+
29
88
  class DockerManager:
30
- """Manages Docker operations for FastLED WASM compiler."""
89
+ def __init__(self) -> None:
90
+ self.client: DockerClient = docker.from_env()
91
+ self.first_run = False
31
92
 
32
- def __init__(self, container_name: str):
33
- self.container_name = container_name
93
+ def get_lock(self) -> FileLock:
94
+ """Get the file lock for this DockerManager instance."""
95
+ return _FILE_LOCK
34
96
 
35
97
  @staticmethod
36
98
  def is_docker_installed() -> bool:
@@ -46,9 +108,13 @@ class DockerManager:
46
108
  print("Docker is not installed.")
47
109
  return False
48
110
 
49
- def is_running(self) -> bool:
111
+ @staticmethod
112
+ def is_running() -> bool:
50
113
  """Check if Docker is running by pinging the Docker daemon."""
114
+ if not DockerManager.is_docker_installed():
115
+ return False
51
116
  try:
117
+ # self.client.ping()
52
118
  client = docker.from_env()
53
119
  client.ping()
54
120
  print("Docker is running.")
@@ -104,158 +170,344 @@ class DockerManager:
104
170
  print(f"Error starting Docker: {str(e)}")
105
171
  return False
106
172
 
107
- def ensure_linux_containers(self) -> bool:
108
- """Ensure Docker is using Linux containers on Windows."""
109
- if sys.platform == "win32":
110
- try:
111
- # Check if we're already in Linux container mode
112
- result = subprocess.run(
113
- ["docker", "info"], capture_output=True, text=True, check=True
114
- )
115
- if "linux" in result.stdout.lower():
116
- return True
173
+ def validate_or_download_image(
174
+ self, image_name: str, tag: str = "latest", upgrade: bool = False
175
+ ) -> None:
176
+ """
177
+ Validate if the image exists, and if not, download it.
178
+ If upgrade is True, will pull the latest version even if image exists locally.
179
+ """
180
+ print(f"Validating image {image_name}:{tag}...")
117
181
 
118
- print("Switching to Linux containers...")
119
- subprocess.run(
120
- ["cmd", "/c", "docker context ls"], check=True, capture_output=True
121
- )
122
- subprocess.run(
123
- ["cmd", "/c", "docker context use default"],
124
- check=True,
125
- capture_output=True,
182
+ try:
183
+ local_image = self.client.images.get(f"{image_name}:{tag}")
184
+ print(f"Image {image_name}:{tag} is already available.")
185
+
186
+ if upgrade:
187
+ remote_image = self.client.images.get_registry_data(
188
+ f"{image_name}:{tag}"
126
189
  )
127
- return True
128
- except subprocess.CalledProcessError as e:
129
- print(f"Failed to switch to Linux containers: {e}")
130
- print(f"stdout: {e.stdout}")
131
- print(f"stderr: {e.stderr}")
132
- return False
133
- return True # Non-Windows platforms don't need this
190
+ remote_image_hash = remote_image.id
191
+ try:
192
+ remote_image_hash_from_local_image = DISK_CACHE.get(local_image.id)
193
+ except KeyboardInterrupt:
194
+ raise
195
+ except Exception:
196
+ remote_image_hash_from_local_image = None
197
+ import traceback
198
+ import warnings
199
+
200
+ stack = traceback.format_exc()
201
+ warnings.warn(
202
+ f"Error getting remote image hash from local image: {stack}"
203
+ )
204
+ if remote_image_hash_from_local_image == remote_image_hash:
205
+ print(f"Local image {image_name}:{tag} is up to date.")
206
+ return
207
+
208
+ # Quick check for latest version
209
+
210
+ print(f"Pulling newer version of {image_name}:{tag}...")
211
+ _ = self.client.images.pull(image_name, tag=tag)
212
+ print(f"Updated to newer version of {image_name}:{tag}")
213
+ local_image_hash = self.client.images.get(f"{image_name}:{tag}").id
214
+ DISK_CACHE.put(local_image_hash, remote_image_hash)
215
+
216
+ except docker.errors.ImageNotFound:
217
+ print(f"Image {image_name}:{tag} not found. Downloading...")
218
+ self.client.images.pull(image_name, tag=tag)
219
+ try:
220
+ local_image = self.client.images.get(f"{image_name}:{tag}")
221
+ local_image_hash = local_image.id
222
+ DISK_CACHE.put(local_image_hash, remote_image_hash)
223
+ print(f"Image {image_name}:{tag} downloaded successfully.")
224
+ except docker.errors.ImageNotFound:
225
+ import warnings
134
226
 
135
- def get_lock(self) -> FileLock:
136
- """Get the file lock for this DockerManager instance."""
137
- return _FILE_LOCK
227
+ warnings.warn(f"Image {image_name}:{tag} not found after download.")
138
228
 
139
- def ensure_image_exists(self, force_update: bool = False) -> bool:
140
- """Check if local image exists, pull from remote if not or if update requested."""
229
+ def tag_image(self, image_name: str, old_tag: str, new_tag: str) -> None:
230
+ """
231
+ Tag an image with a new tag.
232
+ """
233
+ image: Image = self.client.images.get(f"{image_name}:{old_tag}")
234
+ image.tag(image_name, new_tag)
235
+ print(f"Image {image_name}:{old_tag} tagged as {new_tag}.")
141
236
 
237
+ def _container_configs_match(
238
+ self,
239
+ container: Container,
240
+ command: str | None,
241
+ volumes: dict | None,
242
+ ports: dict | None,
243
+ ) -> bool:
244
+ """Compare if existing container has matching configuration"""
142
245
  try:
143
- if not self.ensure_linux_containers():
144
- return False
246
+ # Check if container is using the same image
247
+ container_image_id = container.image.id
248
+ container_image_tags = container.image.tags
145
249
 
146
- image_name = f"{self.container_name}:{TAG}"
147
- remote_image = f"niteris/fastled-wasm:{TAG}"
148
- print("Pulling latest")
149
- cmd_check_newer_image = [
150
- "docker",
151
- "pull",
152
- remote_image,
153
- ]
154
- result = subprocess.run(
155
- cmd_check_newer_image,
156
- text=True,
157
- check=False,
158
- )
159
- if result.returncode != 0:
160
- print("Failed to check for newer image.")
161
- return False
162
- print("Tagging image")
163
-
164
- tag_result = subprocess.run(
165
- [
166
- "docker",
167
- "tag",
168
- remote_image,
169
- image_name,
170
- ],
171
- capture_output=True,
172
- text=True,
173
- check=False,
174
- )
175
- if tag_result.returncode != 0:
176
- print(f"Failed to tag image: {tag_result.stderr}")
250
+ # Simplified image comparison - just compare the IDs directly
251
+ if not container_image_tags:
252
+ print(f"Container using untagged image with ID: {container_image_id}")
253
+ else:
254
+ current_image = self.client.images.get(container_image_tags[0])
255
+ if container_image_id != current_image.id:
256
+ print(
257
+ f"Container using different image version. Container: {container_image_id}, Current: {current_image.id}"
258
+ )
259
+ return False
260
+
261
+ # Check command if specified
262
+ if command and container.attrs["Config"]["Cmd"] != command.split():
263
+ print(
264
+ f"Command mismatch: {container.attrs['Config']['Cmd']} != {command}"
265
+ )
177
266
  return False
178
- return True
179
- except subprocess.CalledProcessError as e:
180
- print(f"Failed to ensure image exists: {e}")
181
- return False
182
267
 
183
- def container_exists(self) -> bool:
184
- """Check if a container with the given name exists."""
185
- try:
186
- result = subprocess.run(
187
- ["docker", "container", "inspect", self.container_name],
188
- capture_output=True,
189
- check=False,
190
- )
191
- return result.returncode == 0
192
- except subprocess.CalledProcessError:
193
- return False
268
+ # Check volumes if specified
269
+ if volumes:
270
+ container_mounts = (
271
+ {
272
+ m["Source"]: {"bind": m["Destination"], "mode": m["Mode"]}
273
+ for m in container.attrs["Mounts"]
274
+ }
275
+ if container.attrs.get("Mounts")
276
+ else {}
277
+ )
194
278
 
195
- def remove_container(self) -> bool:
196
- """Remove a container if it exists."""
197
- try:
198
- subprocess.run(
199
- ["docker", "rm", "-f", self.container_name],
200
- check=True,
201
- )
202
- return True
203
- except subprocess.CalledProcessError:
204
- return False
279
+ for host_dir, mount in volumes.items():
280
+ if host_dir not in container_mounts:
281
+ print(f"Volume {host_dir} not found in container mounts.")
282
+ return False
283
+ if container_mounts[host_dir] != mount:
284
+ print(
285
+ f"Volume {host_dir} has different mount options: {container_mounts[host_dir]} != {mount}"
286
+ )
287
+ return False
288
+
289
+ # Check ports if specified
290
+ if ports:
291
+ container_ports = (
292
+ container.attrs["Config"]["ExposedPorts"]
293
+ if container.attrs["Config"].get("ExposedPorts")
294
+ else {}
295
+ )
296
+ container_port_bindings = (
297
+ container.attrs["HostConfig"]["PortBindings"]
298
+ if container.attrs["HostConfig"].get("PortBindings")
299
+ else {}
300
+ )
205
301
 
206
- def full_container_name(self) -> str:
207
- """Get the name of the container."""
208
- return f"{self.container_name}:{TAG}"
302
+ for container_port, host_port in ports.items():
303
+ port_key = f"{container_port}/tcp"
304
+ if port_key not in container_ports:
305
+ print(f"Container port {port_key} not found.")
306
+ return False
307
+ if not container_port_bindings.get(port_key, [{"HostPort": None}])[
308
+ 0
309
+ ]["HostPort"] == str(host_port):
310
+ print(f"Port {host_port} is not bound to {port_key}.")
311
+ return False
312
+ except KeyboardInterrupt:
313
+ raise
314
+ except docker.errors.NotFound:
315
+ print("Container not found.")
316
+ return False
317
+ except Exception as e:
318
+ stack = traceback.format_exc()
319
+ warnings.warn(f"Error checking container config: {e}\n{stack}")
320
+ return False
321
+ return True
209
322
 
210
323
  def run_container(
211
324
  self,
212
- cmd: list[str],
325
+ image_name: str,
326
+ tag: str,
327
+ container_name: str,
328
+ command: str | None = None,
213
329
  volumes: dict[str, dict[str, str]] | None = None,
214
330
  ports: dict[int, int] | None = None,
215
- tty: bool = False,
216
- ) -> subprocess.Popen:
217
- """Run the Docker container with the specified volume.
331
+ ) -> Container:
332
+ """
333
+ Run a container from an image. If it already exists with matching config, start it.
334
+ If it exists with different config, remove and recreate it.
218
335
 
219
336
  Args:
220
- cmd: Command to run in the container
221
337
  volumes: Dict mapping host paths to dicts with 'bind' and 'mode' keys
338
+ Example: {'/host/path': {'bind': '/container/path', 'mode': 'rw'}}
222
339
  ports: Dict mapping host ports to container ports
340
+ Example: {8080: 80} maps host port 8080 to container port 80
223
341
  """
224
- volumes = volumes or {}
225
- ports = ports or {}
226
-
227
- print("Creating new container...")
228
- docker_command = ["docker", "run"]
229
-
230
- if tty:
231
- assert sys.stdout.isatty(), "TTY mode requires a TTY"
232
- docker_command.append("-it")
233
- # Attach volumes if specified
234
- docker_command += [
235
- "--name",
236
- self.container_name,
237
- ]
238
- if ports:
239
- for host_port, container_port in ports.items():
240
- docker_command.extend(["-p", f"{host_port}:{container_port}"])
241
- if volumes:
242
- for host_path, mount_spec in volumes.items():
243
- docker_command.extend(
244
- ["-v", f"{host_path}:{mount_spec['bind']}:{mount_spec['mode']}"]
342
+ image_name = f"{image_name}:{tag}"
343
+ try:
344
+ container: Container = self.client.containers.get(container_name)
345
+
346
+ # Check if configuration matches
347
+ if not self._container_configs_match(container, command, volumes, ports):
348
+ print(
349
+ f"Container {container_name} exists but with different configuration. Removing and recreating..."
245
350
  )
351
+ container.remove(force=True)
352
+ raise docker.errors.NotFound("Container removed due to config mismatch")
353
+ print(f"Container {container_name} found with matching configuration.")
354
+
355
+ # Existing container with matching config - handle various states
356
+ if container.status == "running":
357
+ print(f"Container {container_name} is already running.")
358
+ elif container.status == "exited":
359
+ print(f"Starting existing container {container_name}.")
360
+ container.start()
361
+ elif container.status == "restarting":
362
+ print(f"Waiting for container {container_name} to restart...")
363
+ timeout = 10
364
+ container.wait(timeout=10)
365
+ if container.status == "running":
366
+ print(f"Container {container_name} has restarted.")
367
+ else:
368
+ print(
369
+ f"Container {container_name} did not restart within {timeout} seconds."
370
+ )
371
+ container.stop()
372
+ print(f"Container {container_name} has been stopped.")
373
+ container.start()
374
+ elif container.status == "paused":
375
+ print(f"Resuming existing container {container_name}.")
376
+ container.unpause()
377
+ else:
378
+ print(f"Unknown container status: {container.status}")
379
+ print(f"Starting existing container {container_name}.")
380
+ self.first_run = True
381
+ container.start()
382
+ except docker.errors.NotFound:
383
+ print(f"Creating and starting {container_name}")
384
+ container = self.client.containers.run(
385
+ image_name,
386
+ command,
387
+ name=container_name,
388
+ detach=True,
389
+ tty=True,
390
+ volumes=volumes,
391
+ ports=ports,
392
+ )
393
+ return container
246
394
 
247
- docker_command.extend(
248
- [
249
- f"{self.container_name}:{TAG}",
250
- ]
251
- )
252
- docker_command.extend(cmd)
395
+ def attach_and_run(self, container: Container | str) -> RunningContainer:
396
+ """
397
+ Attach to a running container and monitor its logs in a background thread.
398
+ Returns a RunningContainer object that can be used to stop monitoring.
399
+ """
400
+ if isinstance(container, str):
401
+ container = self.get_container(container)
402
+
403
+ print(f"Attaching to container {container.name}...")
404
+
405
+ first_run = self.first_run
406
+ self.first_run = False
407
+
408
+ return RunningContainer(container, first_run)
409
+
410
+ def suspend_container(self, container: Container | str) -> None:
411
+ """
412
+ Suspend (pause) the container.
413
+ """
414
+ if isinstance(container, str):
415
+ container_name = container
416
+ container = self.get_container(container)
417
+ if not container:
418
+ print(f"Could not put container {container_name} to sleep.")
419
+ return
420
+ try:
421
+ container.pause()
422
+ print(f"Container {container.name} has been suspended.")
423
+ except KeyboardInterrupt:
424
+ print(f"Container {container.name} interrupted by keyboard interrupt.")
425
+ except Exception as e:
426
+ print(f"Failed to suspend container {container.name}: {e}")
427
+
428
+ def resume_container(self, container: Container | str) -> None:
429
+ """
430
+ Resume (unpause) the container.
431
+ """
432
+ if isinstance(container, str):
433
+ container = self.get_container(container)
434
+ try:
435
+ container.unpause()
436
+ print(f"Container {container.name} has been resumed.")
437
+ except Exception as e:
438
+ print(f"Failed to resume container {container.name}: {e}")
439
+
440
+ def get_container(self, container_name: str) -> Container:
441
+ """
442
+ Get a container by name.
443
+ """
444
+ try:
445
+ return self.client.containers.get(container_name)
446
+ except docker.errors.NotFound:
447
+ print(f"Container {container_name} not found.")
448
+ raise
449
+
450
+ def is_container_running(self, container_name: str) -> bool:
451
+ """
452
+ Check if a container is running.
453
+ """
454
+ try:
455
+ container = self.client.containers.get(container_name)
456
+ return container.status == "running"
457
+ except docker.errors.NotFound:
458
+ print(f"Container {container_name} not found.")
459
+ return False
460
+
461
+
462
+ def main():
463
+ # Register SIGINT handler
464
+ # signal.signal(signal.SIGINT, handle_sigint)
465
+
466
+ docker_manager = DockerManager()
253
467
 
254
- print(f"Running command: {' '.join(docker_command)}")
468
+ # Parameters
469
+ image_name = "python"
470
+ tag = "3.10-slim"
471
+ # new_tag = "my-python"
472
+ container_name = "my-python-container"
473
+ command = "python -m http.server"
255
474
 
256
- process = subprocess.Popen(
257
- docker_command,
258
- text=True,
475
+ try:
476
+ # Step 1: Validate or download the image
477
+ docker_manager.validate_or_download_image(image_name, tag, upgrade=True)
478
+
479
+ # Step 2: Tag the image
480
+ # docker_manager.tag_image(image_name, tag, new_tag)
481
+
482
+ # Step 3: Run the container
483
+ container = docker_manager.run_container(
484
+ image_name, tag, container_name, command
259
485
  )
260
486
 
261
- return process
487
+ # Step 4: Attach and monitor the container logs
488
+ running_container = docker_manager.attach_and_run(container)
489
+
490
+ # Wait for keyboard interrupt
491
+ while True:
492
+ time.sleep(0.1)
493
+
494
+ except KeyboardInterrupt:
495
+ print("\nStopping container...")
496
+ running_container.stop()
497
+ container = docker_manager.get_container(container_name)
498
+ docker_manager.suspend_container(container)
499
+
500
+ try:
501
+ # Suspend and resume the container
502
+ container = docker_manager.get_container(container_name)
503
+ docker_manager.suspend_container(container)
504
+
505
+ input("Press Enter to resume the container...")
506
+
507
+ docker_manager.resume_container(container)
508
+ except Exception as e:
509
+ print(f"An error occurred: {e}")
510
+
511
+
512
+ if __name__ == "__main__":
513
+ main()
fastled/web_compile.py CHANGED
@@ -5,7 +5,7 @@ import os
5
5
  import shutil
6
6
  import tempfile
7
7
  import zipfile
8
- from concurrent.futures import Future, ProcessPoolExecutor, as_completed
8
+ from concurrent.futures import Future, ThreadPoolExecutor, as_completed
9
9
  from dataclasses import dataclass
10
10
  from pathlib import Path
11
11
 
@@ -21,7 +21,7 @@ ENDPOINT_COMPILED_WASM = "compile/wasm"
21
21
  _TIMEOUT = 60 * 4 # 2 mins timeout
22
22
  _AUTH_TOKEN = "oBOT5jbsO4ztgrpNsQwlmFLIKB"
23
23
  ENABLE_EMBEDDED_DATA = True
24
- _EXECUTOR = ProcessPoolExecutor(max_workers=8)
24
+ _EXECUTOR = ThreadPoolExecutor(max_workers=8)
25
25
 
26
26
 
27
27
  @dataclass
@@ -66,16 +66,11 @@ def _test_connection(host: str, use_ipv4: bool) -> ConnectionResult:
66
66
  )
67
67
  result = ConnectionResult(host, test_response.status_code == 200, use_ipv4)
68
68
  except KeyboardInterrupt:
69
- # main thraed interrupt
70
-
71
69
  _thread.interrupt_main()
72
70
 
73
71
  except TimeoutError:
74
72
  result = ConnectionResult(host, False, use_ipv4)
75
- except Exception as e:
76
- import warnings
77
-
78
- warnings.warn(f"Connection failed: {e}")
73
+ except Exception:
79
74
  result = ConnectionResult(host, False, use_ipv4)
80
75
  return result
81
76
 
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fastled
3
- Version: 1.1.15
3
+ Version: 1.1.16
4
4
  Summary: FastLED Wasm Compiler
5
5
  Home-page: https://github.com/zackees/fastled-wasm
6
6
  Maintainer: Zachary Vorhies
7
7
  License: BSD 3-Clause License
8
8
  Keywords: template-python-cmd
9
9
  Classifier: Programming Language :: Python :: 3
10
- Requires-Python: >=3.7
10
+ Requires-Python: >=3.9
11
11
  Description-Content-Type: text/markdown
12
12
  License-File: LICENSE
13
13
  Requires-Dist: docker
@@ -16,6 +16,8 @@ Requires-Dist: watchdog
16
16
  Requires-Dist: livereload
17
17
  Requires-Dist: download
18
18
  Requires-Dist: filelock
19
+ Requires-Dist: disklru>=2.0.1
20
+ Requires-Dist: appdirs
19
21
 
20
22
  # FastLED Wasm compiler
21
23
 
@@ -159,6 +161,7 @@ A: A big chunk of space is being used by unnecessary javascript `emscripten` is
159
161
 
160
162
  # Revisions
161
163
 
164
+ * 1.1.16 - Rewrote docker logic to use container suspension and resumption. Much much faster.
162
165
  * 1.1.15 - Fixed logic for considering ipv6 addresses. Auto selection of ipv6 is now restored.
163
166
  * 1.1.14 - Fixes for regression in using --server and --localhost as two instances, this is now under test.
164
167
  * 1.1.13 - Disable the use of ipv6. It produces random timeouts on the onrender server we are using for the web compiler.
@@ -1,20 +1,20 @@
1
- fastled/__init__.py,sha256=2BNTyVWziwf2g_946IKK7hNoB3ufZnDa9odeUM0JvlA,64
2
- fastled/app.py,sha256=7qjfLbqSlLVmkLifHaEkRAXo0ZRiJFiBCDvAxeOJX8Y,15290
1
+ fastled/__init__.py,sha256=lSQZ1Dcii_FVM8VSJksjq9KpLmMsNl8jlUnsYp7Ko7I,64
2
+ fastled/app.py,sha256=5ZKCN8XwcK6wfbp7hQRg-hUkZOSMJgmiTksQ8oX2rME,15094
3
3
  fastled/build_mode.py,sha256=joMwsV4K1y_LijT4gEAcjx69RZBoe_KmFmHZdPYbL_4,631
4
4
  fastled/cli.py,sha256=CNR_pQR0sNVPNuv8e_nmm-0PI8sU-eUBUgnWgWkzW9c,237
5
- fastled/compile_server.py,sha256=baXSd8V3C2tA--oYl8-_TPEvJxtNYck788W-iT-xyi8,8022
6
- fastled/docker_manager.py,sha256=gaDZpFV7E-9JhYIn6ahkP--9dGT32-WR5wZaU-o--6g,9107
5
+ fastled/compile_server.py,sha256=IKdW82dswZSWrp8oCH4khAMYqz6Vf_65m1ad8m2aSHA,5304
6
+ fastled/docker_manager.py,sha256=m09qpT5GnmofPGkPIV6KDJWYduRdRZhQAntBpJoV-24,19722
7
7
  fastled/filewatcher.py,sha256=fJNMQRDCpihSL4nQeYPqbD4m1Jzjcz_-YRAo-wlPW6k,6518
8
8
  fastled/keyboard.py,sha256=rqndglWYzRy6oiqHgsmx1peLd0Yrpci01zGENlCzh_s,2576
9
9
  fastled/open_browser.py,sha256=RRHcsZ5Vzsw1AuZUEYuSfjKmf_9j3NGMDUR-FndHmqs,1483
10
10
  fastled/paths.py,sha256=VsPmgu0lNSCFOoEC0BsTYzDygXqy15AHUfN-tTuzDZA,99
11
11
  fastled/sketch.py,sha256=KhhPFqlFVlBk8YrzFy7-ioe7zEzecgrVLhyFbLpBp7k,1845
12
12
  fastled/util.py,sha256=t4M3NFMhnCzfYbLvIyJi0RdFssZqbTN_vVIaej1WV-U,265
13
- fastled/web_compile.py,sha256=QXPtSQfWle4JVd3IG-ZHz-LfRsHiYWQRl2yRXH34m4s,10492
13
+ fastled/web_compile.py,sha256=KuvKGdX6SSUUqC7YgX4T9SMSP5wdcPUhpg9-K9zPoTI,10378
14
14
  fastled/assets/example.txt,sha256=lTBovRjiz0_TgtAtbA1C5hNi2ffbqnNPqkKg6UiKCT8,54
15
- fastled-1.1.15.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
16
- fastled-1.1.15.dist-info/METADATA,sha256=L7sRuUoYogtP4T95QHkLM23ds3SZTdp2V75R13vlzlE,13223
17
- fastled-1.1.15.dist-info/WHEEL,sha256=0VNUDWQJzfRahYI3neAhz2UVbRCtztpN5dPHAGvmGXc,109
18
- fastled-1.1.15.dist-info/entry_points.txt,sha256=RCwmzCSOS4-C2i9EziANq7Z2Zb4KFnEMR1FQC0bBwAw,101
19
- fastled-1.1.15.dist-info/top_level.txt,sha256=xfG6Z_ol9V5YmBROkZq2QTRwjbS2ouCUxaTJsOwfkOo,14
20
- fastled-1.1.15.dist-info/RECORD,,
15
+ fastled-1.1.16.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
16
+ fastled-1.1.16.dist-info/METADATA,sha256=a7SXnewYqDzmkJF6sUkgfn0IwYH_yByh3HbUfxnhDYg,13375
17
+ fastled-1.1.16.dist-info/WHEEL,sha256=0VNUDWQJzfRahYI3neAhz2UVbRCtztpN5dPHAGvmGXc,109
18
+ fastled-1.1.16.dist-info/entry_points.txt,sha256=RCwmzCSOS4-C2i9EziANq7Z2Zb4KFnEMR1FQC0bBwAw,101
19
+ fastled-1.1.16.dist-info/top_level.txt,sha256=xfG6Z_ol9V5YmBROkZq2QTRwjbS2ouCUxaTJsOwfkOo,14
20
+ fastled-1.1.16.dist-info/RECORD,,