Skip to content

Commit f51613b

Browse files
authored
use new empack (#5)
* using empack 3.0.0 * removed emsdk * disabled node * removed dead code
1 parent 07a9677 commit f51613b

File tree

12 files changed

+100
-187
lines changed

12 files changed

+100
-187
lines changed

.github/workflows/main.yaml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ jobs:
1919

2020
strategy:
2121
fail-fast: false
22-
matrix:
23-
emsdk_ver: ["3.1.2"]
2422

2523
steps:
2624
- uses: actions/checkout@v2
@@ -33,17 +31,12 @@ jobs:
3331
with:
3432
environment-file: dev-env.yml
3533
environment-name: dev-env
36-
micromamba-version: "0.22.0"
34+
micromamba-version: "1.4.1"
3735

3836
- name: Install Playwright
3937
run: |
4038
playwright install
4139
42-
- name: Setup emsdk
43-
run: |
44-
micromamba activate dev-env
45-
emsdk install ${{matrix.emsdk_ver}}
46-
4740
- name: Install pyjs-code-runner
4841
run: |
4942
micromamba activate dev-env
@@ -52,9 +45,6 @@ jobs:
5245
- name: Run Tests
5346
run: |
5447
micromamba activate dev-env
55-
emsdk activate ${{matrix.emsdk_ver}}
56-
source $CONDA_EMSDK_DIR/emsdk_env.sh
57-
5848
pytest -s
5949
6050
@@ -64,8 +54,6 @@ jobs:
6454

6555
strategy:
6656
fail-fast: false
67-
matrix:
68-
emsdk_ver: ["3.1.2"]
6957

7058
steps:
7159
- uses: actions/checkout@v2
@@ -78,22 +66,17 @@ jobs:
7866
with:
7967
environment-file: dev-env-pip.yml
8068
environment-name: dev-env
81-
micromamba-version: "0.22.0"
69+
micromamba-version: "1.4.1"
8270

83-
8471
- name: Install pyjs-code-runner
8572
run: |
8673
micromamba activate dev-env
87-
emsdk install ${{matrix.emsdk_ver}}
88-
8974
python -m pip install .
9075
playwright install
9176
9277
- name: Run Tests
9378
run: |
9479
micromamba activate dev-env
95-
emsdk activate ${{matrix.emsdk_ver}}
96-
source $CONDA_EMSDK_DIR/emsdk_env.sh
9780
9881
pytest -s
9982

README.md

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ in browser environment is a complex process with a lot of (complicated) steps.
2020
Here we assume a file `main.py` located at `~/foo/bar/main.py`.
2121

2222
```bash
23-
# run with node
23+
# run in browser-main-thread backend
2424
pyjs_code_runner run script \
25-
node \
25+
browser-main \
2626
--conda-env ~/micromamba/envs/my_env `# the emscripten-forge env` \
2727
`# in which to run the code` \
2828
\
@@ -37,25 +37,10 @@ pyjs_code_runner run script
3737
\
3838
--async-main `# should a top-level async` \
3939
`# function named main be called` \
40-
41-
```
42-
43-
44-
```bash
45-
# run in browser-main-thread backend
46-
# in a headless fashion
47-
pyjs_code_runner run script \
48-
browser-main \
49-
--conda-env ~/micromamba/envs/my_env \
50-
--mount ~/foo/bar:/home/web_user/fubar \
51-
--script main.py \
52-
--work-dir /tests \
53-
--async-main \
5440
--headless
5541

5642
```
5743

58-
5944
```bash
6045
# run in browser-worker-thread backend
6146
# in a headless fashion

dev-env.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies:
77
- pytest
88
- typer
99
- appdirs
10-
- empack >=2.0.0
10+
- empack >=3,<4
1111
- microsoft::playwright
1212
- emsdk
1313
- rich

pyjs_code_runner/backend/backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
class BackendFamilyType(str, Enum):
6-
browser = "browsef"
6+
browser = "browser"
77
node = "node"
88

99

@@ -61,9 +61,8 @@ def ensure_playwright_imports():
6161

6262
def get_backend_cls(backend_type):
6363
if backend_type == BackendType.node:
64-
from .node.node import NodeBackend
64+
raise RuntimeError("the node backend is currently disabled as its not (yet) working with empack>=3.0.0")
6565

66-
return NodeBackend
6766

6867
elif backend_type == BackendType.browser_main:
6968
ensure_playwright_imports()

pyjs_code_runner/backend/browser_main/browser_main.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ async def playwright_run_in_main_thread(self, page_url):
5858

5959
async def handle_console(msg):
6060
txt = str(msg)
61-
if (
62-
txt.startswith(
63-
"warning: Browser does not support creating object URLs"
64-
)
65-
or txt.startswith("Failed to load resource:")
66-
or txt.startswith("Could not find platform dependent libraries")
67-
or txt.startswith("Consider setting $PYTHONHOME")
68-
):
69-
pass
70-
else:
71-
print(txt)
61+
print(txt)
7262

7363
page.on("console", handle_console)
7464

pyjs_code_runner/backend/browser_worker/browser_worker.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ async def handle_console(msg):
101101
txt.startswith(
102102
"warning: Browser does not support creating object URLs"
103103
)
104-
or txt.startswith("Failed to load resource:")
105-
or txt.startswith("Could not find platform dependent libraries")
106-
or txt.startswith("Consider setting $PYTHONHOME")
107104
):
108105
pass
109106
else:

pyjs_code_runner/cli/run.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@
1515
from ..get_file_filter import get_file_filter
1616
from ..constants import EMSCRIPTEN_HOME
1717

18-
# packaging
1918
run_app = typer.Typer()
2019
app.add_typer(run_app, name="run")
2120

22-
23-
# packaging
2421
script_app = typer.Typer()
2522
run_app.add_typer(script_app, name="script")
2623

2724

28-
# constants
29-
30-
3125
def parse_mounts(mounts):
3226
wrong_patter_exception = lambda m: exit_with_err(
3327
f"wrong pattern, must be: <to_mount>:<mount_path> but is `{m}`"
@@ -54,10 +48,15 @@ def parse_mounts(mounts):
5448

5549
conda_env_option = require_option(
5650
*make_names("conda-env"),
57-
help="name of the conda environment in which to run the code",
51+
help="host location of the conda environment in which to run the code",
5852
)
5953
script_option = require_option(
60-
*make_names("script"), help="path of script INSIDE VIRTUAL FILESYSTEM to run"
54+
*make_names("script"), help="path of script inside the virtual fileystem to run"
55+
)
56+
relocate_prefix_option = typer.Option(
57+
"/",
58+
*make_names("relocate-prefix"),
59+
help="location of the conda environment in the virtual file",
6160
)
6261
async_main_option = typer.Option(
6362
False,
@@ -123,6 +122,7 @@ def browser_main(
123122
script: Path = script_option,
124123
async_main: bool = async_main_option,
125124
mounts: List[str] = mounts_option,
125+
relocate_prefix: Optional[Path] = relocate_prefix_option,
126126
work_dir: Optional[Path] = work_dir_option,
127127
pkg_file_filter: Optional[List[Path]] = pkg_file_filter_option,
128128
pyjs_dir: Optional[Path] = pyjs_dir_option,
@@ -136,6 +136,7 @@ def browser_main(
136136
run_script(
137137
backend_type=BackendType.browser_main,
138138
conda_env=conda_env,
139+
relocate_prefix=relocate_prefix,
139140
script=script,
140141
async_main=async_main,
141142
mounts=mounts,
@@ -155,6 +156,7 @@ def browser_worker(
155156
script: Path = script_option,
156157
async_main: bool = async_main_option,
157158
mounts: List[str] = mounts_option,
159+
relocate_prefix: Optional[Path] = relocate_prefix_option,
158160
work_dir: Optional[Path] = work_dir_option,
159161
pkg_file_filter: List[Path] = pkg_file_filter_option,
160162
pyjs_dir: Optional[Path] = pyjs_dir_option,
@@ -168,6 +170,7 @@ def browser_worker(
168170
run_script(
169171
backend_type=BackendType.browser_worker,
170172
conda_env=conda_env,
173+
relocate_prefix=relocate_prefix,
171174
script=script,
172175
async_main=async_main,
173176
mounts=mounts,
@@ -194,6 +197,7 @@ def node(
194197
script: Path = script_option,
195198
async_main: bool = async_main_option,
196199
mounts: List[str] = mounts_option,
200+
relocate_prefix: Optional[Path] = relocate_prefix_option,
197201
work_dir: Optional[Path] = work_dir_option,
198202
pkg_file_filter: List[Path] = pkg_file_filter_option,
199203
pyjs_dir: Optional[Path] = pyjs_dir_option,
@@ -205,6 +209,7 @@ def node(
205209
run_script(
206210
backend_type=BackendType.node,
207211
conda_env=conda_env,
212+
relocate_prefix=relocate_prefix,
208213
script=script,
209214
async_main=async_main,
210215
mounts=mounts,
@@ -221,6 +226,7 @@ def node(
221226
def run_script(
222227
backend_type,
223228
conda_env,
229+
relocate_prefix,
224230
script,
225231
async_main,
226232
mounts,
@@ -244,6 +250,7 @@ def run_script(
244250

245251
run(
246252
conda_env=conda_env,
253+
relocate_prefix=relocate_prefix,
247254
backend_type=backend_type,
248255
script=script,
249256
async_main=async_main,

pyjs_code_runner/get_file_filter.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77

88
def get_file_filter(pkg_file_filter, cache_dir):
99
if pkg_file_filter is None or len(pkg_file_filter) == 0:
10-
11-
# empack now supports passing None
1210
return None
13-
# path_in_cache = Path(cache_dir) / "empack_config.yaml"
14-
# if not path_in_cache.exists():
15-
16-
# urllib.request.urlretrieve(EMPACK_FILE_FILTER_URL, path_in_cache)
17-
# return pkg_file_filter_from_yaml(path_in_cache)
18-
1911
else:
2012
print(pkg_file_filter)
2113
return pkg_file_filter_from_yaml(*pkg_file_filter)

pyjs_code_runner/js/utils.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
1+
async function fetchMount(pyjs, mount){
2+
let url = `./${mount}`;
3+
let filename = `/mount_tarballs/${mount}`;
4+
let byte_array = await pyjs._fetch_byte_array(url, filename);
5+
pyjs.FS.writeFile(filename, byte_array);
6+
pyjs._untar_from_python(filename, "/");
7+
}
8+
9+
async function fetchMounts(pyjs) {
10+
let response = await fetch("./mounts.json");
11+
if (!response.ok) {
12+
throw new Error(`HTTP error while fetching ./mounts.json! status: ${response.status}`);
13+
}
14+
let mounts = await response.json();
15+
pyjs.FS.mkdir("/mount_tarballs");
16+
await Promise.all(mounts.map(mount => fetchMount(pyjs, mount)));
17+
18+
}
19+
120
async function make_pyjs(print, error) {
2-
var pyjs = await createModule({print:print,error:print})
3-
var EmscriptenForgeModule = pyjs
4-
globalThis.EmscriptenForgeModule = pyjs
5-
globalThis.pyjs = pyjs
21+
var pyjs = await createModule({ print: print, error: print })
22+
23+
await pyjs.bootstrap_from_empack_packed_environment(
24+
`./empack_env_meta.json`, /* packages_json_url */
25+
".", /* package_tarballs_root_url */
26+
false /* verbose */
27+
);
628

7-
const { default: importPackages } = await import("./packed_env.js")
8-
await importPackages();
29+
await fetchMounts(pyjs);
30+
//await Promise.all([promise_env, promise_mount]);
931

10-
const { default: importMounts } = await import("./packed_mounts.js")
11-
await importMounts();
12-
13-
await pyjs.init()
32+
//await pyjs.init()
1433

34+
globalThis.pyjs = pyjs
1535
return pyjs
1636
}
1737

1838
globalThis.make_pyjs = make_pyjs
1939

20-
21-
2240
function eval_main_script(pyjs, workdir, filename) {
23-
try{
41+
try {
2442
pyjs.exec("import os;from os.path import exists")
2543
pyjs.exec(`os.chdir("${workdir}")`)
2644
pyjs.eval_file(filename);
2745
return 0;
2846
}
29-
catch(e){
30-
console.error("error while evaluating main file:",e)
47+
catch (e) {
48+
console.error("error while evaluating main file:", e)
3149
return 1;
3250
}
3351
return 0
@@ -56,36 +74,25 @@ async def main_runner():
5674
asyncio.ensure_future(main_runner())
5775
`)
5876

59-
while(true)
60-
{
77+
while (true) {
6178
await new Promise(resolve => setTimeout(resolve, 100));
6279
const _async_done_ = pyjs.eval("_async_done_[0]")
63-
if(_async_done_)
64-
{
80+
if (_async_done_) {
6581
break;
6682
}
6783
}
6884
return pyjs.eval("_ret_code[0]")
69-
85+
7086
}
7187
globalThis.run_async_python_main = run_async_python_main
7288

73-
// export { make_pyjs, run_async_python_main, eval_main_script };
74-
7589

76-
if (typeof exports === 'object' && typeof module === 'object'){
77-
console.log("A")
78-
module.exports = make_pyjs;
90+
if (typeof exports === 'object' && typeof module === 'object') {
91+
module.exports = make_pyjs;
7992
}
80-
else if (typeof define === 'function' && define['amd']){
81-
console.log("B")
82-
define([], function() { return make_pyjs; });
93+
else if (typeof define === 'function' && define['amd']) {
94+
define([], function () { return make_pyjs; });
8395
}
84-
else if (typeof exports === 'object'){
85-
console.log("C")
86-
exports["make_pyjs"] = make_pyjs;
96+
else if (typeof exports === 'object') {
97+
exports["make_pyjs"] = make_pyjs;
8798
}
88-
// else{
89-
// console.log("D")
90-
// export { make_pyjs, run_async_python_main, eval_main_script };
91-
// }

0 commit comments

Comments
 (0)