Skip to content

Commit 719a14b

Browse files
cli/discover: remove local collections if the remote collection is deleted
This works when the destination backend is 'filesystem' and the source is CalDAV-calendar-home-set. pimutils#868
1 parent d5d45f2 commit 719a14b

File tree

9 files changed

+105
-6
lines changed

9 files changed

+105
-6
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Version 0.19.0
4646
==============
4747

4848
- Add "shell" password fetch strategy to pass command string to a shell.
49+
- Add ``implicit`` option to storage section. It creates/deletes implicitly
50+
collections in the destinations, when new collections are created/deleted
51+
in the source. The deletion is implemented only for the "filesystem" storage.
52+
See :ref:`storage_config`.
4953
- Add "description" and "order" as metadata. These fetch the CalDAV:
5054
calendar-description, ``CardDAV:addressbook-description`` and
5155
``apple-ns:calendar-order`` properties respectively.

docs/config.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ Local
381381
#post_hook = null
382382
#pre_deletion_hook = null
383383
#fileignoreext = ".tmp"
384+
#implicit = "create"
385+
#implicit = ["create", "delete"]
384386

385387
Can be used with `khal <http://lostpackets.de/khal/>`_. See :doc:`vdir` for
386388
a more formal description of the format.
@@ -405,6 +407,12 @@ Local
405407
The command will be called with the path of the deleted file.
406408
:param fileeignoreext: The file extention to ignore. It is only useful
407409
if fileext is set to the empty string. The default is ``.tmp``.
410+
:param implicit: When a new collection is created on the source,
411+
create it in the destination without asking questions, when
412+
the value is "create". When the value is "delete" and a collection
413+
is removed on the source, remove it in the destination. The value
414+
can be a string or an array of strings. The deletion is implemented
415+
only for the "filesystem" storage.
408416

409417
.. storage:: singlefile
410418

tests/system/cli/test_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def test_read_config(read_config):
6262
"yesno": False,
6363
"number": 42,
6464
"instance_name": "bob_a",
65+
"implicit": [],
6566
},
66-
"bob_b": {"type": "carddav", "instance_name": "bob_b"},
67+
"bob_b": {'type': "carddav", "instance_name": "bob_b", "implicit": []},
6768
}
6869

6970

tests/system/utils/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_get_storage_init_args():
2222
from vdirsyncer.storage.memory import MemoryStorage
2323

2424
all, required = utils.get_storage_init_args(MemoryStorage)
25-
assert all == {"fileext", "collection", "read_only", "instance_name", "no_delete"}
25+
assert all == {"fileext", "collection", "read_only", "instance_name", "no_delete", "implicit"}
2626
assert not required
2727

2828

vdirsyncer/cli/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ def _parse_section(
119119
raise ValueError("More than one general section.")
120120
self._general = options
121121
elif section_type == "storage":
122+
if "implicit" not in options:
123+
options["implicit"] = []
124+
elif isinstance(options["implicit"], str):
125+
options["implicit"] = [options['implicit']]
126+
elif not isinstance(options["implicit"], list):
127+
raise ValueError(
128+
"`implicit` parameter must be a list, string or absent.")
122129
self._storages[name] = options
123130
elif section_type == "pair":
124131
self._pairs[name] = options

vdirsyncer/cli/discover.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import aiohttp
1010
import aiostream
1111

12+
from . import cli_logger
1213
from .. import exceptions
1314
from .utils import handle_collection_not_found
15+
from .utils import handle_collection_was_removed
1416
from .utils import handle_storage_init_error
1517
from .utils import load_status
1618
from .utils import save_status
@@ -106,6 +108,29 @@ async def collections_for_pair(
106108
_handle_collection_not_found=handle_collection_not_found,
107109
)
108110
)
111+
if "from b" in (pair.collections or []):
112+
only_in_a = set((await a_discovered.get_self()).keys()) - set(
113+
(await b_discovered.get_self()).keys())
114+
if only_in_a and "delete" in pair.config_a["implicit"]:
115+
for a in only_in_a:
116+
try:
117+
handle_collection_was_removed(pair.config_a, a)
118+
save_status(status_path, pair.name, a, data_type="metadata")
119+
save_status(status_path, pair.name, a, data_type="items")
120+
except NotImplementedError as e:
121+
cli_logger.error(e)
122+
123+
if "from a" in (pair.collections or []):
124+
only_in_b = set((await b_discovered.get_self()).keys()) - set(
125+
(await a_discovered.get_self()).keys())
126+
if only_in_b and "delete" in pair.config_b["implicit"]:
127+
for b in only_in_b:
128+
try:
129+
handle_collection_was_removed(pair.config_b, b)
130+
save_status(status_path, pair.name, b, data_type="metadata")
131+
save_status(status_path, pair.name, b, data_type="items")
132+
except NotImplementedError as e:
133+
cli_logger.error(e)
109134

110135
await _sanity_check_collections(rv, connector=connector)
111136

vdirsyncer/cli/utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ def save_status(
245245
status_name = get_status_name(pair, collection)
246246
path = expand_path(os.path.join(base_path, status_name)) + "." + data_type
247247
prepare_status_path(path)
248+
if data is None:
249+
try:
250+
os.remove(path)
251+
except OSError: # the file has not existed
252+
pass
253+
return
248254

249255
with atomic_write(path, mode="w", overwrite=True) as f:
250256
json.dump(data, f)
@@ -342,6 +348,19 @@ def assert_permissions(path: str, wanted: int) -> None:
342348
os.chmod(path, wanted)
343349

344350

351+
def handle_collection_was_removed(config, collection):
352+
if "delete" in config["implicit"]:
353+
storage_type = config["type"]
354+
cls, config = storage_class_from_config(config)
355+
config["collection"] = collection
356+
try:
357+
args = cls.delete_collection(**config)
358+
args["type"] = storage_type
359+
return args
360+
except NotImplementedError as e:
361+
cli_logger.error(e)
362+
363+
345364
async def handle_collection_not_found(config, collection, e=None):
346365
storage_name = config.get("instance_name", None)
347366

@@ -351,7 +370,8 @@ async def handle_collection_not_found(config, collection, e=None):
351370
)
352371
)
353372

354-
if click.confirm("Should vdirsyncer attempt to create it?"):
373+
if "create" in config["implicit"] or click.confirm(
374+
"Should vdirsyncer attempt to create it?"):
355375
storage_type = config["type"]
356376
cls, config = storage_class_from_config(config)
357377
config["collection"] = collection

vdirsyncer/storage/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Storage(metaclass=StorageMeta):
5151
5252
:param read_only: Whether the synchronization algorithm should avoid writes
5353
to this storage. Some storages accept no value other than ``True``.
54+
:param implicit: Whether the synchronization shall create/delete collections
55+
in the destination, when these were created/removed from the source. Must
56+
be a possibly empty list of strings.
5457
"""
5558

5659
fileext = ".txt"
@@ -84,9 +87,16 @@ def __init__(
8487
read_only=None,
8588
no_delete=None,
8689
collection=None,
90+
implicit=None
8791
):
8892
if read_only is None:
8993
read_only = self.read_only
94+
if implicit is None:
95+
self.implicit = []
96+
elif isinstance(implicit, str):
97+
self.implicit = [implicit]
98+
else:
99+
self.implicit = implicit
90100
if self.read_only and not read_only:
91101
raise exceptions.UserError("This storage can only be read-only.")
92102
self.read_only = bool(read_only)
@@ -134,6 +144,18 @@ async def create_collection(cls, collection, **kwargs):
134144
"""
135145
raise NotImplementedError
136146

147+
@classmethod
148+
def delete_collection(cls, collection, **kwargs):
149+
'''
150+
Delete the specified collection and return the new arguments.
151+
152+
``collection=None`` means the arguments are already pointing to a
153+
possible collection location.
154+
155+
The returned args should contain the collection name, for UI purposes.
156+
'''
157+
raise NotImplementedError()
158+
137159
def __repr__(self):
138160
try:
139161
if self.instance_name:

vdirsyncer/storage/filesystem.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import errno
44
import logging
55
import os
6+
import shutil
67
import subprocess
78

89
from atomicwrites import atomic_write
@@ -65,9 +66,7 @@ async def discover(cls, path, **kwargs):
6566
def _validate_collection(cls, path):
6667
if not os.path.isdir(path) or os.path.islink(path):
6768
return False
68-
if os.path.basename(path).startswith("."):
69-
return False
70-
return True
69+
return not os.path.basename(path).startswith(".")
7170

7271
@classmethod
7372
async def create_collection(cls, collection, **kwargs):
@@ -83,6 +82,19 @@ async def create_collection(cls, collection, **kwargs):
8382
kwargs["collection"] = collection
8483
return kwargs
8584

85+
@classmethod
86+
def delete_collection(cls, collection, **kwargs):
87+
kwargs = dict(kwargs)
88+
path = kwargs['path']
89+
90+
if collection is not None:
91+
path = os.path.join(path, collection)
92+
shutil.rmtree(path, ignore_errors=True)
93+
94+
kwargs["path"] = path
95+
kwargs["collection"] = collection
96+
return kwargs
97+
8698
def _get_filepath(self, href):
8799
return os.path.join(self.path, href)
88100

0 commit comments

Comments
 (0)