diff --git a/jupyter_core/application.py b/jupyter_core/application.py index e572888..8b55a8a 100644 --- a/jupyter_core/application.py +++ b/jupyter_core/application.py @@ -168,9 +168,17 @@ def ask(): def migrate_config(self): """Migrate config/data from IPython 3""" - if os.path.exists(os.path.join(self.config_dir, "migrated")): - # already migrated - return + try: # let's see if we can open the marker file + # for reading and updating (writing) + f_marker = open(os.path.join(self.config_dir, "migrated"), 'r+') # noqa + except PermissionError: # not readable and/or writable + return # so let's give up migration in such an environment + except FileNotFoundError: # cannot find the marker file + pass # that means we have not migrated yet, so continue + else: # if we got here without raising anything, + # that means the file exists + f_marker.close() + return # so we must have already migrated -> bail out from .migrate import get_ipython_dir, migrate diff --git a/jupyter_core/tests/test_migrate.py b/jupyter_core/tests/test_migrate.py index 776b3cc..b86887e 100644 --- a/jupyter_core/tests/test_migrate.py +++ b/jupyter_core/tests/test_migrate.py @@ -11,6 +11,7 @@ import pytest from jupyter_core import migrate as migrate_mod +from jupyter_core.application import JupyterApp from jupyter_core.migrate import ( migrate, migrate_config, @@ -53,7 +54,7 @@ def env(request): def fin(): """Cleanup test env""" env_patch.stop() - shutil.rmtree(td) + shutil.rmtree(td, ignore_errors=os.name == 'nt') request.addfinalizer(fin) @@ -216,3 +217,31 @@ def test_migrate(env): migrate() assert os.path.exists(env["JUPYTER_CONFIG_DIR"]) assert os.path.exists(env["JUPYTER_DATA_DIR"]) + + +def test_app_migrate(env): + shutil.copytree(dotipython, env["IPYTHONDIR"]) + app = JupyterApp() + app.initialize([]) + assert os.path.exists(env["JUPYTER_CONFIG_DIR"]) + assert os.path.exists(env["JUPYTER_DATA_DIR"]) + + +def test_app_migrate_skip_if_marker(env): + shutil.copytree(dotipython, env["IPYTHONDIR"]) + touch(pjoin(env["JUPYTER_CONFIG_DIR"], "migrated"), "done") + app = JupyterApp() + app.initialize([]) + assert os.listdir(env["JUPYTER_CONFIG_DIR"]) == ["migrated"] + assert not os.path.exists(env["JUPYTER_DATA_DIR"]) + + +def test_app_migrate_skip_unwritable_marker(env): + shutil.copytree(dotipython, env["IPYTHONDIR"]) + migrated_marker = pjoin(env["JUPYTER_CONFIG_DIR"], "migrated") + touch(migrated_marker, "done") + os.chmod(migrated_marker, 0) # make it unworkable + app = JupyterApp() + app.initialize([]) + assert os.listdir(env["JUPYTER_CONFIG_DIR"]) == ["migrated"] + assert not os.path.exists(env["JUPYTER_DATA_DIR"])