File: basic.py

package info (click to toggle)
jupyter-cache 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 840 kB
  • sloc: python: 2,601; makefile: 40; sh: 9
file content (275 lines) | stat: -rw-r--r-- 8,363 bytes parent folder | download
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
import logging
import multiprocessing as mproc
import os
from pathlib import Path
import tempfile
from typing import NamedTuple, Tuple

from jupyter_cache.base import JupyterCacheAbstract, ProjectNb
from jupyter_cache.cache.db import NbProjectRecord
from jupyter_cache.executors.base import ExecutorRunResult, JupyterExecutorAbstract
from jupyter_cache.executors.utils import (
    ExecutionResult,
    copy_assets,
    create_cache_bundle,
    single_nb_execution,
)

REPORT_LEVEL = logging.INFO + 1
logging.addLevelName(REPORT_LEVEL, "REPORT")


class ProcessData(NamedTuple):
    """Data for the process worker."""

    pk: int
    uri: str
    cache: JupyterCacheAbstract
    timeout: int
    allow_errors: bool


class ExecutionWorkerBase:
    """Base execution worker.

    Note this must be pickleable.
    """

    @property
    def logger(self) -> logging.Logger:
        raise NotImplementedError

    def log_info(self, msg: str):
        self.logger.info(msg)

    def execute(self, project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
        raise NotImplementedError

    def __call__(self, data: ProcessData) -> Tuple[int, str]:
        try:
            project_nb = data.cache.get_project_notebook(data.pk)
        except Exception:
            self.logger.error(
                "Failed Retrieving: %s" % data.uri,
                exc_info=True,
            )
            return (2, data.uri)

        try:
            self.log_info("Executing: %s" % project_nb.uri)
            result = self.execute(project_nb, data)
        except Exception:
            self.logger.error(
                "Failed Executing: %s" % data.uri,
                exc_info=True,
            )
            return (2, data.uri)

        if result.err:
            self.logger.warning(
                "Execution Excepted: %s\n%s: %s"
                % (project_nb.uri, type(result.err).__name__, str(result.err))
            )
            NbProjectRecord.set_traceback(
                project_nb.uri, result.exc_string, data.cache.db
            )
            return (1, data.uri)

        self.log_info("Execution Successful: %s" % project_nb.uri)
        try:
            # TODO deal with artifact retrieval
            bundle = create_cache_bundle(
                project_nb, result.cwd, None, result.time, result.exc_string
            )
            data.cache.cache_notebook_bundle(
                bundle, check_validity=False, overwrite=True
            )
        except Exception:
            self.logger.error(
                "Failed Caching: %s" % data.uri,
                exc_info=True,
            )
            return (2, data.uri)

        return (0, data.uri)


class ExecutionWorkerLocalSerial(ExecutionWorkerBase):
    """Execution worker, that executes in local folder."""

    def __init__(self, logger: logging.Logger) -> None:
        super().__init__()
        self._logger = logger

    @property
    def logger(self) -> logging.Logger:
        return self._logger

    @staticmethod
    def execute(project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
        cwd = str(Path(project_nb.uri).parent)
        return single_nb_execution(
            project_nb.nb,
            cwd=cwd,
            timeout=data.timeout,
            allow_errors=data.allow_errors,
        )


class ExecutionWorkerTempSerial(ExecutionWorkerBase):
    """Execution worker, that executes in temporary folder."""

    def __init__(self, logger: logging.Logger) -> None:
        super().__init__()
        self._logger = logger

    @property
    def logger(self) -> logging.Logger:
        return self._logger

    @staticmethod
    def execute(project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
        with tempfile.TemporaryDirectory() as cwd:
            copy_assets(project_nb.uri, project_nb.assets, cwd)
            return single_nb_execution(
                project_nb.nb,
                cwd=cwd,
                timeout=data.timeout,
                allow_errors=data.allow_errors,
            )


class ExecutionWorkerLocalMProc(ExecutionWorkerBase):
    """Execution worker, that executes in local folder."""

    @property
    def logger(self) -> logging.Logger:
        return mproc.get_logger()

    def log_info(self, msg: str):
        # multiprocessing logs a lot at info level that we do not want to see
        self.logger.log(REPORT_LEVEL, msg)

    @staticmethod
    def execute(project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
        cwd = str(Path(project_nb.uri).parent)
        return single_nb_execution(
            project_nb.nb,
            cwd=cwd,
            timeout=data.timeout,
            allow_errors=data.allow_errors,
        )


class ExecutionWorkerTempMProc(ExecutionWorkerBase):
    """Execution worker, that executes in temporary folder."""

    @property
    def logger(self) -> logging.Logger:
        return mproc.get_logger()

    def log_info(self, msg: str):
        # multiprocessing logs a lot at info level that we do not want to see
        self.logger.log(REPORT_LEVEL, msg)

    @staticmethod
    def execute(project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
        with tempfile.TemporaryDirectory() as cwd:
            copy_assets(project_nb.uri, project_nb.assets, cwd)
            return single_nb_execution(
                project_nb.nb,
                cwd=cwd,
                timeout=data.timeout,
                allow_errors=data.allow_errors,
            )


class JupyterExecutorLocalSerial(JupyterExecutorAbstract):
    """An implementation of an executor; executing locally in serial."""

    _EXECUTION_WORKER = ExecutionWorkerLocalSerial

    def run_and_cache(
        self,
        *,
        filter_uris=None,
        filter_pks=None,
        timeout=30,
        allow_errors=False,
        force=False,
    ) -> ExecutorRunResult:
        # Get the notebook that require re-execution
        execute_records = self.get_records(
            filter_uris, filter_pks, clear_tracebacks=True, force=force
        )

        self.logger.info("Executing %s notebook(s) in serial" % len(execute_records))

        results = [
            self._EXECUTION_WORKER(self.logger)(
                ProcessData(record.pk, record.uri, self.cache, timeout, allow_errors)
            )
            for record in execute_records
        ]

        return ExecutorRunResult(
            succeeded=[p for i, p in results if i == 0],
            excepted=[p for i, p in results if i == 1],
            errored=[p for i, p in results if i == 2],
        )


class JupyterExecutorTempSerial(JupyterExecutorLocalSerial):
    """An implementation of an executor; executing in a temporary folder in serial."""

    _EXECUTION_WORKER = ExecutionWorkerTempSerial


class JupyterExecutorLocalMproc(JupyterExecutorAbstract):
    """An implementation of an executor; executing locally in parallel."""

    _EXECUTION_WORKER = ExecutionWorkerLocalMProc

    def run_and_cache(
        self,
        *,
        filter_uris=None,
        filter_pks=None,
        timeout=30,
        allow_errors=False,
        force=False,
    ) -> ExecutorRunResult:
        # Get the notebook that require re-execution
        execute_records = self.get_records(
            filter_uris, filter_pks, clear_tracebacks=True
        )

        self.logger.info(
            "Executing %s notebook(s) over pool of %s processors"
            % (len(execute_records), os.cpu_count())
        )
        mproc.log_to_stderr(
            REPORT_LEVEL if self.logger.level == logging.INFO else self.logger.level
        )

        with mproc.Pool() as pool:
            results = pool.map(
                self._EXECUTION_WORKER(),
                [
                    ProcessData(
                        record.pk, record.uri, self.cache, timeout, allow_errors
                    )
                    for record in execute_records
                ],
            )
        return ExecutorRunResult(
            succeeded=[p for i, p in results if i == 0],
            excepted=[p for i, p in results if i == 1],
            errored=[p for i, p in results if i == 2],
        )


class JupyterExecutorTempMproc(JupyterExecutorLocalMproc):
    """An implementation of an executor; executing in a temporary directory and in parallel."""

    _EXECUTION_WORKER = ExecutionWorkerTempMProc