standarddir: Remove data moving support
Some of it might need to be reintroduced for e.g. #5179, but let's start with a clean state when that happens.
This commit is contained in:
parent
5390f8e65f
commit
f11b339c4a
|
|
@ -48,6 +48,8 @@ Removed
|
|||
- The `--enable-webengine-inspector` flag (which was only needed for Qt 5.10 and
|
||||
below) is now dropped. With Qt 5.11 and newer, the inspector/devtools are
|
||||
enabled unconditionally.
|
||||
- Support for moving qutebrowser data from versions before v1.0.0 has been
|
||||
removed.
|
||||
|
||||
Changed
|
||||
~~~~~~~
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@
|
|||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import shutil
|
||||
import contextlib
|
||||
import enum
|
||||
import argparse
|
||||
|
|
@ -31,7 +30,7 @@ from typing import Iterator, Optional
|
|||
from PyQt5.QtCore import QStandardPaths
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
||||
from qutebrowser.utils import log, debug, message, utils
|
||||
from qutebrowser.utils import log, debug, utils
|
||||
|
||||
# The cached locations
|
||||
_locations = {}
|
||||
|
|
@ -340,44 +339,6 @@ def init(args: Optional[argparse.Namespace]) -> None:
|
|||
|
||||
_init_dirs(args)
|
||||
_init_cachedir_tag()
|
||||
if args is not None and getattr(args, 'basedir', None) is None:
|
||||
if utils.is_mac: # pragma: no cover
|
||||
_move_macos()
|
||||
elif utils.is_windows: # pragma: no cover
|
||||
_move_windows()
|
||||
|
||||
|
||||
def _move_macos() -> None:
|
||||
"""Move most config files to new location on macOS."""
|
||||
old_config = config(auto=True) # ~/Library/Preferences/qutebrowser
|
||||
new_config = config() # ~/.qutebrowser
|
||||
for f in os.listdir(old_config):
|
||||
if f not in ['qsettings', 'autoconfig.yml']:
|
||||
_move_data(os.path.join(old_config, f),
|
||||
os.path.join(new_config, f))
|
||||
|
||||
|
||||
def _move_windows() -> None:
|
||||
"""Move the whole qutebrowser directory from Local to Roaming AppData."""
|
||||
# %APPDATA%\Local\qutebrowser
|
||||
old_appdata_dir = _writable_location(QStandardPaths.AppLocalDataLocation)
|
||||
# %APPDATA%\Roaming\qutebrowser
|
||||
new_appdata_dir = _writable_location(QStandardPaths.AppDataLocation)
|
||||
|
||||
# data subfolder
|
||||
old_data = os.path.join(old_appdata_dir, 'data')
|
||||
new_data = os.path.join(new_appdata_dir, 'data')
|
||||
ok = _move_data(old_data, new_data)
|
||||
if not ok: # pragma: no cover
|
||||
return
|
||||
|
||||
# config files
|
||||
new_config_dir = os.path.join(new_appdata_dir, 'config')
|
||||
_create(new_config_dir)
|
||||
for f in os.listdir(old_appdata_dir):
|
||||
if f != 'cache':
|
||||
_move_data(os.path.join(old_appdata_dir, f),
|
||||
os.path.join(new_config_dir, f))
|
||||
|
||||
|
||||
def _init_cachedir_tag() -> None:
|
||||
|
|
@ -397,33 +358,3 @@ def _init_cachedir_tag() -> None:
|
|||
"cachedir/\n")
|
||||
except OSError:
|
||||
log.init.exception("Failed to create CACHEDIR.TAG")
|
||||
|
||||
|
||||
def _move_data(old: str, new: str) -> bool:
|
||||
"""Migrate data from an old to a new directory.
|
||||
|
||||
If the old directory does not exist, the migration is skipped.
|
||||
If the new directory already exists, an error is shown.
|
||||
|
||||
Return: True if moving succeeded, False otherwise.
|
||||
"""
|
||||
if not os.path.exists(old):
|
||||
return False
|
||||
|
||||
log.init.debug("Migrating data from {} to {}".format(old, new))
|
||||
|
||||
if os.path.exists(new):
|
||||
if not os.path.isdir(new) or os.listdir(new):
|
||||
message.error("Failed to move data from {} as {} is non-empty!"
|
||||
.format(old, new))
|
||||
return False
|
||||
os.rmdir(new)
|
||||
|
||||
try:
|
||||
shutil.move(old, new)
|
||||
except OSError as e:
|
||||
message.error("Failed to move data from {} to {}: {}".format(
|
||||
old, new, e))
|
||||
return False
|
||||
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ import textwrap
|
|||
import logging
|
||||
import subprocess
|
||||
|
||||
import attr
|
||||
from PyQt5.QtCore import QStandardPaths
|
||||
import pytest
|
||||
|
||||
|
|
@ -397,128 +396,8 @@ class TestSystemData:
|
|||
assert standarddir.data(system=True) == standarddir.data()
|
||||
|
||||
|
||||
class TestMoveWindowsAndMacOS:
|
||||
|
||||
"""Test other invocations of _move_data."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def patch_standardpaths(self, files, monkeypatch):
|
||||
locations = {
|
||||
QStandardPaths.AppLocalDataLocation: str(files.local_data_dir),
|
||||
QStandardPaths.AppDataLocation: str(files.roaming_data_dir),
|
||||
}
|
||||
monkeypatch.setattr(standarddir.QStandardPaths, 'writableLocation',
|
||||
locations.get)
|
||||
monkeypatch.setattr(
|
||||
standarddir, 'config', lambda auto=False:
|
||||
str(files.auto_config_dir if auto else files.config_dir))
|
||||
|
||||
@pytest.fixture
|
||||
def files(self, tmpdir):
|
||||
|
||||
@attr.s
|
||||
class Files:
|
||||
|
||||
auto_config_dir = attr.ib()
|
||||
config_dir = attr.ib()
|
||||
local_data_dir = attr.ib()
|
||||
roaming_data_dir = attr.ib()
|
||||
|
||||
return Files(
|
||||
auto_config_dir=tmpdir / 'auto_config' / APPNAME,
|
||||
config_dir=tmpdir / 'config' / APPNAME,
|
||||
local_data_dir=tmpdir / 'data' / APPNAME,
|
||||
roaming_data_dir=tmpdir / 'roaming-data' / APPNAME,
|
||||
)
|
||||
|
||||
def test_move_macos(self, files):
|
||||
"""Test moving configs on macOS."""
|
||||
(files.auto_config_dir / 'autoconfig.yml').ensure()
|
||||
(files.auto_config_dir / 'quickmarks').ensure()
|
||||
files.config_dir.ensure(dir=True)
|
||||
|
||||
standarddir._move_macos()
|
||||
|
||||
assert (files.auto_config_dir / 'autoconfig.yml').exists()
|
||||
assert not (files.config_dir / 'autoconfig.yml').exists()
|
||||
assert not (files.auto_config_dir / 'quickmarks').exists()
|
||||
assert (files.config_dir / 'quickmarks').exists()
|
||||
|
||||
def test_move_windows(self, files):
|
||||
"""Test moving configs on Windows."""
|
||||
(files.local_data_dir / 'data' / 'blocked-hosts').ensure()
|
||||
(files.local_data_dir / 'qutebrowser.conf').ensure()
|
||||
(files.local_data_dir / 'cache' / 'cachefile').ensure()
|
||||
|
||||
standarddir._move_windows()
|
||||
|
||||
assert (files.roaming_data_dir / 'data' / 'blocked-hosts').exists()
|
||||
assert (files.roaming_data_dir / 'config' /
|
||||
'qutebrowser.conf').exists()
|
||||
assert not (files.roaming_data_dir / 'cache').exists()
|
||||
assert (files.local_data_dir / 'cache' / 'cachefile').exists()
|
||||
|
||||
|
||||
class TestMove:
|
||||
|
||||
@pytest.fixture
|
||||
def dirs(self, tmpdir):
|
||||
@attr.s
|
||||
class Dirs:
|
||||
|
||||
old = attr.ib()
|
||||
new = attr.ib()
|
||||
old_file = attr.ib()
|
||||
new_file = attr.ib()
|
||||
|
||||
old_dir = tmpdir / 'old'
|
||||
new_dir = tmpdir / 'new'
|
||||
return Dirs(old=old_dir, new=new_dir,
|
||||
old_file=old_dir / 'file', new_file=new_dir / 'file')
|
||||
|
||||
def test_no_old_dir(self, dirs, caplog):
|
||||
"""Nothing should happen without any old directory."""
|
||||
standarddir._move_data(str(dirs.old), str(dirs.new))
|
||||
assert not any(message.startswith('Migrating data from')
|
||||
for message in caplog.messages)
|
||||
|
||||
@pytest.mark.parametrize('empty_dest', [True, False])
|
||||
def test_moving_data(self, dirs, empty_dest):
|
||||
dirs.old_file.ensure()
|
||||
if empty_dest:
|
||||
dirs.new.ensure(dir=True)
|
||||
|
||||
standarddir._move_data(str(dirs.old), str(dirs.new))
|
||||
assert not dirs.old_file.exists()
|
||||
assert dirs.new_file.exists()
|
||||
|
||||
def test_already_existing(self, dirs, caplog):
|
||||
dirs.old_file.ensure()
|
||||
dirs.new_file.ensure()
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
standarddir._move_data(str(dirs.old), str(dirs.new))
|
||||
|
||||
expected = "Failed to move data from {} as {} is non-empty!".format(
|
||||
dirs.old, dirs.new)
|
||||
assert caplog.messages[-1] == expected
|
||||
|
||||
def test_deleting_error(self, dirs, monkeypatch, mocker, caplog):
|
||||
"""When there was an error it should be logged."""
|
||||
mock = mocker.Mock(side_effect=OSError('error'))
|
||||
monkeypatch.setattr(standarddir.shutil, 'move', mock)
|
||||
dirs.old_file.ensure()
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
standarddir._move_data(str(dirs.old), str(dirs.new))
|
||||
|
||||
expected = "Failed to move data from {} to {}: error".format(
|
||||
dirs.old, dirs.new)
|
||||
assert caplog.messages[-1] == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('args_kind', ['basedir', 'normal', 'none'])
|
||||
def test_init(mocker, tmpdir, monkeypatch, args_kind):
|
||||
def test_init(tmpdir, monkeypatch, args_kind):
|
||||
"""Do some sanity checks for standarddir.init().
|
||||
|
||||
Things like _init_cachedir_tag() are tested in more detail in other tests.
|
||||
|
|
@ -527,8 +406,6 @@ def test_init(mocker, tmpdir, monkeypatch, args_kind):
|
|||
|
||||
monkeypatch.setenv('HOME', str(tmpdir))
|
||||
|
||||
m_windows = mocker.patch('qutebrowser.utils.standarddir._move_windows')
|
||||
m_mac = mocker.patch('qutebrowser.utils.standarddir._move_macos')
|
||||
if args_kind == 'normal':
|
||||
args = types.SimpleNamespace(basedir=None)
|
||||
elif args_kind == 'basedir':
|
||||
|
|
@ -540,19 +417,6 @@ def test_init(mocker, tmpdir, monkeypatch, args_kind):
|
|||
standarddir.init(args)
|
||||
|
||||
assert standarddir._locations != {}
|
||||
if args_kind == 'normal':
|
||||
if utils.is_mac:
|
||||
m_windows.assert_not_called()
|
||||
assert m_mac.called
|
||||
elif utils.is_windows:
|
||||
assert m_windows.called
|
||||
m_mac.assert_not_called()
|
||||
else:
|
||||
m_windows.assert_not_called()
|
||||
m_mac.assert_not_called()
|
||||
else:
|
||||
m_windows.assert_not_called()
|
||||
m_mac.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.linux
|
||||
|
|
|
|||
Loading…
Reference in New Issue