Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of config migration #356

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions jupyter_core/tests/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])