|
1 | 1 | """Command-line interface for PSC.""" |
| 2 | +import os |
| 3 | +import tarfile |
| 4 | +from pathlib import Path |
| 5 | +from shutil import copytree |
| 6 | +from tempfile import TemporaryDirectory |
2 | 7 |
|
3 | 8 | import typer |
4 | 9 | import uvicorn |
| 10 | +from starlette.testclient import TestClient |
| 11 | +from urllib3 import HTTPResponse |
| 12 | +from urllib3 import PoolManager |
| 13 | + |
| 14 | +from psc.here import HERE |
| 15 | +from psc.resources import get_resources |
5 | 16 |
|
6 | 17 |
|
7 | 18 | app = typer.Typer() |
8 | 19 |
|
9 | 20 |
|
| 21 | +def rmtree(root: Path) -> None: |
| 22 | + """Recursively remove everything in the target path.""" |
| 23 | + for p in root.iterdir(): |
| 24 | + if p.is_dir(): |
| 25 | + rmtree(p) |
| 26 | + else: |
| 27 | + p.unlink() |
| 28 | + |
| 29 | + root.rmdir() |
| 30 | + |
| 31 | + |
| 32 | +@app.command() |
| 33 | +def download( |
| 34 | + dry_run: bool = typer.Option(False, "--dry-run") |
| 35 | +) -> None: # pragma: no cover |
| 36 | + """Download Pyodide and PyScript distributions into project dir.""" |
| 37 | + # Get Pyodide first |
| 38 | + pyodide_url_prefix = "https://github.com/pyodide/pyodide/releases/download" |
| 39 | + pyodide_url = f"{pyodide_url_prefix}/0.21.3/pyodide-build-0.21.3.tar.bz2" |
| 40 | + http = PoolManager() |
| 41 | + pyodide_request: HTTPResponse = http.request("GET", pyodide_url) # type: ignore |
| 42 | + with TemporaryDirectory() as tmp_dir_name: |
| 43 | + os.chdir(tmp_dir_name) |
| 44 | + tmp_dir = Path(tmp_dir_name) |
| 45 | + temp_file = tmp_dir / "pyodide.tar.bz2" |
| 46 | + temp_file.write_bytes(pyodide_request.data) |
| 47 | + tar = tarfile.open(temp_file) |
| 48 | + tar.extractall() |
| 49 | + target = HERE / "pyodide" |
| 50 | + if not dry_run: |
| 51 | + copytree(tmp_dir / "pyodide", target, dirs_exist_ok=True) |
| 52 | + print("Downloaded Pyodide") |
| 53 | + |
| 54 | + # Next, PyScript |
| 55 | + pyscript_url = "https://pyscript.net/latest/pyscript.js" |
| 56 | + pyscript_request: HTTPResponse = http.request("GET", pyscript_url) # type: ignore |
| 57 | + if not dry_run: |
| 58 | + target = HERE / "pyscript" |
| 59 | + if not dry_run: |
| 60 | + with open(target / "pyscript.js", "wb") as pyscript_output: |
| 61 | + pyscript_output.write(pyscript_request.data) |
| 62 | + |
| 63 | + print("Downloaded PyScript") |
| 64 | + |
| 65 | + |
| 66 | +@app.command() |
| 67 | +def build() -> None: # pragma: no cover |
| 68 | + """Write the export to a public directory.""" |
| 69 | + print("Building to the public directory") |
| 70 | + |
| 71 | + # If the target directory exists, remove it, then copy everything |
| 72 | + # under src/psc to the target |
| 73 | + public = HERE.parent.parent / "dist/public" |
| 74 | + if public.exists(): |
| 75 | + rmtree(public) |
| 76 | + copytree(HERE, public) |
| 77 | + |
| 78 | + # Setup test client async with our app |
| 79 | + from psc.app import app as web_app |
| 80 | + |
| 81 | + with TestClient(web_app) as test_client: |
| 82 | + # Render the home page then write to the output directory |
| 83 | + response = test_client.get("/") |
| 84 | + assert response.status_code == 200 |
| 85 | + html = response.text |
| 86 | + output = public / "index.html" |
| 87 | + output.write_text(html) |
| 88 | + |
| 89 | + # Same for gallery page |
| 90 | + response = test_client.get("/gallery/index.html") |
| 91 | + assert response.status_code == 200 |
| 92 | + html = response.text |
| 93 | + # output = public / "gallery/index.html" |
| 94 | + with open(public / "gallery/index.html", "w") as f: |
| 95 | + f.write(html) |
| 96 | + |
| 97 | + # Now for each page |
| 98 | + resources = get_resources() |
| 99 | + for page in resources.pages.values(): |
| 100 | + response = test_client.get(f"/pages/{page.path.stem}.html") |
| 101 | + output = public / f"pages/{page.path.stem}.html" |
| 102 | + output.write_text(response.text) |
| 103 | + |
| 104 | + # And for each example |
| 105 | + for example in resources.examples.values(): |
| 106 | + url = f"/gallery/examples/{example.path.stem}/index.html" |
| 107 | + response = test_client.get(url) |
| 108 | + output = public / f"gallery/examples/{example.path.stem}/index.html" |
| 109 | + output.write_text(response.text) |
| 110 | + |
| 111 | + |
| 112 | +# @app.callback(invoke_without_command=True) |
10 | 113 | @app.command() |
11 | | -def main(dry_run: bool = typer.Option(False, "--dry-run")) -> None: # pragma: no cover |
| 114 | +def start(dry_run: bool = typer.Option(False, "--dry-run")) -> None: # pragma: no cover |
12 | 115 | """Default command, used to start the server.""" |
13 | 116 | # If running from the test, we don't want to actually start server |
14 | 117 | if dry_run: |
15 | 118 | print("Skipping server startup") |
16 | 119 | else: |
17 | 120 | print("Starting server") |
18 | 121 | uvicorn.run("psc:app", port=3000, log_level="info") # pragma: no cover |
19 | | - |
20 | | - |
21 | | -if __name__ == "__main__": # pragma: no cover |
22 | | - app() |
|
0 commit comments