|
3 | 3 | import os |
4 | 4 | import shutil |
5 | 5 | import sys |
6 | | - |
| 6 | +import shutil |
7 | 7 | from ..backend_base import BackendBase |
8 | 8 | from subprocess import Popen, PIPE, STDOUT |
| 9 | +import textwrap |
9 | 10 |
|
10 | 11 | THIS_DIR = os.path.dirname(os.path.realpath(__file__)) |
11 | 12 |
|
12 | 13 |
|
13 | 14 | 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): |
15 | 16 | super().__init__( |
16 | 17 | host_work_dir=host_work_dir, |
17 | 18 | work_dir=work_dir, |
18 | 19 | script=script, |
19 | 20 | async_main=async_main, |
20 | 21 | ) |
| 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: |
21 | 38 |
|
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 |
23 | 55 |
|
24 | 56 | def run(self): |
25 | 57 | main_name = "node_main.js" |
26 | 58 | main = Path(THIS_DIR) / main_name |
27 | 59 | shutil.copyfile(main, self.host_work_dir / main_name) |
28 | 60 |
|
29 | 61 | cmd = [ |
30 | | - "node", |
| 62 | + self.node_binary, |
31 | 63 | "--no-experimental-fetch", |
32 | 64 | main_name, |
33 | 65 | self.work_dir, |
34 | 66 | self.script, |
35 | 67 | str(int(self.async_main)), |
36 | 68 | ] |
37 | 69 |
|
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) |
0 commit comments