-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlabextensions.py
508 lines (408 loc) · 16.5 KB
/
labextensions.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# coding: utf-8
"""Jupyter LabExtension Entry Points."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import sys
from copy import copy
from jupyter_core.application import JupyterApp, base_aliases, base_flags
from traitlets import Bool, Instance, List, Unicode, default
from jupyterlab.coreconfig import CoreConfig
from jupyterlab.debuglog import DebugLogFileMixin
from .commands import (
HERE,
AppOptions,
build,
check_extension,
disable_extension,
enable_extension,
get_app_version,
install_extension,
link_package,
list_extensions,
uninstall_extension,
unlink_package,
update_extension,
)
from .federated_labextensions import (
build_labextension,
develop_labextension_py,
watch_labextension,
)
from .labapp import LabApp
flags = dict(base_flags)
flags["no-build"] = (
{"BaseExtensionApp": {"should_build": False}},
"Defer building the app after the action.",
)
flags["dev-build"] = ({"BaseExtensionApp": {"dev_build": True}}, "Build in development mode.")
flags["no-minimize"] = (
{"BaseExtensionApp": {"minimize": False}},
"Do not minimize a production build.",
)
flags["clean"] = (
{"BaseExtensionApp": {"should_clean": True}},
"Cleanup intermediate files after the action.",
)
flags["splice-source"] = (
{"BaseExtensionApp": {"splice_source": True}},
"Splice source packages into app directory.",
)
check_flags = copy(flags)
check_flags["installed"] = (
{"CheckLabExtensionsApp": {"should_check_installed_only": True}},
"Check only if the extension is installed.",
)
develop_flags = copy(flags)
develop_flags["overwrite"] = ({"DevelopLabExtensionApp": {"overwrite": True}}, "Overwrite files")
update_flags = copy(flags)
update_flags["all"] = ({"UpdateLabExtensionApp": {"all": True}}, "Update all extensions")
uninstall_flags = copy(flags)
uninstall_flags["all"] = ({"UninstallLabExtensionApp": {"all": True}}, "Uninstall all extensions")
aliases = dict(base_aliases)
aliases["app-dir"] = "BaseExtensionApp.app_dir"
aliases["dev-build"] = "BaseExtensionApp.dev_build"
aliases["minimize"] = "BaseExtensionApp.minimize"
aliases["debug-log-path"] = "DebugLogFileMixin.debug_log_path"
install_aliases = copy(aliases)
install_aliases["pin-version-as"] = "InstallLabExtensionApp.pin"
enable_aliases = copy(aliases)
enable_aliases["level"] = "EnableLabExtensionsApp.level"
disable_aliases = copy(aliases)
disable_aliases["level"] = "DisableLabExtensionsApp.level"
VERSION = get_app_version()
class BaseExtensionApp(JupyterApp, DebugLogFileMixin):
version = VERSION
flags = flags
aliases = aliases
name = "lab"
# Not configurable!
core_config = Instance(CoreConfig, allow_none=True)
app_dir = Unicode("", config=True, help="The app directory to target")
should_build = Bool(True, config=True, help="Whether to build the app after the action")
dev_build = Bool(
None,
allow_none=True,
config=True,
help="Whether to build in dev mode. Defaults to True (dev mode) if there are any locally linked extensions, else defaults to False (production mode).",
)
minimize = Bool(
True, config=True, help="Whether to minimize a production build (defaults to True)."
)
should_clean = Bool(
False,
config=True,
help="Whether temporary files should be cleaned up after building jupyterlab",
)
splice_source = Bool(False, config=True, help="Splice source packages into app directory.")
labextensions_path = List(
Unicode(), help="The standard paths to look in for prebuilt JupyterLab extensions"
)
@default("labextensions_path")
def _default_labextensions_path(self):
lab = LabApp()
lab.load_config_file()
return lab.extra_labextensions_path + lab.labextensions_path
@default("splice_source")
def _default_splice_source(self):
version = get_app_version(AppOptions(app_dir=self.app_dir))
return version.endswith("-spliced")
def start(self):
if self.app_dir and self.app_dir.startswith(HERE):
raise ValueError("Cannot run lab extension commands in core app")
with self.debug_logging():
ans = self.run_task()
if ans and self.should_build:
production = None if self.dev_build is None else not self.dev_build
app_options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
splice_source=self.splice_source,
)
build(
clean_staging=self.should_clean,
production=production,
minimize=self.minimize,
app_options=app_options,
)
def run_task(self):
pass
def _log_format_default(self):
"""A default format for messages"""
return "%(message)s"
class InstallLabExtensionApp(BaseExtensionApp):
description = """Install labextension(s)
Usage
jupyter labextension install [--pin-version-as <alias,...>] <package...>
This installs JupyterLab extensions similar to yarn add or npm install.
Pass a list of comma separate names to the --pin-version-as flag
to use as aliases for the packages providers. This is useful to
install multiple versions of the same extension.
These can be uninstalled with the alias you provided
to the flag, similar to the "alias" feature of yarn add.
"""
aliases = install_aliases
pin = Unicode("", config=True, help="Pin this version with a certain alias")
def run_task(self):
pinned_versions = self.pin.split(",")
self.extra_args = self.extra_args or [os.getcwd()]
return any(
[
install_extension(
arg,
# Pass in pinned alias if we have it
pin=pinned_versions[i] if i < len(pinned_versions) else None,
app_options=AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
),
)
for i, arg in enumerate(self.extra_args)
]
)
class DevelopLabExtensionApp(BaseExtensionApp):
description = "(developer) Develop labextension"
flags = develop_flags
user = Bool(False, config=True, help="Whether to do a user install")
sys_prefix = Bool(True, config=True, help="Use the sys.prefix as the prefix")
overwrite = Bool(False, config=True, help="Whether to overwrite files")
symlink = Bool(True, config=False, help="Whether to use a symlink")
labextensions_dir = Unicode(
"", config=True, help="Full path to labextensions dir (probably use prefix or user)"
)
def run_task(self):
"Add config for this labextension"
self.extra_args = self.extra_args or [os.getcwd()]
for arg in self.extra_args:
develop_labextension_py(
arg,
user=self.user,
sys_prefix=self.sys_prefix,
labextensions_dir=self.labextensions_dir,
logger=self.log,
overwrite=self.overwrite,
symlink=self.symlink,
)
class BuildLabExtensionApp(BaseExtensionApp):
description = "(developer) Build labextension"
static_url = Unicode("", config=True, help="Sets the url for static assets when building")
development = Bool(False, config=True, help="Build in development mode")
source_map = Bool(False, config=True, help="Generate source maps")
core_path = Unicode(
os.path.join(HERE, "staging"),
config=True,
help="Directory containing core application package.json file",
)
aliases = {
"static-url": "BuildLabExtensionApp.static_url",
"development": "BuildLabExtensionApp.development",
"source-map": "BuildLabExtensionApp.source_map",
"core-path": "BuildLabExtensionApp.core_path",
}
def run_task(self):
self.extra_args = self.extra_args or [os.getcwd()]
build_labextension(
self.extra_args[0],
logger=self.log,
development=self.development,
static_url=self.static_url or None,
source_map=self.source_map,
core_path=self.core_path or None,
)
class WatchLabExtensionApp(BaseExtensionApp):
description = "(developer) Watch labextension"
development = Bool(True, config=True, help="Build in development mode")
source_map = Bool(False, config=True, help="Generate source maps")
core_path = Unicode(
os.path.join(HERE, "staging"),
config=True,
help="Directory containing core application package.json file",
)
aliases = {
"development": "BuildLabExtensionApp.development",
"source-map": "BuildLabExtensionApp.source_map",
"core-path": "BuildLabExtensionApp.core_path",
}
def run_task(self):
self.extra_args = self.extra_args or [os.getcwd()]
labextensions_path = self.labextensions_path
watch_labextension(
self.extra_args[0],
labextensions_path,
logger=self.log,
development=self.development,
source_map=self.source_map,
core_path=self.core_path or None,
)
class UpdateLabExtensionApp(BaseExtensionApp):
description = "Update labextension(s)"
flags = update_flags
all = Bool(False, config=True, help="Whether to update all extensions")
def run_task(self):
if not self.all and not self.extra_args:
self.log.warning(
"Specify an extension to update, or use --all to update all extensions"
)
return False
app_options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
)
if self.all:
return update_extension(all_=True, app_options=app_options)
return any([update_extension(name=arg, app_options=app_options) for arg in self.extra_args])
class LinkLabExtensionApp(BaseExtensionApp):
description = """
Link local npm packages that are not lab extensions.
Links a package to the JupyterLab build process. A linked
package is manually re-installed from its source location when
`jupyter lab build` is run.
"""
should_build = Bool(True, config=True, help="Whether to build the app after the action")
def run_task(self):
self.extra_args = self.extra_args or [os.getcwd()]
options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
labextensions_path=self.labextensions_path,
core_config=self.core_config,
)
return any([link_package(arg, app_options=options) for arg in self.extra_args])
class UnlinkLabExtensionApp(BaseExtensionApp):
description = "Unlink packages by name or path"
def run_task(self):
self.extra_args = self.extra_args or [os.getcwd()]
options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
labextensions_path=self.labextensions_path,
core_config=self.core_config,
)
return any([unlink_package(arg, app_options=options) for arg in self.extra_args])
class UninstallLabExtensionApp(BaseExtensionApp):
description = "Uninstall labextension(s) by name"
flags = uninstall_flags
all = Bool(False, config=True, help="Whether to uninstall all extensions")
def run_task(self):
self.extra_args = self.extra_args or [os.getcwd()]
options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
labextensions_path=self.labextensions_path,
core_config=self.core_config,
)
return any(
[
uninstall_extension(arg, all_=self.all, app_options=options)
for arg in self.extra_args
]
)
class ListLabExtensionsApp(BaseExtensionApp):
description = "List the installed labextensions"
def run_task(self):
list_extensions(
app_options=AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
)
)
class EnableLabExtensionsApp(BaseExtensionApp):
description = "Enable labextension(s) by name"
aliases = enable_aliases
level = Unicode("sys_prefix", help="Level at which to enable: sys_prefix, user, system").tag(
config=True
)
def run_task(self):
app_options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
)
[
enable_extension(arg, app_options=app_options, level=self.level)
for arg in self.extra_args
]
class DisableLabExtensionsApp(BaseExtensionApp):
description = "Disable labextension(s) by name"
aliases = disable_aliases
level = Unicode("sys_prefix", help="Level at which to enable: sys_prefix, user, system").tag(
config=True
)
def run_task(self):
app_options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
)
[
disable_extension(arg, app_options=app_options, level=self.level)
for arg in self.extra_args
]
class CheckLabExtensionsApp(BaseExtensionApp):
description = "Check labextension(s) by name"
flags = check_flags
should_check_installed_only = Bool(
False, config=True, help="Whether it should check only if the extensions is installed"
)
def run_task(self):
app_options = AppOptions(
app_dir=self.app_dir,
logger=self.log,
core_config=self.core_config,
labextensions_path=self.labextensions_path,
)
all_enabled = all(
check_extension(
arg, installed=self.should_check_installed_only, app_options=app_options
)
for arg in self.extra_args
)
if not all_enabled:
self.exit(1)
_EXAMPLES = """
jupyter labextension list # list all configured labextensions
jupyter labextension install <extension name> # install a labextension
jupyter labextension uninstall <extension name> # uninstall a labextension
jupyter labextension develop # (developer) develop a prebuilt labextension
jupyter labextension build # (developer) build a prebuilt labextension
jupyter labextension watch # (developer) watch a prebuilt labextension
"""
class LabExtensionApp(JupyterApp):
"""Base jupyter labextension command entry point"""
name = "jupyter labextension"
version = VERSION
description = "Work with JupyterLab extensions"
examples = _EXAMPLES
subcommands = dict(
install=(InstallLabExtensionApp, "Install labextension(s)"),
update=(UpdateLabExtensionApp, "Update labextension(s)"),
uninstall=(UninstallLabExtensionApp, "Uninstall labextension(s)"),
list=(ListLabExtensionsApp, "List labextensions"),
link=(LinkLabExtensionApp, "Link labextension(s)"),
unlink=(UnlinkLabExtensionApp, "Unlink labextension(s)"),
enable=(EnableLabExtensionsApp, "Enable labextension(s)"),
disable=(DisableLabExtensionsApp, "Disable labextension(s)"),
check=(CheckLabExtensionsApp, "Check labextension(s)"),
develop=(DevelopLabExtensionApp, "(developer) Develop labextension(s)"),
build=(BuildLabExtensionApp, "(developer) Build labextension"),
watch=(WatchLabExtensionApp, "(developer) Watch labextension"),
)
def start(self):
"""Perform the App's functions as configured"""
super(LabExtensionApp, self).start()
# The above should have called a subcommand and raised NoStart; if we
# get here, it didn't, so we should self.log.info a message.
subcmds = ", ".join(sorted(self.subcommands))
self.exit("Please supply at least one subcommand: %s" % subcmds)
main = LabExtensionApp.launch_instance
if __name__ == "__main__":
sys.exit(main())