|
7 | 7 |
|
8 | 8 | def main():
|
9 | 9 | """
|
10 |
| - Command-line interface for **Brian2Wasm**. |
11 |
| -
|
12 |
| - Usage |
13 |
| - ----- |
14 |
| - ``python -m brian2wasm <script.py> [--no-server] [--skip-install]`` |
15 |
| -
|
16 |
| - Parameters |
17 |
| - ---------- |
18 |
| - script : str |
19 |
| - Path to the user’s Python model. The file **must** end with |
20 |
| - ``.py`` and must not call ``set_device`` itself – the CLI inserts |
21 |
| - the appropriate ``set_device('wasm_standalone', …)`` line |
22 |
| - automatically. |
23 |
| - --no-server : flag, optional |
24 |
| - Generate the WASM/HTML output without starting the local preview |
25 |
| - web-server (sets the ``BRIAN2WASM_NO_SERVER`` environment |
26 |
| - variable for the subprocess). |
27 |
| - --skip-install : flag, optional |
28 |
| - Run Brian2WASM without checking or installing EMSDK. Use this if |
29 |
| - you are sure EMSDK is already installed and configured in your |
30 |
| - environment. |
31 |
| -
|
32 |
| - Behaviour |
33 |
| - --------- |
34 |
| - 1. Validates that *script* exists and is a ``.py`` file. |
35 |
| - 2. Looks for an ``<scriptname>.html`` file in the same directory. |
36 |
| - * If found, passes the HTML file to ``set_device`` so the custom |
37 |
| - template is used. |
38 |
| - * Otherwise falls back to the default template. |
39 |
| - 3. Unless *--skip-install* is given, verifies EMSDK installation |
40 |
| - (Pixi/Conda/CONDA_EMSDK_DIR) and attempts to activate it. |
41 |
| - 4. Prepends the required ``set_device('wasm_standalone', …)`` call to |
42 |
| - the script source in-memory. |
43 |
| - 5. Executes the modified script with its own directory as working |
44 |
| - directory, so any relative paths inside the model behave as |
45 |
| - expected. |
46 |
| -
|
47 |
| - Exit status |
48 |
| - ----------- |
49 |
| - * ``0`` – build finished successfully (and server started unless |
50 |
| - *--no-server* was given). |
51 |
| - * ``1`` – any error (missing file, not a ``.py`` file, EMSDK not found |
52 |
| - or not activated, exception during model execution, etc.). |
| 10 | + Command-line interface entry point for Brian2Wasm. |
| 11 | +
|
| 12 | + This function validates the given script, injects the required |
| 13 | + ``set_device('wasm_standalone', …)`` call, ensures EMSDK is |
| 14 | + available (unless skipped), and executes the modified script. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + script : str |
| 19 | + Path to the Python model file. The file must exist, end with |
| 20 | + ``.py``, and must not call ``set_device`` directly, since this |
| 21 | + function injects the correct call automatically. |
| 22 | + --no-server : bool, optional |
| 23 | + If given, generates the WASM/HTML output without starting the |
| 24 | + local preview web server. Internally sets the environment |
| 25 | + variable ``BRIAN2WASM_NO_SERVER=1``. |
| 26 | + --skip-install : bool, optional |
| 27 | + If given, skips EMSDK installation and activation checks. |
| 28 | + Use this flag when you are certain EMSDK is already installed |
| 29 | + and properly configured in your environment. |
| 30 | +
|
| 31 | + Raises |
| 32 | + ------ |
| 33 | + FileNotFoundError |
| 34 | + If the provided script path does not exist. |
| 35 | + ValueError |
| 36 | + If the provided file is not a Python ``.py`` script. |
| 37 | + RuntimeError |
| 38 | + If execution of the modified script fails for any reason. |
| 39 | + SystemExit |
| 40 | + If errors occur during validation or script execution, the |
| 41 | + process exits with status code ``1``. |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + None |
| 46 | + This function is intended as a CLI entry point and does not |
| 47 | + return a value. |
53 | 48 | """
|
54 | 49 |
|
55 | 50 | parser = argparse.ArgumentParser(
|
@@ -140,6 +135,35 @@ def main():
|
140 | 135 |
|
141 | 136 |
|
142 | 137 | def check_emsdk():
|
| 138 | + """ |
| 139 | + Verify that the Emscripten SDK (EMSDK) is installed and attempt to activate it. |
| 140 | +
|
| 141 | + This function checks for EMSDK in the current environment, using either |
| 142 | + the system path (``emsdk`` executable) or the ``CONDA_EMSDK_DIR`` variable. |
| 143 | + If EMSDK is missing, it prints installation instructions and exits. |
| 144 | + If EMSDK is found but not activated, it attempts to activate the latest |
| 145 | + version, optionally prompting the user to install and activate it. |
| 146 | +
|
| 147 | + Parameters |
| 148 | + ---------- |
| 149 | + None |
| 150 | + This function takes no arguments. |
| 151 | +
|
| 152 | + Raises |
| 153 | + ------ |
| 154 | + SystemExit |
| 155 | + If EMSDK is not found, not activated, or installation/activation |
| 156 | + fails, the process exits with status code ``1``. |
| 157 | + RuntimeError |
| 158 | + If subprocess execution encounters an unexpected failure during |
| 159 | + EMSDK activation. |
| 160 | +
|
| 161 | + Returns |
| 162 | + ------- |
| 163 | + None |
| 164 | + This function is intended as a setup check and does not |
| 165 | + return a value. Its success or failure is indicated by process exit. |
| 166 | + """ |
143 | 167 | emsdk = shutil.which("emsdk")
|
144 | 168 | conda_emsdk_dir = os.environ.get("CONDA_EMSDK_DIR")
|
145 | 169 |
|
|
0 commit comments