Skip to content

Commit c644219

Browse files
smartass101Ondrej Groverpre-commit-ci[bot]blink1073
authored
Better handling of config migration (#356)
Co-authored-by: Ondrej Grover <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <[email protected]>
1 parent 0ae6f90 commit c644219

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

jupyter_core/application.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,17 @@ def ask():
168168

169169
def migrate_config(self):
170170
"""Migrate config/data from IPython 3"""
171-
if os.path.exists(os.path.join(self.config_dir, "migrated")):
172-
# already migrated
173-
return
171+
try: # let's see if we can open the marker file
172+
# for reading and updating (writing)
173+
f_marker = open(os.path.join(self.config_dir, "migrated"), 'r+') # noqa
174+
except PermissionError: # not readable and/or writable
175+
return # so let's give up migration in such an environment
176+
except FileNotFoundError: # cannot find the marker file
177+
pass # that means we have not migrated yet, so continue
178+
else: # if we got here without raising anything,
179+
# that means the file exists
180+
f_marker.close()
181+
return # so we must have already migrated -> bail out
174182

175183
from .migrate import get_ipython_dir, migrate
176184

jupyter_core/tests/test_migrate.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
from jupyter_core import migrate as migrate_mod
14+
from jupyter_core.application import JupyterApp
1415
from jupyter_core.migrate import (
1516
migrate,
1617
migrate_config,
@@ -53,7 +54,7 @@ def env(request):
5354
def fin():
5455
"""Cleanup test env"""
5556
env_patch.stop()
56-
shutil.rmtree(td)
57+
shutil.rmtree(td, ignore_errors=os.name == 'nt')
5758

5859
request.addfinalizer(fin)
5960

@@ -216,3 +217,31 @@ def test_migrate(env):
216217
migrate()
217218
assert os.path.exists(env["JUPYTER_CONFIG_DIR"])
218219
assert os.path.exists(env["JUPYTER_DATA_DIR"])
220+
221+
222+
def test_app_migrate(env):
223+
shutil.copytree(dotipython, env["IPYTHONDIR"])
224+
app = JupyterApp()
225+
app.initialize([])
226+
assert os.path.exists(env["JUPYTER_CONFIG_DIR"])
227+
assert os.path.exists(env["JUPYTER_DATA_DIR"])
228+
229+
230+
def test_app_migrate_skip_if_marker(env):
231+
shutil.copytree(dotipython, env["IPYTHONDIR"])
232+
touch(pjoin(env["JUPYTER_CONFIG_DIR"], "migrated"), "done")
233+
app = JupyterApp()
234+
app.initialize([])
235+
assert os.listdir(env["JUPYTER_CONFIG_DIR"]) == ["migrated"]
236+
assert not os.path.exists(env["JUPYTER_DATA_DIR"])
237+
238+
239+
def test_app_migrate_skip_unwritable_marker(env):
240+
shutil.copytree(dotipython, env["IPYTHONDIR"])
241+
migrated_marker = pjoin(env["JUPYTER_CONFIG_DIR"], "migrated")
242+
touch(migrated_marker, "done")
243+
os.chmod(migrated_marker, 0) # make it unworkable
244+
app = JupyterApp()
245+
app.initialize([])
246+
assert os.listdir(env["JUPYTER_CONFIG_DIR"]) == ["migrated"]
247+
assert not os.path.exists(env["JUPYTER_DATA_DIR"])

0 commit comments

Comments
 (0)