Skip to content

Commit 99bd47b

Browse files
authored
adding github actions ci workflow (#1)
* adding ci
1 parent 7e9e552 commit 99bd47b

File tree

20 files changed

+452
-57
lines changed

20 files changed

+452
-57
lines changed

.github/workflows/main.yaml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
pull_request:
7+
8+
9+
defaults:
10+
run:
11+
shell: bash -l {0}
12+
13+
14+
jobs:
15+
16+
test_with_conda:
17+
18+
runs-on: ubuntu-latest
19+
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
emsdk_ver: ["3.1.2"]
24+
25+
steps:
26+
- uses: actions/checkout@v2
27+
28+
- name: Get number of CPU cores
29+
uses: SimenB/github-actions-cpu-cores@v1
30+
31+
- name: Install mamba
32+
uses: mamba-org/provision-with-micromamba@main
33+
with:
34+
environment-file: dev-env.yml
35+
environment-name: dev-env
36+
micromamba-version: "0.22.0"
37+
38+
- name: Install Playwright
39+
run: |
40+
playwright install
41+
42+
- name: Setup emsdk
43+
run: |
44+
micromamba activate dev-env
45+
emsdk install ${{matrix.emsdk_ver}}
46+
47+
- name: Install pyjs-code-runner
48+
run: |
49+
micromamba activate dev-env
50+
python -m pip install . --no-deps
51+
52+
- name: Run Tests
53+
run: |
54+
micromamba activate dev-env
55+
emsdk activate ${{matrix.emsdk_ver}}
56+
source $CONDA_EMSDK_DIR/emsdk_env.sh
57+
58+
pytest -s
59+
60+
61+
test_with_pip:
62+
63+
runs-on: ubuntu-latest
64+
65+
strategy:
66+
fail-fast: false
67+
matrix:
68+
emsdk_ver: ["3.1.2"]
69+
70+
steps:
71+
- uses: actions/checkout@v2
72+
73+
- name: Get number of CPU cores
74+
uses: SimenB/github-actions-cpu-cores@v1
75+
76+
- name: Install mamba
77+
uses: mamba-org/provision-with-micromamba@main
78+
with:
79+
environment-file: dev-env-pip.yml
80+
environment-name: dev-env
81+
micromamba-version: "0.22.0"
82+
83+
84+
- name: Install pyjs-code-runner
85+
run: |
86+
micromamba activate dev-env
87+
emsdk install ${{matrix.emsdk_ver}}
88+
89+
python -m pip install .
90+
playwright install
91+
92+
- name: Run Tests
93+
run: |
94+
micromamba activate dev-env
95+
emsdk activate ${{matrix.emsdk_ver}}
96+
source $CONDA_EMSDK_DIR/emsdk_env.sh
97+
98+
pytest -s
99+
100+

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include pyjs_code_runner/html/browser_main.html
2+
include pyjs_code_runner/html/browser_worker.html
3+
include pyjs_code_runner/js/utils.js
4+
include pyjs_code_runner/js/worker.js
5+
include pyjs_code_runner/backend/node/node_main.js

dev-env-pip.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: ci-env
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python
6+
- pip
7+
- emsdk
8+
- pytest

dev-env.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: ci-env
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python
6+
- pip
7+
- pytest
8+
- typer
9+
- appdirs
10+
- empack >=2.0.0
11+
- microsoft::playwright
12+
- emsdk
13+
- rich

pyjs_code_runner/backend/backend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ def ensure_playwright_imports():
3636
* conda:
3737
3838
conda install -c microsoft playwright
39+
playwright install
3940
4041
* mamba:
4142
4243
mamba install -c microsoft playwright
44+
playwright install
4345
4446
* micromamba:
4547
4648
micromamb install -c microsoft playwright
49+
playwright install
4750
4851
* pip:
4952
5053
python -m pip install playwright
54+
playwright install
55+
56+
5157
5258
"""
5359
raise ModuleNotFoundError(textwrap.dedent(msg))

pyjs_code_runner/backend/browser_main/browser_main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def run(self):
4040

4141
page_url = f"{url}/{browser_main_html}"
4242
ret = asyncio.run(self.playwright_run_in_main_thread(page_url=page_url))
43+
if ret != 0:
44+
raise RuntimeError("return_code != 0")
4345

4446
async def playwright_run_in_main_thread(self, page_url):
4547
async with async_playwright() as p:

pyjs_code_runner/backend/browser_main/server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def log_message(self, fmt, *args):
3030
@contextmanager
3131
def server_context(work_dir, port):
3232
thread, server = start_server(work_dir=work_dir, port=port)
33-
yield server, f"http://127.0.0.1:{port}"
34-
server.shutdown()
35-
thread.join()
33+
try:
34+
yield server, f"http://127.0.0.1:{port}"
35+
finally:
36+
server.shutdown()
37+
thread.join()

pyjs_code_runner/backend/browser_worker/browser_worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33
from ..backend_base import BackendBase
44
from ...constants import JS_DIR, HTML_DIR
5-
from .server import server_context, find_free_port
5+
from ..browser_main.server import server_context, find_free_port
66
from playwright.async_api import async_playwright
77
from pathlib import Path
88
import shutil
@@ -43,6 +43,8 @@ def run(self):
4343

4444
page_url = f"{url}/{browser_worker_html}"
4545
ret = asyncio.run(self.playwright_run_in_worker_thread(page_url=page_url))
46+
if ret != 0:
47+
raise RuntimeError("return_code != 0")
4648

4749
async def playwright_run_in_worker_thread(self, page_url):
4850
async with async_playwright() as p:

pyjs_code_runner/backend/browser_worker/server.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

pyjs_code_runner/backend/node/node.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33
import os
44
import shutil
5+
import json
56
import sys
67
import shutil
78
from ..backend_base import BackendBase
@@ -53,22 +54,33 @@ def __init__(self, host_work_dir, work_dir, script, async_main, node_binary):
5354
node_binary = shutil_node_binary
5455
self.node_binary = node_binary
5556

57+
def supports_flag_no_experimental_fetch(self):
58+
probe_cmd = [self.node_binary, "--no-experimental-fetch", "--version"]
59+
ret_code = subprocess.call(
60+
probe_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
61+
)
62+
return ret_code == 0
63+
5664
def run(self):
5765
main_name = "node_main.js"
5866
main = Path(THIS_DIR) / main_name
5967
shutil.copyfile(main, self.host_work_dir / main_name)
6068

61-
cmd = [
62-
self.node_binary,
63-
"--no-experimental-fetch",
64-
main_name,
65-
self.work_dir,
66-
self.script,
67-
str(int(self.async_main)),
68-
]
69+
cmd = [self.node_binary]
70+
if self.supports_flag_no_experimental_fetch():
71+
cmd.append("--no-experimental-fetch")
72+
73+
cmd.extend(
74+
[
75+
main_name,
76+
self.work_dir,
77+
self.script,
78+
str(int(self.async_main)),
79+
self.host_work_dir,
80+
]
81+
)
6982

7083
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
71-
7284
# Poll process.stdout to show stdout live
7385
while True:
7486
output = process.stdout.readline()
@@ -77,6 +89,16 @@ def run(self):
7789
if output:
7890
print(output.decode().strip())
7991
rc = process.poll()
80-
# print("RC", rc)
8192
if process.returncode != 0:
82-
sys.exit(process.returncode)
93+
raise RuntimeError(
94+
f"node return with returncode: {process.returncode} rc {rc}"
95+
)
96+
97+
result_path = self.host_work_dir / "_node_result.json"
98+
if result_path.exists():
99+
with open(result_path, "r") as f:
100+
results = json.load(f)
101+
if results["return_code"] != 0:
102+
raise RuntimeError(results["error"])
103+
else:
104+
raise RuntimeError("internal error in pyjs-code-runner")

0 commit comments

Comments
 (0)