File size: 9,484 Bytes
d1ceb73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""JupyterLab Server process handler"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import atexit
import logging
import os
import re
import signal
import subprocess
import sys
import threading
import time
import weakref
from logging import Logger
from shutil import which as _which
from typing import Any

from tornado import gen

try:
    import pty
except ImportError:
    pty = None  # type:ignore[assignment]

if sys.platform == "win32":
    list2cmdline = subprocess.list2cmdline
else:

    def list2cmdline(cmd_list: list[str]) -> str:
        """Shim for list2cmdline on posix."""
        import shlex

        return " ".join(map(shlex.quote, cmd_list))


def which(command: str, env: dict[str, str] | None = None) -> str:
    """Get the full path to a command.

    Parameters
    ----------
    command: str
        The command name or path.
    env: dict, optional
        The environment variables, defaults to `os.environ`.
    """
    env = env or os.environ  # type:ignore[assignment]
    path = env.get("PATH") or os.defpath  # type:ignore[union-attr]
    command_with_path = _which(command, path=path)

    # Allow nodejs as an alias to node.
    if command == "node" and not command_with_path:
        command = "nodejs"
        command_with_path = _which("nodejs", path=path)

    if not command_with_path:
        if command in ["nodejs", "node", "npm"]:
            msg = "Please install Node.js and npm before continuing installation. You may be able to install Node.js from your package manager, from conda, or directly from the Node.js website (https://nodejs.org)."
            raise ValueError(msg)
        raise ValueError("The command was not found or was not " + "executable: %s." % command)
    return os.path.abspath(command_with_path)


class Process:
    """A wrapper for a child process."""

    _procs: weakref.WeakSet = weakref.WeakSet()
    _pool = None

    def __init__(
        self,
        cmd: list[str],
        logger: Logger | None = None,
        cwd: str | None = None,
        kill_event: threading.Event | None = None,
        env: dict[str, str] | None = None,
        quiet: bool = False,
    ) -> None:
        """Start a subprocess that can be run asynchronously.

        Parameters
        ----------
        cmd: list
            The command to run.
        logger: :class:`~logger.Logger`, optional
            The logger instance.
        cwd: string, optional
            The cwd of the process.
        env: dict, optional
            The environment for the process.
        kill_event: :class:`~threading.Event`, optional
            An event used to kill the process operation.
        quiet: bool, optional
            Whether to suppress output.
        """
        if not isinstance(cmd, (list, tuple)):
            msg = "Command must be given as a list"  # type:ignore[unreachable]
            raise ValueError(msg)

        if kill_event and kill_event.is_set():
            msg = "Process aborted"
            raise ValueError(msg)

        self.logger = logger or self.get_log()
        self._last_line = ""
        if not quiet:
            self.logger.info("> %s", list2cmdline(cmd))
        self.cmd = cmd

        kwargs = {}
        if quiet:
            kwargs["stdout"] = subprocess.DEVNULL

        self.proc = self._create_process(cwd=cwd, env=env, **kwargs)
        self._kill_event = kill_event or threading.Event()

        Process._procs.add(self)

    def terminate(self) -> int:
        """Terminate the process and return the exit code."""
        proc = self.proc

        # Kill the process.
        if proc.poll() is None:
            os.kill(proc.pid, signal.SIGTERM)

        # Wait for the process to close.
        try:
            proc.wait(timeout=2.0)
        except subprocess.TimeoutExpired:
            if os.name == "nt":  # noqa: SIM108
                sig = signal.SIGBREAK  # type:ignore[attr-defined]
            else:
                sig = signal.SIGKILL

            if proc.poll() is None:
                os.kill(proc.pid, sig)

        finally:
            if self in Process._procs:
                Process._procs.remove(self)

        return proc.wait()

    def wait(self) -> int:
        """Wait for the process to finish.

        Returns
        -------
        The process exit code.
        """
        proc = self.proc
        kill_event = self._kill_event
        while proc.poll() is None:
            if kill_event.is_set():
                self.terminate()
                msg = "Process was aborted"
                raise ValueError(msg)
            time.sleep(1.0)
        return self.terminate()

    @gen.coroutine
    def wait_async(self) -> Any:
        """Asynchronously wait for the process to finish."""
        proc = self.proc
        kill_event = self._kill_event
        while proc.poll() is None:
            if kill_event.is_set():
                self.terminate()
                msg = "Process was aborted"
                raise ValueError(msg)
            yield gen.sleep(1.0)

        raise gen.Return(self.terminate())

    def _create_process(self, **kwargs: Any) -> subprocess.Popen[str]:
        """Create the process."""
        cmd = list(self.cmd)
        kwargs.setdefault("stderr", subprocess.STDOUT)

        cmd[0] = which(cmd[0], kwargs.get("env"))

        if os.name == "nt":
            kwargs["shell"] = True

        return subprocess.Popen(cmd, **kwargs)  # noqa: S603

    @classmethod
    def _cleanup(cls: type[Process]) -> None:
        """Clean up the started subprocesses at exit."""
        for proc in list(cls._procs):
            proc.terminate()

    def get_log(self) -> Logger:
        """Get our logger."""
        if hasattr(self, "logger") and self.logger is not None:
            return self.logger
        # fallback logger
        self.logger = logging.getLogger("jupyterlab")
        self.logger.setLevel(logging.INFO)
        return self.logger


class WatchHelper(Process):
    """A process helper for a watch process."""

    def __init__(
        self,
        cmd: list[str],
        startup_regex: str,
        logger: Logger | None = None,
        cwd: str | None = None,
        kill_event: threading.Event | None = None,
        env: dict[str, str] | None = None,
    ) -> None:
        """Initialize the process helper.

        Parameters
        ----------
        cmd: list
            The command to run.
        startup_regex: string
            The regex to wait for at startup.
        logger: :class:`~logger.Logger`, optional
            The logger instance.
        cwd: string, optional
            The cwd of the process.
        env: dict, optional
            The environment for the process.
        kill_event: callable, optional
            A function to call to check if we should abort.
        """
        super().__init__(cmd, logger=logger, cwd=cwd, kill_event=kill_event, env=env)

        if pty is None:
            self._stdout = self.proc.stdout  # type:ignore[unreachable]

        while 1:
            line = self._stdout.readline().decode("utf-8")  # type:ignore[has-type]
            if not line:
                msg = "Process ended improperly"
                raise RuntimeError(msg)
            print(line.rstrip())
            if re.match(startup_regex, line):
                break

        self._read_thread = threading.Thread(target=self._read_incoming, daemon=True)
        self._read_thread.start()

    def terminate(self) -> int:
        """Terminate the process."""
        proc = self.proc

        if proc.poll() is None:
            if os.name != "nt":
                # Kill the process group if we started a new session.
                os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
            else:
                os.kill(proc.pid, signal.SIGTERM)

        # Wait for the process to close.
        try:
            proc.wait()
        finally:
            if self in Process._procs:
                Process._procs.remove(self)

        return proc.returncode

    def _read_incoming(self) -> None:
        """Run in a thread to read stdout and print"""
        fileno = self._stdout.fileno()  # type:ignore[has-type]
        while 1:
            try:
                buf = os.read(fileno, 1024)
            except OSError as e:
                self.logger.debug("Read incoming error %s", e)
                return

            if not buf:
                return

            print(buf.decode("utf-8"), end="")

    def _create_process(self, **kwargs: Any) -> subprocess.Popen[str]:
        """Create the watcher helper process."""
        kwargs["bufsize"] = 0

        if pty is not None:
            master, slave = pty.openpty()
            kwargs["stderr"] = kwargs["stdout"] = slave
            kwargs["start_new_session"] = True
            self._stdout = os.fdopen(master, "rb")  # type:ignore[has-type]
        else:
            kwargs["stdout"] = subprocess.PIPE  # type:ignore[unreachable]

            if os.name == "nt":
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                kwargs["startupinfo"] = startupinfo
                kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
                kwargs["shell"] = True

        return super()._create_process(**kwargs)


# Register the cleanup handler.
atexit.register(Process._cleanup)