webcompy-cli 0.0.14__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.
Files changed (47) hide show
  1. webcompy_cli/__init__.py +20 -0
  2. webcompy_cli/_argparser.py +232 -0
  3. webcompy_cli/_browser_probes.py +648 -0
  4. webcompy_cli/_browser_test_harness.py +554 -0
  5. webcompy_cli/_build.py +425 -0
  6. webcompy_cli/_console_types.py +15 -0
  7. webcompy_cli/_dependency_resolver.py +301 -0
  8. webcompy_cli/_dualrun_pytest_plugin.py +130 -0
  9. webcompy_cli/_exception.py +9 -0
  10. webcompy_cli/_generate.py +224 -0
  11. webcompy_cli/_init_project.py +54 -0
  12. webcompy_cli/_inspect.py +741 -0
  13. webcompy_cli/_inspect_pyexec.py +244 -0
  14. webcompy_cli/_lock.py +82 -0
  15. webcompy_cli/_lockfile.py +396 -0
  16. webcompy_cli/_lockfile_sync.py +343 -0
  17. webcompy_cli/_output.py +150 -0
  18. webcompy_cli/_prebuild.py +661 -0
  19. webcompy_cli/_pwa.py +474 -0
  20. webcompy_cli/_pyodide_downloader.py +113 -0
  21. webcompy_cli/_pyodide_lock.py +74 -0
  22. webcompy_cli/_runtime_downloader.py +222 -0
  23. webcompy_cli/_server.py +568 -0
  24. webcompy_cli/_static_files.py +27 -0
  25. webcompy_cli/_utils.py +72 -0
  26. webcompy_cli/_version_sweep.py +213 -0
  27. webcompy_cli/_wheel_builder.py +509 -0
  28. webcompy_cli/config/__init__.py +23 -0
  29. webcompy_cli/config/_build_config.py +168 -0
  30. webcompy_cli/config/_pwa_config.py +203 -0
  31. webcompy_cli/config/_server_config.py +53 -0
  32. webcompy_cli/py.typed +0 -0
  33. webcompy_cli/template_data/app/__init__.py +0 -0
  34. webcompy_cli/template_data/app/app.py +22 -0
  35. webcompy_cli/template_data/app/components/__init__.py +0 -0
  36. webcompy_cli/template_data/app/components/fizzbuzz.py +117 -0
  37. webcompy_cli/template_data/app/components/home.py +15 -0
  38. webcompy_cli/template_data/app/components/input.py +34 -0
  39. webcompy_cli/template_data/app/components/navigation.py +36 -0
  40. webcompy_cli/template_data/app/components/not_found.py +22 -0
  41. webcompy_cli/template_data/app/components/root.py +16 -0
  42. webcompy_cli/template_data/app/router.py +17 -0
  43. webcompy_cli/template_data/webcompy_config.py +12 -0
  44. webcompy_cli-0.0.14.dist-info/METADATA +53 -0
  45. webcompy_cli-0.0.14.dist-info/RECORD +47 -0
  46. webcompy_cli-0.0.14.dist-info/WHEEL +4 -0
  47. webcompy_cli-0.0.14.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,222 @@
1
+ """Download helpers for PyScript and Pyodide runtime assets."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import pathlib
7
+ import re
8
+ import sys
9
+ import urllib.error
10
+ import urllib.request
11
+ import zipfile
12
+
13
+ from webcompy_cli._pyodide_lock import PYODIDE_RUNTIME_URL_TEMPLATE
14
+
15
+ PYSCRIPT_OFFLINE_URL_TEMPLATE = "https://pyscript.net/releases/{pyscript_version}/offline_{pyscript_version}.zip"
16
+ PYODIDE_RUNTIME_ASSETS = [
17
+ "pyodide.mjs",
18
+ "pyodide.asm.wasm",
19
+ "pyodide.asm.mjs",
20
+ "python_stdlib.zip",
21
+ "pyodide-lock.json",
22
+ ]
23
+
24
+
25
+ def get_pyodide_runtime_assets(pyodide_version: str) -> list[str]:
26
+ """Return the Pyodide runtime asset filenames for a given Pyodide version.
27
+
28
+ Pyodide 0.30+ and 314.x distribute the WebAssembly glue module as
29
+ ``pyodide.asm.mjs`` rather than ``pyodide.asm.js``.
30
+
31
+ Args:
32
+ pyodide_version: Target Pyodide distribution version string.
33
+
34
+ Returns:
35
+ Ordered list of runtime asset filenames required for local serving.
36
+
37
+ """
38
+ match = re.match(r"^\s*(\d+)\.(\d+)", pyodide_version)
39
+ major = int(match.group(1)) if match else 314
40
+ minor = int(match.group(2)) if match else 0
41
+ asm_entry = "pyodide.asm.js" if major == 0 and minor < 30 else "pyodide.asm.mjs"
42
+ return [
43
+ "pyodide.mjs",
44
+ "pyodide.asm.wasm",
45
+ asm_entry,
46
+ "python_stdlib.zip",
47
+ "pyodide-lock.json",
48
+ ]
49
+
50
+
51
+ _EXCLUDED_ZIP_NAMES = frozenset(
52
+ {
53
+ "micropython",
54
+ "pyodide",
55
+ "service-worker.js",
56
+ "mini-coi-fd.js",
57
+ "xterm.css",
58
+ "index.html",
59
+ }
60
+ )
61
+
62
+
63
+ class RuntimeDownloadError(Exception):
64
+ pass
65
+
66
+
67
+ def _sha256_of_file(path: pathlib.Path) -> str:
68
+ h = hashlib.sha256()
69
+ with open(path, "rb") as f:
70
+ while True:
71
+ chunk = f.read(65536)
72
+ if not chunk:
73
+ break
74
+ h.update(chunk)
75
+ return h.hexdigest()
76
+
77
+
78
+ def _download_file(url: str, dest: pathlib.Path) -> bytes:
79
+ try:
80
+ req = urllib.request.Request(url, headers={"User-Agent": "WebComPy/0.1"})
81
+ with urllib.request.urlopen(req, timeout=120) as resp:
82
+ data = resp.read()
83
+ except (urllib.error.URLError, OSError, TimeoutError) as e:
84
+ raise RuntimeDownloadError(f"Failed to download {url}: {e}. Ensure you have network access.") from e
85
+ dest.parent.mkdir(parents=True, exist_ok=True)
86
+ dest.write_bytes(data)
87
+ return data
88
+
89
+
90
+ def _should_extract(member_name: str) -> bool:
91
+ parts = member_name.split("/")
92
+ if len(parts) < 3:
93
+ return False
94
+ if parts[0] != "offline" or parts[1] != "pyscript":
95
+ return False
96
+ filename = parts[2]
97
+ if not filename:
98
+ return False
99
+ if filename.endswith(".map") or filename.endswith(".d.ts"):
100
+ return False
101
+ if filename in _EXCLUDED_ZIP_NAMES:
102
+ return False
103
+ return filename.endswith(".js") or filename.endswith(".css")
104
+
105
+
106
+ def download_pyscript_bundle(
107
+ pyscript_version: str,
108
+ modules_dir: pathlib.Path,
109
+ ) -> dict[str, tuple[pathlib.Path, str]]:
110
+ cache_dir = modules_dir / "runtime-assets" / pyscript_version / "pyscript"
111
+ cache_dir.mkdir(parents=True, exist_ok=True)
112
+
113
+ existing_files = list(cache_dir.glob("*.js")) + list(cache_dir.glob("*.css"))
114
+ has_full_bundle = any(f.name not in ("core.js", "core.css") for f in existing_files)
115
+ if has_full_bundle:
116
+ results: dict[str, tuple[pathlib.Path, str]] = {}
117
+ for f in existing_files:
118
+ if f.name.endswith(".map") or f.name.endswith(".d.ts"):
119
+ continue
120
+ results[f.name] = (f, _sha256_of_file(f))
121
+ return results
122
+
123
+ url = PYSCRIPT_OFFLINE_URL_TEMPLATE.format(pyscript_version=pyscript_version)
124
+ _download_file(url, cache_dir / "offline.zip")
125
+ zip_path = cache_dir / "offline.zip"
126
+
127
+ results = {}
128
+ with zipfile.ZipFile(zip_path) as zf:
129
+ for info in zf.infolist():
130
+ if info.is_dir():
131
+ continue
132
+ if not _should_extract(info.filename):
133
+ continue
134
+ target_path = cache_dir / pathlib.Path(info.filename).name
135
+ with zf.open(info) as src:
136
+ data = src.read()
137
+ target_path.write_bytes(data)
138
+ sha256 = hashlib.sha256(data).hexdigest()
139
+ results[target_path.name] = (target_path, sha256)
140
+
141
+ zip_path.unlink()
142
+ return results
143
+
144
+
145
+ def _get_cdn_package_files(lockfile) -> list[tuple[str, str]]:
146
+ if lockfile is None:
147
+ return []
148
+ result: list[tuple[str, str]] = []
149
+ for entry in lockfile.pure_python_packages.values():
150
+ if entry.in_pyodide_cdn and entry.pyodide_file_name and entry.pyodide_sha256:
151
+ result.append((entry.pyodide_file_name, entry.pyodide_sha256))
152
+ return result
153
+
154
+
155
+ def download_runtime_assets(
156
+ pyodide_version: str,
157
+ pyscript_version: str,
158
+ modules_dir: pathlib.Path,
159
+ dest_dir: pathlib.Path | None = None,
160
+ lock_file: object = None,
161
+ ) -> dict[str, tuple[pathlib.Path, str]]:
162
+ from webcompy_cli._lockfile import Lockfile
163
+
164
+ results = download_pyscript_bundle(pyscript_version, modules_dir)
165
+
166
+ if dest_dir is not None:
167
+ copied: dict[str, tuple[pathlib.Path, str]] = {}
168
+ for filename, (src_path, sha256) in results.items():
169
+ dest_path = dest_dir / filename
170
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
171
+ if dest_path != src_path:
172
+ dest_path.write_bytes(src_path.read_bytes())
173
+ copied[filename] = (dest_path, sha256)
174
+ results = copied
175
+
176
+ pyodide_dest_dir = (dest_dir or modules_dir / "runtime-assets" / pyscript_version) / "pyodide"
177
+ pyodide_cache_dir = modules_dir / "runtime-assets" / pyscript_version / "pyodide"
178
+
179
+ for filename in get_pyodide_runtime_assets(pyodide_version):
180
+ cached_path = pyodide_cache_dir / filename
181
+ dest_path = pyodide_dest_dir / filename
182
+ rel_path = f"pyodide/{filename}"
183
+
184
+ if cached_path.is_file():
185
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
186
+ if dest_path != cached_path:
187
+ dest_path.write_bytes(cached_path.read_bytes())
188
+ sha256 = _sha256_of_file(dest_path)
189
+ results[rel_path] = (dest_path, sha256)
190
+ continue
191
+
192
+ url = PYODIDE_RUNTIME_URL_TEMPLATE.format(pyodide_version=pyodide_version, filename=filename)
193
+ data = _download_file(url, cached_path)
194
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
195
+ dest_path.write_bytes(data)
196
+ sha256 = hashlib.sha256(data).hexdigest()
197
+ results[rel_path] = (dest_path, sha256)
198
+
199
+ if isinstance(lock_file, Lockfile):
200
+ from webcompy_cli._pyodide_downloader import download_pyodide_wheel
201
+ from webcompy_cli._pyodide_lock import fetch_pyodide_lock
202
+
203
+ pyodide_lock = fetch_pyodide_lock(pyodide_version, modules_dir)
204
+ micropip_info = pyodide_lock.get("packages", {}).get("micropip", {})
205
+ cdn_files = _get_cdn_package_files(lock_file)
206
+ extra_files: list[tuple[str, str]] = []
207
+ if micropip_info.get("file_name") and micropip_info.get("sha256"):
208
+ extra_files.append((micropip_info["file_name"], micropip_info["sha256"]))
209
+ for file_name, sha256_val in cdn_files + extra_files:
210
+ try:
211
+ wheel_path = download_pyodide_wheel(file_name, pyodide_version, sha256_val, modules_dir)
212
+ except Exception as e:
213
+ print(f"Warning: failed to download CDN wheel {file_name}: {e}", file=sys.stderr, flush=True)
214
+ continue
215
+ dest_path = pyodide_dest_dir / file_name
216
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
217
+ if dest_path != wheel_path:
218
+ dest_path.write_bytes(wheel_path.read_bytes())
219
+ sha256 = _sha256_of_file(dest_path)
220
+ results[f"pyodide/{file_name}"] = (dest_path, sha256)
221
+
222
+ return results