forked from jupyterlab/jupyterlab
/
commands.py
313 lines (242 loc) · 8.84 KB
/
commands.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
# coding: utf-8
"""JupyterLab entry points"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import json
import pipes
import os
import glob
from uuid import uuid4
from os import path as osp
from os.path import join as pjoin
from subprocess import check_output, CalledProcessError
import shutil
import sys
import tarfile
from jupyter_core.paths import ENV_JUPYTER_PATH
from ._version import __version__
if sys.platform == 'win32':
from subprocess import list2cmdline
else:
def list2cmdline(cmd_list):
return ' '.join(map(pipes.quote, cmd_list))
here = osp.dirname(osp.abspath(__file__))
def get_app_dir(app_dir=None):
"""Get the configured JupyterLab app directory.
"""
app_dir = app_dir or os.environ.get('JUPYTERLAB_DIR')
app_dir = app_dir or pjoin(ENV_JUPYTER_PATH[0], 'lab')
return os.path.realpath(app_dir)
def run(cmd, **kwargs):
"""Run a command in the given working directory.
"""
print('> ' + list2cmdline(cmd))
kwargs.setdefault('shell', sys.platform == 'win32')
kwargs.setdefault('env', os.environ)
try:
return check_output(cmd, **kwargs)
except CalledProcessError as error:
raise error
def install_extension(extension, app_dir=None):
"""Install an extension package into JupyterLab.
Follows the semantics of https://docs.npmjs.com/cli/install.
The extension is first validated.
If link is true, the source directory is linked using `npm link`.
"""
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot install extensions in core app')
extension = _normalize_path(extension)
_ensure_package(app_dir)
target = pjoin(app_dir, 'extensions')
# npm pack the extension
output = run(['npm', 'pack', extension], cwd=target)
name = output.decode('utf8').splitlines()[-1]
data = _read_package(pjoin(target, name))
_validate_package(data, extension)
def link_package(path, app_dir=None):
"""Link a package against the JupyterLab build.
"""
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot link packages in core app')
path = _normalize_path(path)
_ensure_package(app_dir)
# Verify the package.json data.
pkg_path = osp.join(path, 'package.json')
if not osp.exists(pkg_path):
msg = 'Linked package must point to a directory with package.json'
raise ValueError(msg)
with open(pkg_path) as fid:
data = json.load(fid)
is_extension = _is_extension(data)
if is_extension:
install_extension(path, app_dir)
linked = _get_linked_packages(app_dir)
linked[data['name']] = path
target = pjoin(app_dir, 'settings', 'linked_packages.json')
with open(target, 'w') as fid:
json.dump(linked, fid)
def unlink_package(package, app_dir=None):
"""Unlink a package from JupyterLab by path or name.
"""
package = _normalize_path(package)
name = None
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot link packages in core app')
linked = _get_linked_packages(app_dir)
for (key, value) in linked.items():
if value == package or key == package:
name = key
break
if not name:
print('No package matching "%s" is linked' % package)
return False
del linked[name]
target = pjoin(app_dir, 'settings', 'linked_packages.json')
with open(target, 'w') as fid:
json.dump(linked, fid)
extensions = _get_extensions(app_dir)
if name in extensions:
uninstall_extension(name, app_dir)
return True
def uninstall_extension(name, app_dir=None):
"""Uninstall an extension by name.
"""
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot install packages in core app')
for (extname, path) in _get_extensions(app_dir).items():
if extname == name:
print('Uninstalling %s from %s' % (name, os.path.dirname(path)))
os.remove(path)
return True
print('No labextension named "%s" installed' % name)
return False
def list_extensions(app_dir=None):
"""List installed extensions.
"""
app_dir = get_app_dir(app_dir)
return sorted(_get_extensions(app_dir).keys())
def clean(app_dir=None):
"""Clean the JupyterLab application directory."""
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot clean the core app')
for name in ['static', 'build']:
target = pjoin(app_dir, name)
if osp.exists(target):
shutil.rmtree(target)
def build(app_dir=None, name=None, version=None, publicPath=None):
"""Build the JupyterLab application."""
# Set up the build directory.
app_dir = get_app_dir(app_dir)
if app_dir == here:
raise ValueError('Cannot build extensions in the core app')
_ensure_package(app_dir, name, version, publicPath)
staging = pjoin(app_dir, 'staging')
# Make sure packages are installed.
run(['npm', 'install'], cwd=staging)
# Install the linked extensions.
for path in _get_linked_packages(app_dir).values():
run(['npm', 'install', path], cwd=staging)
# Build the app.
run(['npm', 'run', 'build'], cwd=staging)
# Move the app to the static dir.
static = pjoin(app_dir, 'static')
if os.path.exists(static):
shutil.rmtree(static)
shutil.copytree(pjoin(staging, 'build'), static)
def _ensure_package(app_dir, name='JupyterLab', version=None, publicPath=None):
"""Make sure the build dir is set up.
"""
if not os.path.exists(pjoin(app_dir, 'extensions')):
try:
os.makedirs(pjoin(app_dir, 'extensions'))
except OSError as e:
if e.errno != errno.EEXIST:
raise
settings = pjoin(app_dir, 'settings')
if not os.path.exists(settings):
try:
os.makedirs(settings)
except OSError as e:
if e.errno != errno.EEXIST:
raise
staging = pjoin(app_dir, 'staging')
if not os.path.exists(staging):
os.makedirs(staging)
for name in ['index.template.js', 'webpack.config.js']:
dest = pjoin(staging, name)
shutil.copy2(pjoin(here, name), dest)
# Template the package.json file.
pkg_path = pjoin(here, 'package.template.json')
with open(pkg_path) as fid:
data = json.load(fid)
extensions = _get_extensions(app_dir)
for (key, value) in extensions.items():
data['dependencies'][key] = value
data['jupyterlab']['extensions'].append(key)
data['jupyterlab']['name'] = name
data['version'] = version or __version__
publicPath = publicPath or uuid4().hex
if not publicPath.endswith('/'):
publicPath += '/'
data['jupyterlab']['publicPath'] = publicPath
data['scripts']['build'] = 'webpack'
pkg_path = pjoin(staging, 'package.json')
with open(pkg_path, 'w') as fid:
json.dump(data, fid, indent=4)
def _is_extension(data):
"""Detect if a package is an extension using its metadata.
"""
if 'jupyterlab' not in data:
return False
if not isinstance(data['jupyterlab'], dict):
return False
return data['jupyterlab'].get('extension', False)
def _validate_package(data, extension):
"""Validate package.json data.
"""
msg = '%s is not a valid JupyterLab extension' % extension
if not _is_extension(data):
raise ValueError(msg)
def _get_extensions(app_dir):
"""Get the extensions in a given app dir.
"""
extensions = dict()
# Look in sys_prefix and app_dir if different
sys_path = pjoin(ENV_JUPYTER_PATH[0], 'lab', 'extensions')
for target in glob.glob(pjoin(sys_path, '*.tgz')):
data = _read_package(target)
extensions[data['name']] = os.path.realpath(target)
app_path = pjoin(app_dir, 'extensions')
if app_path == sys_path or not os.path.exists(app_path):
return extensions
for target in glob.glob(pjoin(app_path, '*.tgz')):
data = _read_package(target)
extensions[data['name']] = os.path.realpath(target)
return extensions
def _read_package(target):
"""Read the package data in a given target tarball.
"""
tar = tarfile.open(target, "r:gz")
f = tar.extractfile('package/package.json')
return json.loads(f.read().decode('utf8'))
def _get_linked_packages(app_dir=None):
"""Get the linked packages in the app dir.
"""
app_dir = get_app_dir(app_dir)
extensions = dict()
link_file = pjoin(app_dir, 'settings', 'linked_packages.json')
if not os.path.exists(link_file):
return extensions
with open(link_file) as fid:
return json.load(fid)
def _normalize_path(extension):
extension = osp.expanduser(extension)
if osp.exists(extension):
extension = osp.abspath(extension)
return extension