Show More
@@ -1,351 +1,349 b'' | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """Utilities for installing Javascript extensions for the notebook""" |
|
2 | """Utilities for installing Javascript extensions for the notebook""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | from __future__ import print_function |
|
7 | from __future__ import print_function | |
8 |
|
8 | |||
9 | import os |
|
9 | import os | |
10 | import shutil |
|
10 | import shutil | |
11 | import sys |
|
11 | import sys | |
12 | import tarfile |
|
12 | import tarfile | |
13 | import zipfile |
|
13 | import zipfile | |
14 | import uuid |
|
14 | import uuid | |
15 | from os.path import basename, join as pjoin |
|
15 | from os.path import basename, join as pjoin | |
16 |
|
16 | |||
17 | # Deferred imports |
|
17 | # Deferred imports | |
18 | try: |
|
18 | try: | |
19 | from urllib.parse import urlparse # Py3 |
|
19 | from urllib.parse import urlparse # Py3 | |
20 | from urllib.request import urlretrieve |
|
20 | from urllib.request import urlretrieve | |
21 | except ImportError: |
|
21 | except ImportError: | |
22 | from urlparse import urlparse |
|
22 | from urlparse import urlparse | |
23 | from urllib import urlretrieve |
|
23 | from urllib import urlretrieve | |
24 |
|
24 | |||
25 | from IPython.utils.path import get_ipython_dir, ensure_dir_exists |
|
25 | from IPython.utils.path import get_ipython_dir, ensure_dir_exists | |
26 | from IPython.utils.py3compat import string_types, cast_unicode_py2 |
|
26 | from IPython.utils.py3compat import string_types, cast_unicode_py2 | |
27 | from IPython.utils.tempdir import TemporaryDirectory |
|
27 | from IPython.utils.tempdir import TemporaryDirectory | |
28 |
|
28 | |||
29 | class ArgumentConflict(ValueError): |
|
29 | class ArgumentConflict(ValueError): | |
30 | pass |
|
30 | pass | |
31 |
|
31 | |||
32 | # Packagers: modify the next block if you store system-installed nbextensions elsewhere (unlikely) |
|
32 | # Packagers: modify the next block if you store system-installed nbextensions elsewhere (unlikely) | |
33 | SYSTEM_NBEXTENSIONS_DIRS = [] |
|
33 | SYSTEM_NBEXTENSIONS_DIRS = [] | |
34 |
|
34 | |||
35 | if os.name == 'nt': |
|
35 | if os.name == 'nt': | |
36 | programdata = os.environ.get('PROGRAMDATA', None) |
|
36 | programdata = os.environ.get('PROGRAMDATA', None) | |
37 | if programdata: # PROGRAMDATA is not defined by default on XP. |
|
37 | if programdata: # PROGRAMDATA is not defined by default on XP. | |
38 | SYSTEM_NBEXTENSIONS_DIRS = [pjoin(programdata, 'jupyter', 'nbextensions')] |
|
38 | SYSTEM_NBEXTENSIONS_DIRS = [pjoin(programdata, 'jupyter', 'nbextensions')] | |
39 | prefixes = [] |
|
39 | prefixes = [] | |
40 | else: |
|
40 | else: | |
41 | prefixes = [os.path.sep + pjoin('usr', 'local'), os.path.sep + 'usr'] |
|
41 | prefixes = [os.path.sep + pjoin('usr', 'local'), os.path.sep + 'usr'] | |
42 |
|
42 | |||
43 | # add sys.prefix at the front |
|
43 | # add sys.prefix at the front | |
44 | if sys.prefix not in prefixes: |
|
44 | if sys.prefix not in prefixes: | |
45 | prefixes.insert(0, sys.prefix) |
|
45 | prefixes.insert(0, sys.prefix) | |
46 |
|
46 | |||
47 | for prefix in prefixes: |
|
47 | for prefix in prefixes: | |
48 | nbext = pjoin(prefix, 'share', 'jupyter', 'nbextensions') |
|
48 | nbext = pjoin(prefix, 'share', 'jupyter', 'nbextensions') | |
49 | if nbext not in SYSTEM_NBEXTENSIONS_DIRS: |
|
49 | if nbext not in SYSTEM_NBEXTENSIONS_DIRS: | |
50 | SYSTEM_NBEXTENSIONS_DIRS.append(nbext) |
|
50 | SYSTEM_NBEXTENSIONS_DIRS.append(nbext) | |
51 |
|
51 | |||
52 | if os.name == 'nt': |
|
52 | if os.name == 'nt': | |
53 | # PROGRAMDATA |
|
53 | # PROGRAMDATA | |
54 | SYSTEM_NBEXTENSIONS_INSTALL_DIR = SYSTEM_NBEXTENSIONS_DIRS[-1] |
|
54 | SYSTEM_NBEXTENSIONS_INSTALL_DIR = SYSTEM_NBEXTENSIONS_DIRS[-1] | |
55 | else: |
|
55 | else: | |
56 | # /usr/local |
|
56 | # /usr/local | |
57 | SYSTEM_NBEXTENSIONS_INSTALL_DIR = SYSTEM_NBEXTENSIONS_DIRS[-2] |
|
57 | SYSTEM_NBEXTENSIONS_INSTALL_DIR = SYSTEM_NBEXTENSIONS_DIRS[-2] | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | def _should_copy(src, dest, verbose=1): |
|
60 | def _should_copy(src, dest, verbose=1): | |
61 | """should a file be copied?""" |
|
61 | """should a file be copied?""" | |
62 | if not os.path.exists(dest): |
|
62 | if not os.path.exists(dest): | |
63 | return True |
|
63 | return True | |
64 | if os.stat(src).st_mtime - os.stat(dest).st_mtime > 1e-6: |
|
64 | if os.stat(src).st_mtime - os.stat(dest).st_mtime > 1e-6: | |
65 | # we add a fudge factor to work around a bug in python 2.x |
|
65 | # we add a fudge factor to work around a bug in python 2.x | |
66 | # that was fixed in python 3.x: http://bugs.python.org/issue12904 |
|
66 | # that was fixed in python 3.x: http://bugs.python.org/issue12904 | |
67 | if verbose >= 2: |
|
67 | if verbose >= 2: | |
68 | print("%s is out of date" % dest) |
|
68 | print("%s is out of date" % dest) | |
69 | return True |
|
69 | return True | |
70 | if verbose >= 2: |
|
70 | if verbose >= 2: | |
71 | print("%s is up to date" % dest) |
|
71 | print("%s is up to date" % dest) | |
72 | return False |
|
72 | return False | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | def _maybe_copy(src, dest, verbose=1): |
|
75 | def _maybe_copy(src, dest, verbose=1): | |
76 | """copy a file if it needs updating""" |
|
76 | """copy a file if it needs updating""" | |
77 | if _should_copy(src, dest, verbose): |
|
77 | if _should_copy(src, dest, verbose): | |
78 | if verbose >= 1: |
|
78 | if verbose >= 1: | |
79 | print("copying %s -> %s" % (src, dest)) |
|
79 | print("copying %s -> %s" % (src, dest)) | |
80 | shutil.copy2(src, dest) |
|
80 | shutil.copy2(src, dest) | |
81 |
|
81 | |||
82 | def _safe_is_tarfile(path): |
|
82 | def _safe_is_tarfile(path): | |
83 | """safe version of is_tarfile, return False on IOError""" |
|
83 | """safe version of is_tarfile, return False on IOError""" | |
84 | try: |
|
84 | try: | |
85 | return tarfile.is_tarfile(path) |
|
85 | return tarfile.is_tarfile(path) | |
86 | except IOError: |
|
86 | except IOError: | |
87 | return False |
|
87 | return False | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | def _get_nbext_dir(nbextensions_dir=None, user=False, prefix=None): |
|
90 | def _get_nbext_dir(nbextensions_dir=None, user=False, prefix=None): | |
91 | """Return the nbextension directory specified""" |
|
91 | """Return the nbextension directory specified""" | |
92 | if sum(map(bool, [user, prefix, nbextensions_dir])) > 1: |
|
92 | if sum(map(bool, [user, prefix, nbextensions_dir])) > 1: | |
93 | raise ArgumentConflict("Cannot specify more than one of user, prefix, or nbextensions_dir.") |
|
93 | raise ArgumentConflict("Cannot specify more than one of user, prefix, or nbextensions_dir.") | |
94 | if user: |
|
94 | if user: | |
95 | nbext = pjoin(get_ipython_dir(), u'nbextensions') |
|
95 | nbext = pjoin(get_ipython_dir(), u'nbextensions') | |
96 | else: |
|
96 | else: | |
97 | if prefix: |
|
97 | if prefix: | |
98 | nbext = pjoin(prefix, 'share', 'jupyter', 'nbextensions') |
|
98 | nbext = pjoin(prefix, 'share', 'jupyter', 'nbextensions') | |
99 | elif nbextensions_dir: |
|
99 | elif nbextensions_dir: | |
100 | nbext = nbextensions_dir |
|
100 | nbext = nbextensions_dir | |
101 | else: |
|
101 | else: | |
102 | nbext = SYSTEM_NBEXTENSIONS_INSTALL_DIR |
|
102 | nbext = SYSTEM_NBEXTENSIONS_INSTALL_DIR | |
103 | return nbext |
|
103 | return nbext | |
104 |
|
104 | |||
105 |
|
105 | |||
106 | def check_nbextension(files, user=False, prefix=None, nbextensions_dir=None): |
|
106 | def check_nbextension(files, user=False, prefix=None, nbextensions_dir=None): | |
107 | """Check whether nbextension files have been installed |
|
107 | """Check whether nbextension files have been installed | |
108 |
|
108 | |||
109 | Returns True if all files are found, False if any are missing. |
|
109 | Returns True if all files are found, False if any are missing. | |
110 |
|
110 | |||
111 | Parameters |
|
111 | Parameters | |
112 | ---------- |
|
112 | ---------- | |
113 |
|
113 | |||
114 | files : list(paths) |
|
114 | files : list(paths) | |
115 | a list of relative paths within nbextensions. |
|
115 | a list of relative paths within nbextensions. | |
116 | user : bool [default: False] |
|
116 | user : bool [default: False] | |
117 | Whether to check the user's .ipython/nbextensions directory. |
|
117 | Whether to check the user's .ipython/nbextensions directory. | |
118 | Otherwise check a system-wide install (e.g. /usr/local/share/jupyter/nbextensions). |
|
118 | Otherwise check a system-wide install (e.g. /usr/local/share/jupyter/nbextensions). | |
119 | prefix : str [optional] |
|
119 | prefix : str [optional] | |
120 | Specify install prefix, if it should differ from default (e.g. /usr/local). |
|
120 | Specify install prefix, if it should differ from default (e.g. /usr/local). | |
121 | Will check prefix/share/jupyter/nbextensions |
|
121 | Will check prefix/share/jupyter/nbextensions | |
122 | nbextensions_dir : str [optional] |
|
122 | nbextensions_dir : str [optional] | |
123 | Specify absolute path of nbextensions directory explicitly. |
|
123 | Specify absolute path of nbextensions directory explicitly. | |
124 | """ |
|
124 | """ | |
125 | nbext = _get_nbext_dir(nbextensions_dir, user, prefix) |
|
125 | nbext = _get_nbext_dir(nbextensions_dir, user, prefix) | |
126 | # make sure nbextensions dir exists |
|
126 | # make sure nbextensions dir exists | |
127 | if not os.path.exists(nbext): |
|
127 | if not os.path.exists(nbext): | |
128 | return False |
|
128 | return False | |
129 |
|
129 | |||
130 | if isinstance(files, string_types): |
|
130 | if isinstance(files, string_types): | |
131 | # one file given, turn it into a list |
|
131 | # one file given, turn it into a list | |
132 | files = [files] |
|
132 | files = [files] | |
133 |
|
133 | |||
134 | return all(os.path.exists(pjoin(nbext, f)) for f in files) |
|
134 | return all(os.path.exists(pjoin(nbext, f)) for f in files) | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | def install_nbextension(path, overwrite=False, symlink=False, user=False, prefix=None, nbextensions_dir=None, destination=None, verbose=1): |
|
137 | def install_nbextension(path, overwrite=False, symlink=False, user=False, prefix=None, nbextensions_dir=None, destination=None, verbose=1): | |
138 | """Install a Javascript extension for the notebook |
|
138 | """Install a Javascript extension for the notebook | |
139 |
|
139 | |||
140 | Stages files and/or directories into the nbextensions directory. |
|
140 | Stages files and/or directories into the nbextensions directory. | |
141 | By default, this compares modification time, and only stages files that need updating. |
|
141 | By default, this compares modification time, and only stages files that need updating. | |
142 | If `overwrite` is specified, matching files are purged before proceeding. |
|
142 | If `overwrite` is specified, matching files are purged before proceeding. | |
143 |
|
143 | |||
144 | Parameters |
|
144 | Parameters | |
145 | ---------- |
|
145 | ---------- | |
146 |
|
146 | |||
147 | path : path to file, directory, zip or tarball archive, or URL to install |
|
147 | path : path to file, directory, zip or tarball archive, or URL to install | |
148 | By default, the file will be installed with its base name, so '/path/to/foo' |
|
148 | By default, the file will be installed with its base name, so '/path/to/foo' | |
149 | will install to 'nbextensions/foo'. See the destination argument below to change this. |
|
149 | will install to 'nbextensions/foo'. See the destination argument below to change this. | |
150 | Archives (zip or tarballs) will be extracted into the nbextensions directory. |
|
150 | Archives (zip or tarballs) will be extracted into the nbextensions directory. | |
151 | overwrite : bool [default: False] |
|
151 | overwrite : bool [default: False] | |
152 | If True, always install the files, regardless of what may already be installed. |
|
152 | If True, always install the files, regardless of what may already be installed. | |
153 | symlink : bool [default: False] |
|
153 | symlink : bool [default: False] | |
154 | If True, create a symlink in nbextensions, rather than copying files. |
|
154 | If True, create a symlink in nbextensions, rather than copying files. | |
155 | Not allowed with URLs or archives. Windows support for symlinks requires |
|
155 | Not allowed with URLs or archives. Windows support for symlinks requires | |
156 | Vista or above, Python 3, and a permission bit which only admin users |
|
156 | Vista or above, Python 3, and a permission bit which only admin users | |
157 | have by default, so don't rely on it. |
|
157 | have by default, so don't rely on it. | |
158 | user : bool [default: False] |
|
158 | user : bool [default: False] | |
159 | Whether to install to the user's .ipython/nbextensions directory. |
|
159 | Whether to install to the user's .ipython/nbextensions directory. | |
160 | Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/nbextensions). |
|
160 | Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/nbextensions). | |
161 | prefix : str [optional] |
|
161 | prefix : str [optional] | |
162 | Specify install prefix, if it should differ from default (e.g. /usr/local). |
|
162 | Specify install prefix, if it should differ from default (e.g. /usr/local). | |
163 | Will install to prefix/share/jupyter/nbextensions |
|
163 | Will install to prefix/share/jupyter/nbextensions | |
164 | nbextensions_dir : str [optional] |
|
164 | nbextensions_dir : str [optional] | |
165 | Specify absolute path of nbextensions directory explicitly. |
|
165 | Specify absolute path of nbextensions directory explicitly. | |
166 | destination : str [optional] |
|
166 | destination : str [optional] | |
167 | name the nbextension is installed to. For example, if destination is 'foo', then |
|
167 | name the nbextension is installed to. For example, if destination is 'foo', then | |
168 | the source file will be installed to 'nbextensions/foo', regardless of the source name. |
|
168 | the source file will be installed to 'nbextensions/foo', regardless of the source name. | |
169 | This cannot be specified if an archive is given as the source. |
|
169 | This cannot be specified if an archive is given as the source. | |
170 | verbose : int [default: 1] |
|
170 | verbose : int [default: 1] | |
171 | Set verbosity level. The default is 1, where file actions are printed. |
|
171 | Set verbosity level. The default is 1, where file actions are printed. | |
172 | set verbose=2 for more output, or verbose=0 for silence. |
|
172 | set verbose=2 for more output, or verbose=0 for silence. | |
173 | """ |
|
173 | """ | |
174 | nbext = _get_nbext_dir(nbextensions_dir, user, prefix) |
|
174 | nbext = _get_nbext_dir(nbextensions_dir, user, prefix) | |
175 | # make sure nbextensions dir exists |
|
175 | # make sure nbextensions dir exists | |
176 | ensure_dir_exists(nbext) |
|
176 | ensure_dir_exists(nbext) | |
177 |
|
177 | |||
178 | if isinstance(path, (list, tuple)): |
|
178 | if isinstance(path, (list, tuple)): | |
179 | raise TypeError("path must be a string pointing to a single extension to install; call this function multiple times to install multiple extensions") |
|
179 | raise TypeError("path must be a string pointing to a single extension to install; call this function multiple times to install multiple extensions") | |
180 |
|
180 | |||
181 | path = cast_unicode_py2(path) |
|
181 | path = cast_unicode_py2(path) | |
182 |
|
182 | |||
183 | if path.startswith(('https://', 'http://')): |
|
183 | if path.startswith(('https://', 'http://')): | |
184 | if symlink: |
|
184 | if symlink: | |
185 | raise ValueError("Cannot symlink from URLs") |
|
185 | raise ValueError("Cannot symlink from URLs") | |
186 | # Given a URL, download it |
|
186 | # Given a URL, download it | |
187 | with TemporaryDirectory() as td: |
|
187 | with TemporaryDirectory() as td: | |
188 | filename = urlparse(path).path.split('/')[-1] |
|
188 | filename = urlparse(path).path.split('/')[-1] | |
189 | local_path = os.path.join(td, filename) |
|
189 | local_path = os.path.join(td, filename) | |
190 | if verbose >= 1: |
|
190 | if verbose >= 1: | |
191 | print("downloading %s to %s" % (path, local_path)) |
|
191 | print("downloading %s to %s" % (path, local_path)) | |
192 | urlretrieve(path, local_path) |
|
192 | urlretrieve(path, local_path) | |
193 | # now install from the local copy |
|
193 | # now install from the local copy | |
194 | install_nbextension(local_path, overwrite=overwrite, symlink=symlink, nbextensions_dir=nbext, destination=destination, verbose=verbose) |
|
194 | install_nbextension(local_path, overwrite=overwrite, symlink=symlink, nbextensions_dir=nbext, destination=destination, verbose=verbose) | |
195 | elif path.endswith('.zip') or _safe_is_tarfile(path): |
|
195 | elif path.endswith('.zip') or _safe_is_tarfile(path): | |
196 | if symlink: |
|
196 | if symlink: | |
197 | raise ValueError("Cannot symlink from archives") |
|
197 | raise ValueError("Cannot symlink from archives") | |
198 | if destination: |
|
198 | if destination: | |
199 | raise ValueError("Cannot give destination for archives") |
|
199 | raise ValueError("Cannot give destination for archives") | |
200 | if verbose >= 1: |
|
200 | if verbose >= 1: | |
201 | print("extracting %s to %s" % (path, nbext)) |
|
201 | print("extracting %s to %s" % (path, nbext)) | |
202 |
|
202 | |||
203 | if path.endswith('.zip'): |
|
203 | if path.endswith('.zip'): | |
204 | archive = zipfile.ZipFile(path) |
|
204 | archive = zipfile.ZipFile(path) | |
205 | elif _safe_is_tarfile(path): |
|
205 | elif _safe_is_tarfile(path): | |
206 | archive = tarfile.open(path) |
|
206 | archive = tarfile.open(path) | |
207 | else: |
|
|||
208 | raise ValueError("Could not extract archive") |
|
|||
209 | archive.extractall(nbext) |
|
207 | archive.extractall(nbext) | |
210 | archive.close() |
|
208 | archive.close() | |
211 | else: |
|
209 | else: | |
212 | if not destination: |
|
210 | if not destination: | |
213 | destination = basename(path) |
|
211 | destination = basename(path) | |
214 | full_dest = pjoin(nbext, destination) |
|
212 | full_dest = pjoin(nbext, destination) | |
215 | if overwrite and os.path.exists(full_dest): |
|
213 | if overwrite and os.path.exists(full_dest): | |
216 | if verbose >= 1: |
|
214 | if verbose >= 1: | |
217 | print("removing %s" % full_dest) |
|
215 | print("removing %s" % full_dest) | |
218 | if os.path.isdir(full_dest) and not os.path.islink(full_dest): |
|
216 | if os.path.isdir(full_dest) and not os.path.islink(full_dest): | |
219 | shutil.rmtree(full_dest) |
|
217 | shutil.rmtree(full_dest) | |
220 | else: |
|
218 | else: | |
221 | os.remove(full_dest) |
|
219 | os.remove(full_dest) | |
222 |
|
220 | |||
223 | if symlink: |
|
221 | if symlink: | |
224 | path = os.path.abspath(path) |
|
222 | path = os.path.abspath(path) | |
225 | if not os.path.exists(full_dest): |
|
223 | if not os.path.exists(full_dest): | |
226 | if verbose >= 1: |
|
224 | if verbose >= 1: | |
227 | print("symlink %s -> %s" % (full_dest, path)) |
|
225 | print("symlink %s -> %s" % (full_dest, path)) | |
228 | os.symlink(path, full_dest) |
|
226 | os.symlink(path, full_dest) | |
229 | elif os.path.isdir(path): |
|
227 | elif os.path.isdir(path): | |
230 | path = pjoin(os.path.abspath(path), '') # end in path separator |
|
228 | path = pjoin(os.path.abspath(path), '') # end in path separator | |
231 | for parent, dirs, files in os.walk(path): |
|
229 | for parent, dirs, files in os.walk(path): | |
232 | dest_dir = pjoin(full_dest, parent[len(path):]) |
|
230 | dest_dir = pjoin(full_dest, parent[len(path):]) | |
233 | if not os.path.exists(dest_dir): |
|
231 | if not os.path.exists(dest_dir): | |
234 | if verbose >= 2: |
|
232 | if verbose >= 2: | |
235 | print("making directory %s" % dest_dir) |
|
233 | print("making directory %s" % dest_dir) | |
236 | os.makedirs(dest_dir) |
|
234 | os.makedirs(dest_dir) | |
237 | for file in files: |
|
235 | for file in files: | |
238 | src = pjoin(parent, file) |
|
236 | src = pjoin(parent, file) | |
239 | # print("%r, %r" % (dest_dir, file)) |
|
237 | # print("%r, %r" % (dest_dir, file)) | |
240 | dest_file = pjoin(dest_dir, file) |
|
238 | dest_file = pjoin(dest_dir, file) | |
241 | _maybe_copy(src, dest_file, verbose) |
|
239 | _maybe_copy(src, dest_file, verbose) | |
242 | else: |
|
240 | else: | |
243 | src = path |
|
241 | src = path | |
244 | _maybe_copy(src, full_dest, verbose) |
|
242 | _maybe_copy(src, full_dest, verbose) | |
245 |
|
243 | |||
246 | #---------------------------------------------------------------------- |
|
244 | #---------------------------------------------------------------------- | |
247 | # install nbextension app |
|
245 | # install nbextension app | |
248 | #---------------------------------------------------------------------- |
|
246 | #---------------------------------------------------------------------- | |
249 |
|
247 | |||
250 | from IPython.utils.traitlets import Bool, Enum, Unicode, TraitError |
|
248 | from IPython.utils.traitlets import Bool, Enum, Unicode, TraitError | |
251 | from IPython.core.application import BaseIPythonApplication |
|
249 | from IPython.core.application import BaseIPythonApplication | |
252 |
|
250 | |||
253 | flags = { |
|
251 | flags = { | |
254 | "overwrite" : ({ |
|
252 | "overwrite" : ({ | |
255 | "NBExtensionApp" : { |
|
253 | "NBExtensionApp" : { | |
256 | "overwrite" : True, |
|
254 | "overwrite" : True, | |
257 | }}, "Force overwrite of existing files" |
|
255 | }}, "Force overwrite of existing files" | |
258 | ), |
|
256 | ), | |
259 | "debug" : ({ |
|
257 | "debug" : ({ | |
260 | "NBExtensionApp" : { |
|
258 | "NBExtensionApp" : { | |
261 | "verbose" : 2, |
|
259 | "verbose" : 2, | |
262 | }}, "Extra output" |
|
260 | }}, "Extra output" | |
263 | ), |
|
261 | ), | |
264 | "quiet" : ({ |
|
262 | "quiet" : ({ | |
265 | "NBExtensionApp" : { |
|
263 | "NBExtensionApp" : { | |
266 | "verbose" : 0, |
|
264 | "verbose" : 0, | |
267 | }}, "Minimal output" |
|
265 | }}, "Minimal output" | |
268 | ), |
|
266 | ), | |
269 | "symlink" : ({ |
|
267 | "symlink" : ({ | |
270 | "NBExtensionApp" : { |
|
268 | "NBExtensionApp" : { | |
271 | "symlink" : True, |
|
269 | "symlink" : True, | |
272 | }}, "Create symlink instead of copying files" |
|
270 | }}, "Create symlink instead of copying files" | |
273 | ), |
|
271 | ), | |
274 | "user" : ({ |
|
272 | "user" : ({ | |
275 | "NBExtensionApp" : { |
|
273 | "NBExtensionApp" : { | |
276 | "user" : True, |
|
274 | "user" : True, | |
277 | }}, "Install to the user's IPython directory" |
|
275 | }}, "Install to the user's IPython directory" | |
278 | ), |
|
276 | ), | |
279 | } |
|
277 | } | |
280 | flags['s'] = flags['symlink'] |
|
278 | flags['s'] = flags['symlink'] | |
281 |
|
279 | |||
282 | aliases = { |
|
280 | aliases = { | |
283 | "ipython-dir" : "NBExtensionApp.ipython_dir", |
|
281 | "ipython-dir" : "NBExtensionApp.ipython_dir", | |
284 | "prefix" : "NBExtensionApp.prefix", |
|
282 | "prefix" : "NBExtensionApp.prefix", | |
285 | "nbextensions" : "NBExtensionApp.nbextensions_dir", |
|
283 | "nbextensions" : "NBExtensionApp.nbextensions_dir", | |
286 | "destination" : "NBExtensionApp.destination", |
|
284 | "destination" : "NBExtensionApp.destination", | |
287 | } |
|
285 | } | |
288 |
|
286 | |||
289 | class NBExtensionApp(BaseIPythonApplication): |
|
287 | class NBExtensionApp(BaseIPythonApplication): | |
290 | """Entry point for installing notebook extensions""" |
|
288 | """Entry point for installing notebook extensions""" | |
291 |
|
289 | |||
292 | description = """Install IPython notebook extensions |
|
290 | description = """Install IPython notebook extensions | |
293 |
|
291 | |||
294 | Usage |
|
292 | Usage | |
295 |
|
293 | |||
296 | ipython install-nbextension path/url |
|
294 | ipython install-nbextension path/url | |
297 |
|
295 | |||
298 | This copies a file or a folder into the IPython nbextensions directory. |
|
296 | This copies a file or a folder into the IPython nbextensions directory. | |
299 | If a URL is given, it will be downloaded. |
|
297 | If a URL is given, it will be downloaded. | |
300 | If an archive is given, it will be extracted into nbextensions. |
|
298 | If an archive is given, it will be extracted into nbextensions. | |
301 | If the requested files are already up to date, no action is taken |
|
299 | If the requested files are already up to date, no action is taken | |
302 | unless --overwrite is specified. |
|
300 | unless --overwrite is specified. | |
303 | """ |
|
301 | """ | |
304 |
|
302 | |||
305 | examples = """ |
|
303 | examples = """ | |
306 | ipython install-nbextension /path/to/myextension |
|
304 | ipython install-nbextension /path/to/myextension | |
307 | """ |
|
305 | """ | |
308 | aliases = aliases |
|
306 | aliases = aliases | |
309 | flags = flags |
|
307 | flags = flags | |
310 |
|
308 | |||
311 | overwrite = Bool(False, config=True, help="Force overwrite of existing files") |
|
309 | overwrite = Bool(False, config=True, help="Force overwrite of existing files") | |
312 | symlink = Bool(False, config=True, help="Create symlinks instead of copying files") |
|
310 | symlink = Bool(False, config=True, help="Create symlinks instead of copying files") | |
313 | user = Bool(False, config=True, help="Whether to do a user install") |
|
311 | user = Bool(False, config=True, help="Whether to do a user install") | |
314 | prefix = Unicode('', config=True, help="Installation prefix") |
|
312 | prefix = Unicode('', config=True, help="Installation prefix") | |
315 | nbextensions_dir = Unicode('', config=True, help="Full path to nbextensions dir (probably use prefix or user)") |
|
313 | nbextensions_dir = Unicode('', config=True, help="Full path to nbextensions dir (probably use prefix or user)") | |
316 | destination = Unicode('', config=True, help="Destination for the copy or symlink") |
|
314 | destination = Unicode('', config=True, help="Destination for the copy or symlink") | |
317 | verbose = Enum((0,1,2), default_value=1, config=True, |
|
315 | verbose = Enum((0,1,2), default_value=1, config=True, | |
318 | help="Verbosity level" |
|
316 | help="Verbosity level" | |
319 | ) |
|
317 | ) | |
320 |
|
318 | |||
321 | def install_extensions(self): |
|
319 | def install_extensions(self): | |
322 | if len(self.extra_args)>1: |
|
320 | if len(self.extra_args)>1: | |
323 | raise ValueError("only one nbextension allowed at a time. Call multiple times to install multiple extensions.") |
|
321 | raise ValueError("only one nbextension allowed at a time. Call multiple times to install multiple extensions.") | |
324 | install_nbextension(self.extra_args[0], |
|
322 | install_nbextension(self.extra_args[0], | |
325 | overwrite=self.overwrite, |
|
323 | overwrite=self.overwrite, | |
326 | symlink=self.symlink, |
|
324 | symlink=self.symlink, | |
327 | verbose=self.verbose, |
|
325 | verbose=self.verbose, | |
328 | user=self.user, |
|
326 | user=self.user, | |
329 | prefix=self.prefix, |
|
327 | prefix=self.prefix, | |
330 | destination=self.destination, |
|
328 | destination=self.destination, | |
331 | nbextensions_dir=self.nbextensions_dir, |
|
329 | nbextensions_dir=self.nbextensions_dir, | |
332 | ) |
|
330 | ) | |
333 |
|
331 | |||
334 | def start(self): |
|
332 | def start(self): | |
335 | if not self.extra_args: |
|
333 | if not self.extra_args: | |
336 | for nbext in [pjoin(self.ipython_dir, u'nbextensions')] + SYSTEM_NBEXTENSIONS_DIRS: |
|
334 | for nbext in [pjoin(self.ipython_dir, u'nbextensions')] + SYSTEM_NBEXTENSIONS_DIRS: | |
337 | if os.path.exists(nbext): |
|
335 | if os.path.exists(nbext): | |
338 | print("Notebook extensions in %s:" % nbext) |
|
336 | print("Notebook extensions in %s:" % nbext) | |
339 | for ext in os.listdir(nbext): |
|
337 | for ext in os.listdir(nbext): | |
340 | print(u" %s" % ext) |
|
338 | print(u" %s" % ext) | |
341 | else: |
|
339 | else: | |
342 | try: |
|
340 | try: | |
343 | self.install_extensions() |
|
341 | self.install_extensions() | |
344 | except ArgumentConflict as e: |
|
342 | except ArgumentConflict as e: | |
345 | print(str(e), file=sys.stderr) |
|
343 | print(str(e), file=sys.stderr) | |
346 | self.exit(1) |
|
344 | self.exit(1) | |
347 |
|
345 | |||
348 |
|
346 | |||
349 | if __name__ == '__main__': |
|
347 | if __name__ == '__main__': | |
350 | NBExtensionApp.launch_instance() |
|
348 | NBExtensionApp.launch_instance() | |
351 |
|
349 |
General Comments 0
You need to be logged in to leave comments.
Login now