-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook_environments.py
executable file
·443 lines (370 loc) · 18 KB
/
notebook_environments.py
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2020 (c) Vladislav Punko <[email protected]>
from __future__ import print_function, unicode_literals
import argparse
import base64
import collections
import errno
import glob
import io
import json
import logging
import logging.config
import os
import platform
import re
import shutil
import subprocess
import sys
import warnings
try:
_to_unicode = unicode
except NameError:
_to_unicode = str
try:
from json.decoder import JSONDecodeError
except ImportError:
# Use this error when decoder exceptions are thrown at this program runtime.
JSONDecodeError = ValueError
LOGO_32_32 = b"""
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBI
WXMAAAN9AAADfQH05qvEAAAAB3RJTUUH3gwMFiI09WvoEQAAA8lJREFUWMPtV11oXEUYPbNuoW20
jdBYqkKMWgsGBKkYIoo/lOJPqlWI+qASUxRRH6QiiFZJBRFbRVDwoaAtkohQlFALpQ+SH9paFYOI
a4VYsEpjg1EUrTGZ+c7xYe9u7t+mV9k3HbjM3e8Oc873zTkzs8B/vbmiAze9MrLKiHsg3mQM7SLP
I9km0pP8ibIZmirO+53jO+491lQCm3aOdVD2McXVIkEaSKL6Xuut9psyGzpy9mQfBgZ4prlLRQgQ
4Q6Cqyk+LmOXoCmJqD1M9iVC93f91vFIkbkLERC1QaTXWdpzcNvNn9L4STz7ZCWiXrq+yNzlIoNM
WgVyiQI/2LB9f4XkbWlAUQkyMK5sGgEsAG2kuDEPMF0RSWheBWhPSGoFDYwIOQDmuETivhwdgOK/
I9CzY2ytlawXxjtJttXUDRKEACNM9UxdIuuoKozG69TWfsA9H009DWof1qx42bmBkEug59XRh0Tb
BatNaLllRv1dDZeg6u/SCkDtVSWHdsCuxsnpFgDPZFxwy+sjF0p8M1HKDLiya51HMGkhgB5gqPby
j+qb/nMyBNw8+kSWU1ZC3O+N1jr+vY7rMAP6NigA8hEJD9CvxFL/QHYfIK9gTNmLKjwR08K3mPIl
fASE7jqwYlXg/JUZDZC8tBEgGwE2tt3gxLNfToPhRtAAGaqVsCoJ2NoMAYmX5Je3vgxe4pikCcFm
CQIgFD1V1bk/nOHwxLYvLoZsqA7MGIFq7LIs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6YadTGgO6YWgA7zfD4s88NKHvt40pG3d/GHIGjM3TEPxjIPAACebP45qHdG6+W5
3+eMIYA7IDIrc+YH0+x+3Tap77o3pk4QwDTci1F9P6hLojU4Cxx7DSAtect/xvmqtthmgjmKphLo
O7YWYq0HdXqRXr3JlpcAuQ/1z24XKfdB2Un8rzGevPkyjI6sBvhVwD5wka/98NEL4S+RxmOysOt8
7GuBUDBe/tQsaLoOYB2gCzK/UQciCeAiBK9B0Q/weQgOoVIOyfznzmBqTI2pMTUmefwX5Mz8p5zV
bn8AAAAASUVORK5CYII=
"""
KERNEL_NAME_PATTERN = re.compile(r"^[a-z0-9._\-]+$", re.IGNORECASE)
# Create a structure to store information about the location of a kernel on the current machine.
_kernel_info = collections.namedtuple(typename="kernel_info", field_names=("name", "path"))
logging.basicConfig(format="%(levelname)s :: %(name)s :: %(message)s")
# Create a new instance of the preferred reporting system for this program.
_logger = logging.getLogger("notebook_environments")
__all__ = (
"add_active_environment",
"initialize_new_notebook_environment",
"purge_broken_kernels",
"remove_active_environment",
"show_kernels",
)
__version__ = "0.8.14"
def _in_virtual_environment():
is_using_venv = (
# Take into consideration user's virtual environments based on standard python packages.
# See information: https://www.python.org/dev/peps/pep-0405
hasattr(sys, "real_prefix")
or getattr(sys, "base_prefix", sys.prefix) != sys.prefix
) # pep 405
# Check a virtual environment of the working python interpreter at this program runtime.
return bool(os.getenv("CONDA_PREFIX") or os.getenv("VIRTUAL_ENV") or is_using_venv)
def _get_data_path(*subdirs):
paths_spec = {
# Set the main path to store notebook server settings on mac operating systems.
"darwin": os.path.join(os.path.expanduser("~/Library/Jupyter"), *subdirs),
# Set the main path to store notebook server settings on unix operating systems.
"linux": os.path.join(os.path.expanduser("~/.local/share/jupyter"), *subdirs),
}
try:
return paths_spec[platform.system().lower()]
except KeyError:
_logger.error("This user's operating system isn't supported now.")
# Stop this program runtime and return the exit status code.
sys.exit(errno.EPERM)
def _get_kernel_name():
name = os.path.basename(sys.prefix)
if not KERNEL_NAME_PATTERN.match(name):
_logger.error("It's impossible to create a new kernel name with invalid characters.")
# Stop this program runtime and return the exit status code.
sys.exit(errno.EPERM)
return name
def _list_kernels_in(path):
try:
content = os.listdir(path)
except OSError as err:
if err.errno in (errno.ENOENT, errno.ENOTDIR):
_logger.warning("There are no python kernels in a determined path.")
# Stop this program runtime and return the exit status code.
sys.exit(0)
else:
raise
for item in content:
abspath = os.path.join(path, item)
if os.path.isdir(abspath) and os.path.isfile(os.path.join(abspath, "kernel.json")):
# Generate information about the location of a kernel on the current machine.
yield _kernel_info(name=item, path=abspath)
def _write_kernel_specification(path):
kernel_spec = {
"argv": [
sys.executable,
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}",
],
"display_name": "python{0} --> {1}".format(sys.version_info[0], sys.prefix),
# Set the main interpreter to run python code cells on the working notebook server.
"language": "python",
}
try:
with io.open(os.path.join(path, "kernel.json"), mode="wt", encoding="utf-8") as stream_out:
# Create a new kernel specification on the current machine.
stream_out.write(_to_unicode(json.dumps(kernel_spec, ensure_ascii=False, indent=2)))
except (IOError, OSError) as err:
_logger.error("It's impossible to create a new specification on the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EIO))
def _provide_required_packages():
with io.open(os.devnull, mode="wb") as devnull:
try:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "ipykernel"],
stdout=devnull,
stderr=sys.stderr, # use this system stream to show all package errors
)
except subprocess.CalledProcessError as err:
_logger.error(
(
"It's impossible to install packages on the current machine.\n"
"You are to update setup tools and run the installation process another time.\n"
"python -m pip install --upgrade pip setuptools wheel"
)
)
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EPERM))
def _write_python_logos(path):
logos_spec = {
"logo-32x32.png": LOGO_32_32,
"logo-64x64.png": LOGO_64_64,
}
for logo_name, logo_image in logos_spec.items():
try:
with io.open(os.path.join(path, logo_name), mode="wb") as stream_out:
# Create a new python logo on the current machine.
stream_out.write(base64.b64decode(logo_image))
except (IOError, OSError) as err:
_logger.error("It's impossible to create python logos on the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EIO))
def _check_and_remove_broken_kernel(path):
try:
with io.open(os.path.join(path, "kernel.json"), mode="rt", encoding="utf-8") as stream_in:
# Get a path to the location of a kernel interpreter on the current machine.
python_path = json.load(stream_in)["argv"][0]
# The provided path to python interpreter is to exist and be accessible to an active user.
if not all((os.path.isfile(python_path), os.access(python_path, os.X_OK))):
_remove_dir(path)
# It's considered python kernels corrupted when these errors occur at this program runtime.
except (IOError, OSError, IndexError, KeyError, JSONDecodeError):
_remove_dir(path)
def _create_dir(path):
try:
# Create a new directory on the current machine.
os.makedirs(path)
except OSError as err:
if err.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
_logger.error("It's impossible to create a new directory on the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EPERM))
def _remove_dir(path):
try:
if os.path.islink(path):
os.unlink(path)
else:
# Execute a function to remove the specified directory tree from the current machine.
shutil.rmtree(path)
except OSError as err:
_logger.error("It's impossible to remove a directory from the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EPERM))
def _create_new_kernel(name):
path = _get_data_path("kernels", name)
_create_dir(path)
_provide_required_packages()
_write_kernel_specification(path)
_write_python_logos(path)
def add_active_environment():
if not _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment isn't active.")
# Stop this program runtime and return the exit status code.
sys.exit(errno.EPERM)
# Add an active python environment to the working notebook server on the current machine.
_create_new_kernel(_get_kernel_name())
def remove_active_environment():
if not _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment isn't active.")
# Stop this program runtime and return the exit status code.
sys.exit(errno.EPERM)
# Find and remove an active python environment from the working notebook server.
for path in glob.glob(_get_data_path("kernels", _get_kernel_name())):
_remove_dir(path)
def purge_broken_kernels():
try:
# Find and remove broken python kernels from the working notebook server.
for kernel_info in _list_kernels_in(_get_data_path("kernels")):
_check_and_remove_broken_kernel(kernel_info.path)
except OSError as err:
_logger.error("It's impossible to find and remove broken python kernels.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EPERM))
def initialize_new_notebook_environment():
if _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment is active.")
# Stop this program runtime and return the exit status code.
sys.exit(errno.EPERM)
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from jupyter_core.paths import jupyter_path # noqa
except ImportError:
def jupyter_path(subdirs): # this function is to return a list
return [_get_data_path(subdirs)]
# Find and remove all python kernels from the working notebook server.
for path in jupyter_path("kernels"):
if os.path.exists(path) and os.path.isdir(path):
_remove_dir(path)
# Add the main python kernel to the working notebook server on the current machine.
_create_new_kernel("python{0}".format(sys.version_info[0]))
def show_kernels():
try:
for kernel_info in _list_kernels_in(_get_data_path("kernels")):
# Show information about the location of a kernel on the current machine.
print("kernel: {0} --> {1}".format(kernel_info.name, kernel_info.path))
except OSError as err:
_logger.error("It's impossible to show python kernels on the working notebook server.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EPERM))
def main(): # pragma: no cover
# Create a new instance of the preferred argument parser.
parser = argparse.ArgumentParser(
description="Manage python virtual environments on the working notebook server."
)
parser.add_argument("-v", "--version", action="version", version=str(__version__))
parser.add_argument(
"-d",
"--debug",
action="store_const",
const=logging.DEBUG, # level must be an int or a str
default=logging.WARNING,
dest="logging_level",
help="print a lot of debugging statements while executing user's commands",
)
# Prevent the users from using two or more arguments at this program runtime.
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-a",
"--add",
action="store_const",
const=add_active_environment,
dest="user_command",
help="add an active python environment to the working notebook server",
)
group.add_argument(
"-r",
"--remove",
action="store_const",
const=remove_active_environment,
dest="user_command",
help="find and remove an active python environment from the working notebook server",
)
group.add_argument(
"-p",
"--purge",
action="store_const",
const=purge_broken_kernels,
dest="user_command",
help="find and remove broken python kernels from the working notebook server",
)
group.add_argument(
"-i",
"--initialize",
action="store_const",
const=initialize_new_notebook_environment,
dest="user_command",
help="find and remove all python kernels and add the working interpreter",
)
group.add_argument(
"-s",
"--show",
action="store_const",
const=show_kernels,
dest="user_command",
help="show active python kernels on the working notebook server",
)
try:
arguments = parser.parse_args()
# Set a new logging level of the preferred reporting system.
_logger.setLevel(arguments.logging_level)
# Execute a command on the current machine.
arguments.user_command()
except KeyboardInterrupt as err:
_logger.error("Stop this program runtime on the current machine.")
_logger.debug("An unexpected error occurred at this program runtime:", exc_info=True)
# Stop this program runtime and return the exit status code.
sys.exit(getattr(err, "errno", errno.EINTR))
if __name__ == "__main__":
main()