Skip to content

Commit 6998d21

Browse files
authored
Merge pull request #1045 from jcb91/running
improved running-server checks for install
2 parents dd47f82 + b5fe1da commit 6998d21

File tree

6 files changed

+55
-42
lines changed

6 files changed

+55
-42
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ config-editing operations, or only the file-copy operations:
167167
- `jupyter_notebook_config.json` to enable the serverextension
168168
`jupyter_nbextensions_configurator`.
169169

170+
Finally, the `--skip-running-check` option flag is provided in order to allow
171+
the installation to proceed even if a notebook server appears to be currently
172+
running (by default, the install will not be performed if a notebook server
173+
appears to be running).
174+
170175
An analogous `uninstall` command is also provided, to remove all of the
171176
nbextension files from the jupyter directories.
172177

conda.recipe/meta.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ requirements:
2929
- jupyter_nbextensions_configurator >=0.2.4
3030
- nbconvert >=4.2
3131
- notebook >=4.0
32-
- psutil >=2.2.1
3332
- pyyaml
3433
- setuptools
3534
- tornado

docs/source/install.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ config-editing operations, or only the file-copy operations:
9999
- `jupyter_notebook_config.json` to enable the serverextension
100100
`jupyter_nbextensions_configurator`.
101101

102+
Finally, the `--skip-running-check` option flag is provided in order to allow
103+
the installation to proceed even if a notebook server appears to be currently
104+
running (by default, the install will not be performed if a notebook server
105+
appears to be running).
106+
102107
An analogous `uninstall` command is also provided, to remove all of the
103108
nbextension files from the jupyter directories.
104109

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def main():
6969
'jupyter_nbextensions_configurator >=0.2.4',
7070
'nbconvert >=4.2',
7171
'notebook >=4.0',
72-
'psutil >=2.2.1',
7372
'pyyaml',
7473
'tornado',
7574
'traitlets >=4.1',

src/jupyter_contrib_nbextensions/application.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
import jupyter_contrib_nbextensions
1515
from jupyter_contrib_nbextensions.install import (
16-
install, toggle_install_config, toggle_install_files, uninstall,
16+
install, NotebookRunningError, toggle_install_config, toggle_install_files,
17+
uninstall,
1718
)
1819
from jupyter_contrib_nbextensions.migrate import migrate
1920

@@ -71,6 +72,11 @@ class BaseContribNbextensionsInstallApp(BaseContribNbextensionsApp):
7172
{'BaseContribNbextensionsInstallApp': {'symlink': True}},
7273
'Create symlinks for nbextensions instead of copying files'
7374
),
75+
'skip-running-check': (
76+
{'BaseContribNbextensionsInstallApp':
77+
{'skip_running_check': True}},
78+
'Perform actions even if notebook server(s) are already running'
79+
),
7480
}
7581

7682
_conflicting_flagsets = [['--user', '--system', '--sys-prefix'], ]
@@ -91,6 +97,9 @@ class BaseContribNbextensionsInstallApp(BaseContribNbextensionsApp):
9197
'', config=True,
9298
help='Full path to nbextensions dir '
9399
'(consider instead using system, sys_prefix, prefix or user option)')
100+
skip_running_check = Bool(
101+
False, config=True,
102+
help='Perform actions even if notebook server(s) are already running')
94103

95104
def parse_command_line(self, argv=None):
96105
"""
@@ -153,15 +162,23 @@ def start(self):
153162
sys.exit('{} takes no extra arguments'.format(self.name))
154163
self.log.info('{} {}'.format(self.name, ' '.join(self.argv)))
155164
kwargs = dict(
156-
user=self.user, sys_prefix=self.sys_prefix, logger=self.log)
165+
user=self.user, sys_prefix=self.sys_prefix, logger=self.log,
166+
skip_running_check=self.skip_running_check)
157167
kwargs_files = dict(**kwargs)
158168
kwargs_files.update(dict(
159169
prefix=self.prefix, nbextensions_dir=self.nbextensions_dir,
160170
overwrite=self.overwrite, symlink=self.symlink))
161-
if not self.only_config:
162-
toggle_install_files(self._toggle_value, **kwargs_files)
163-
if not self.only_files:
164-
toggle_install_config(self._toggle_value, **kwargs)
171+
try:
172+
if not self.only_config:
173+
toggle_install_files(self._toggle_value, **kwargs_files)
174+
if not self.only_files:
175+
toggle_install_config(self._toggle_value, **kwargs)
176+
except NotebookRunningError as err:
177+
self.log.warn('Error: %s', err)
178+
self.log.info(
179+
'To perform actions even while a notebook server is running,'
180+
'you may use the flag\n--skip-running-check')
181+
raise
165182

166183

167184
class UninstallContribNbextensionsApp(InstallContribNbextensionsApp):

src/jupyter_contrib_nbextensions/install.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import jupyter_highlight_selected_word
1212
import latex_envs
13-
import psutil
1413
from jupyter_contrib_core.notebook_compat import nbextensions
1514
from jupyter_nbextensions_configurator.application import \
1615
EnableJupyterNbextensionsConfiguratorApp
16+
from notebook.notebookapp import list_running_servers
1717
from traitlets.config import Config
1818
from traitlets.config.manager import BaseJSONConfigManager
1919

@@ -24,53 +24,37 @@ class NotebookRunningError(Exception):
2424
pass
2525

2626

27-
def notebook_is_running():
27+
def notebook_is_running(runtime_dir=None):
2828
"""Return true if a notebook process appears to be running."""
29-
for p in psutil.process_iter():
30-
# p.name() can throw exceptions due to zombie processes on Mac OS X, so
31-
# ignore psutil.ZombieProcess
32-
# (See https://code.google.com/p/psutil/issues/detail?id=428)
33-
34-
# It isn't enough to search just the process name, we have to
35-
# search the process command to see if jupyter-notebook is running.
36-
37-
# Checking the process command can cause an AccessDenied exception to
38-
# be thrown for system owned processes, ignore those as well
39-
try:
40-
# use lower, since python may be Python, e.g. on OSX
41-
if ('python' or 'jupyter') in p.name().lower():
42-
for arg in p.cmdline():
43-
# the missing k is deliberate!
44-
# The usual string 'jupyter-notebook' can get truncated.
45-
if 'jupyter-noteboo' in arg:
46-
return True
47-
except (psutil.ZombieProcess, psutil.AccessDenied):
48-
pass
29+
try:
30+
return bool(next(list_running_servers(runtime_dir=runtime_dir)))
31+
except StopIteration:
4932
return False
5033

5134

5235
def toggle_install(install, user=False, sys_prefix=False, overwrite=False,
5336
symlink=False, prefix=None, nbextensions_dir=None,
54-
logger=None):
37+
logger=None, skip_running_check=False):
5538
"""Install or remove all jupyter_contrib_nbextensions files & config."""
56-
if notebook_is_running():
39+
if not skip_running_check and notebook_is_running():
5740
raise NotebookRunningError(
5841
'Cannot configure while the Jupyter notebook server is running')
5942
_check_conflicting_kwargs(user=user, sys_prefix=sys_prefix, prefix=prefix,
6043
nbextensions_dir=nbextensions_dir)
6144
toggle_install_files(
6245
install, user=user, sys_prefix=sys_prefix, overwrite=overwrite,
6346
symlink=symlink, prefix=prefix, nbextensions_dir=nbextensions_dir,
64-
logger=logger)
47+
logger=logger, skip_running_check=skip_running_check)
6548
toggle_install_config(
66-
install, user=user, sys_prefix=sys_prefix, logger=logger)
49+
install, user=user, sys_prefix=sys_prefix, logger=logger,
50+
skip_running_check=skip_running_check)
6751

6852

6953
def toggle_install_files(install, user=False, sys_prefix=False, logger=None,
7054
overwrite=False, symlink=False, prefix=None,
71-
nbextensions_dir=None):
55+
nbextensions_dir=None, skip_running_check=False):
7256
"""Install/remove jupyter_contrib_nbextensions files."""
73-
if notebook_is_running():
57+
if not skip_running_check and notebook_is_running():
7458
raise NotebookRunningError(
7559
'Cannot configure while the Jupyter notebook server is running')
7660
kwargs = dict(user=user, sys_prefix=sys_prefix, prefix=prefix,
@@ -96,9 +80,10 @@ def toggle_install_files(install, user=False, sys_prefix=False, logger=None,
9680
nbextensions.uninstall_nbextension_python(mod.__name__, **kwargs)
9781

9882

99-
def toggle_install_config(install, user=False, sys_prefix=False, logger=None):
83+
def toggle_install_config(install, user=False, sys_prefix=False,
84+
skip_running_check=False, logger=None):
10085
"""Install/remove contrib nbextensions to/from jupyter_nbconvert_config."""
101-
if notebook_is_running():
86+
if not skip_running_check and notebook_is_running():
10287
raise NotebookRunningError(
10388
'Cannot configure while the Jupyter notebook server is running')
10489
_check_conflicting_kwargs(user=user, sys_prefix=sys_prefix)
@@ -163,20 +148,23 @@ def toggle_install_config(install, user=False, sys_prefix=False, logger=None):
163148

164149

165150
def install(user=False, sys_prefix=False, prefix=None, nbextensions_dir=None,
166-
logger=None, overwrite=False, symlink=False):
151+
logger=None, overwrite=False, symlink=False,
152+
skip_running_check=False):
167153
"""Install all jupyter_contrib_nbextensions files & config."""
168154
return toggle_install(
169155
True, user=user, sys_prefix=sys_prefix, prefix=prefix,
170156
nbextensions_dir=nbextensions_dir, logger=logger,
171-
overwrite=overwrite, symlink=symlink)
157+
overwrite=overwrite, symlink=symlink,
158+
skip_running_check=skip_running_check)
172159

173160

174161
def uninstall(user=False, sys_prefix=False, prefix=None, nbextensions_dir=None,
175-
logger=None):
162+
logger=None, skip_running_check=False):
176163
"""Uninstall all jupyter_contrib_nbextensions files & config."""
177164
return toggle_install(
178165
False, user=user, sys_prefix=sys_prefix, prefix=prefix,
179-
nbextensions_dir=nbextensions_dir, logger=logger)
166+
nbextensions_dir=nbextensions_dir, logger=logger,
167+
skip_running_check=skip_running_check)
180168

181169
# -----------------------------------------------------------------------------
182170
# Private API

0 commit comments

Comments
 (0)