Skip to content

Commit 7e9e552

Browse files
committed
improved node availability check
1 parent b406c86 commit 7e9e552

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

pyjs_code_runner/backend/backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ def ensure_playwright_imports():
2525
import playwright
2626
except ModuleNotFoundError as e:
2727
msg = """\n
28-
pyks-code-runner error:
28+
pyjs-code-runner error:
2929
3030
* Cannot import playwright!
3131
32-
to use the browser-{worker/main} backends
33-
playwright needs to be installed.
32+
To use the browser-{worker/main} backends `playwright` needs to be installed.
3433
3534
Install playwight with:
3635

pyjs_code_runner/backend/node/node.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,80 @@
33
import os
44
import shutil
55
import sys
6-
6+
import shutil
77
from ..backend_base import BackendBase
88
from subprocess import Popen, PIPE, STDOUT
9+
import textwrap
910

1011
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
1112

1213

1314
class NodeBackend(BackendBase):
14-
def __init__(self, host_work_dir, work_dir, script, async_main, node_exe):
15+
def __init__(self, host_work_dir, work_dir, script, async_main, node_binary):
1516
super().__init__(
1617
host_work_dir=host_work_dir,
1718
work_dir=work_dir,
1819
script=script,
1920
async_main=async_main,
2021
)
22+
if node_binary is None:
23+
shutil_node_binary = shutil.which("node")
24+
if shutil_node_binary is None:
25+
raise RuntimeError(
26+
textwrap.dedent(
27+
"""\n
28+
pyjs-code-runner error:
29+
30+
* Cannot find node
31+
32+
33+
to use the node backend `node`/`nodejs` needs to be installed.
34+
35+
Install playwight with:
36+
37+
* conda:
2138
22-
self.node_exe = node_exe
39+
conda install -c conda-forge nodejs
40+
41+
* mamba:
42+
43+
mamba install -c conda-forge nodejs
44+
45+
* micromamba:
46+
47+
micromamb install -c conda-forge nodejs
48+
49+
"""
50+
)
51+
)
52+
else:
53+
node_binary = shutil_node_binary
54+
self.node_binary = node_binary
2355

2456
def run(self):
2557
main_name = "node_main.js"
2658
main = Path(THIS_DIR) / main_name
2759
shutil.copyfile(main, self.host_work_dir / main_name)
2860

2961
cmd = [
30-
"node",
62+
self.node_binary,
3163
"--no-experimental-fetch",
3264
main_name,
3365
self.work_dir,
3466
self.script,
3567
str(int(self.async_main)),
3668
]
3769

38-
if False:
39-
ret = subprocess.run(cmd, cwd=os.getcwd(), stdout=PIPE)
40-
returncode = ret.returncode
41-
output = ret.stdout.decode()
42-
print(output)
43-
if returncode != 0:
44-
sys.exit(returncode)
45-
46-
else:
47-
print("START")
48-
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
49-
50-
# Poll process.stdout to show stdout live
51-
while True:
52-
output = process.stdout.readline()
53-
if process.poll() is not None:
54-
break
55-
if output:
56-
print(output.decode().strip())
57-
rc = process.poll()
58-
# print("RC", rc)
59-
if process.returncode != 0:
60-
sys.exit(process.returncode)
70+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
71+
72+
# Poll process.stdout to show stdout live
73+
while True:
74+
output = process.stdout.readline()
75+
if process.poll() is not None:
76+
break
77+
if output:
78+
print(output.decode().strip())
79+
rc = process.poll()
80+
# print("RC", rc)
81+
if process.returncode != 0:
82+
sys.exit(process.returncode)

pyjs_code_runner/cli/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def browser_worker(
173173
)
174174

175175

176-
node_exe_option = typer.Option(
176+
node_binary_option = typer.Option(
177177
None,
178-
*make_names("env-cache-dir"),
178+
"--node-binary",
179179
help="""node exectutable""",
180180
)
181181

@@ -191,7 +191,7 @@ def node(
191191
pyjs_dir: Optional[Path] = pyjs_dir_option,
192192
host_work_dir: Optional[Path] = host_work_dir_option,
193193
cache_dir: Optional[Path] = cache_dir_option,
194-
node_exe: Optional[Path] = node_exe_option,
194+
node_binary: Optional[Path] = node_binary_option,
195195
):
196196
run_script(
197197
backend_type=BackendType.node,
@@ -204,7 +204,7 @@ def node(
204204
pyjs_dir=pyjs_dir,
205205
cache_dir=cache_dir,
206206
host_work_dir=host_work_dir,
207-
backend_kwargs=dict(node_exe=node_exe),
207+
backend_kwargs=dict(node_binary=node_binary),
208208
)
209209

210210

0 commit comments

Comments
 (0)