forked from picklepete/pyicloud
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathexample_reminders_delta.py
More file actions
412 lines (344 loc) · 13.3 KB
/
example_reminders_delta.py
File metadata and controls
412 lines (344 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""Dedicated integration validator for Reminders delta-sync APIs.
This script validates the currently implemented delta APIs in
`pyicloud.services.reminders.service.RemindersService`:
- sync_cursor()
- iter_changes(since=...)
The validation intentionally checks update and delete in separate cursor windows,
because CloudKit zone changes are a delta-state feed rather than an append-only
event log for individual records.
"""
from __future__ import annotations
import argparse
import os
import sys
import traceback
from dataclasses import dataclass, field
from datetime import datetime, timezone
from getpass import getpass
from time import monotonic, sleep
from typing import Any, Iterable, Optional, Sequence
from pyicloud import PyiCloudService
from pyicloud.services.reminders.models.domain import Reminder, RemindersList
@dataclass
class ValidationTracker:
checks: int = 0
failures: list[str] = field(default_factory=list)
def expect(self, condition: bool, label: str, detail: str = "") -> None:
self.checks += 1
if condition:
print(f" [PASS] {label}")
return
message = label if not detail else f"{label}: {detail}"
self.failures.append(message)
print(f" [FAIL] {message}")
@dataclass
class RunState:
created: Optional[Reminder] = None
deleted: bool = False
def banner(title: str) -> None:
print(f"\n{'=' * 78}")
print(title)
print(f"{'=' * 78}")
def parse_args() -> argparse.Namespace:
now = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--username",
default=os.getenv("PYICLOUD_USERNAME"),
help="Apple ID email. Defaults to interactive prompt.",
)
parser.add_argument(
"--password",
default=os.getenv("PYICLOUD_PASSWORD"),
help="Apple ID password. Defaults to keyring or interactive prompt.",
)
parser.add_argument(
"--list-name",
default="pyicloud testing",
help="Existing reminders list title to use.",
)
parser.add_argument(
"--prefix",
default=f"pyicloud-reminders-delta-{now}",
help="Prefix added to the dedicated delta reminder title.",
)
parser.add_argument(
"--consistency-timeout",
type=float,
default=20.0,
help="Seconds to wait for eventual consistency checks.",
)
parser.add_argument(
"--poll-interval",
type=float,
default=1.0,
help="Polling interval in seconds for eventual consistency checks.",
)
parser.add_argument(
"--cleanup",
action="store_true",
help="Delete the generated reminder if the script fails before the delete phase.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Print traceback on unexpected errors.",
)
return parser.parse_args()
def resolve_credentials(args: argparse.Namespace) -> tuple[str, Optional[str]]:
username = args.username or input("Apple ID: ").strip()
if not username:
raise ValueError("Apple ID username is required.")
password = args.password
if password == "":
password = None
if password is None and sys.stdin.isatty():
answer = input("Password not provided. Prompt now? [y/N]: ").strip().lower()
if answer in {"y", "yes"}:
password = getpass("Apple ID password: ")
return username, password
def _prompt_selection(
prompt: str, options: Sequence[Any], default_index: int = 0
) -> int:
if not options:
raise ValueError("Cannot select from an empty option list.")
selected_index = default_index
if len(options) > 1:
raw_index = input(f"{prompt} [{default_index}]: ").strip()
if raw_index:
selected_index = int(raw_index)
if selected_index < 0 or selected_index >= len(options):
raise RuntimeError("Invalid selection.")
return selected_index
def authenticate(args: argparse.Namespace) -> PyiCloudService:
username, password = resolve_credentials(args)
print("Authenticating with iCloud...")
api = PyiCloudService(apple_id=username, password=password)
if api.requires_2fa:
fido2_devices = list(api.fido2_devices)
if fido2_devices:
print("Security key verification required.")
for index, _device in enumerate(fido2_devices):
print(f" {index}: Security key {index}")
selected_index = _prompt_selection(
"Select security key",
fido2_devices,
)
selected_device = fido2_devices[selected_index]
print("Touch the selected security key to continue.")
try:
api.confirm_security_key(selected_device)
except Exception as exc: # pragma: no cover - live integration path
raise RuntimeError("Security key verification failed.") from exc
else:
code = input("Enter 2FA code: ").strip()
if not api.validate_2fa_code(code):
raise RuntimeError("Invalid 2FA code.")
if not api.is_trusted_session:
print("Session is not trusted. Requesting trust...")
api.trust_session()
elif api.requires_2sa:
devices = api.trusted_devices
if not devices:
raise RuntimeError("2SA required but no trusted devices were returned.")
print("Trusted devices:")
for index, _device in enumerate(devices):
print(f" {index}: Trusted device")
selected_index = _prompt_selection(
"Select trusted device",
devices,
)
device = devices[selected_index]
if not api.send_verification_code(device):
raise RuntimeError("Failed to send 2SA verification code.")
code = input("Enter 2SA verification code: ").strip()
if not api.validate_verification_code(device, code):
raise RuntimeError("Invalid 2SA verification code.")
return api
def pick_target_list(lists: Iterable[RemindersList], list_name: str) -> RemindersList:
all_lists = list(lists)
if not all_lists:
raise RuntimeError("No reminders lists found in iCloud account.")
print("Available lists:")
for lst in all_lists:
print(f" - {lst.title} ({lst.id})")
for lst in all_lists:
if lst.title == list_name:
print(f"\nUsing list: {lst.title} ({lst.id})")
return lst
raise RuntimeError(
f"List '{list_name}' not found. "
f"Please create it first or pass --list-name with an existing list."
)
def wait_until(
description: str,
predicate,
timeout_seconds: float,
poll_interval: float,
) -> bool:
deadline = monotonic() + timeout_seconds
while monotonic() < deadline:
if predicate():
return True
sleep(poll_interval)
print(f" [WARN] Timed out while waiting for: {description}")
return False
def cleanup_generated(api: PyiCloudService, state: RunState) -> None:
if state.created is None or state.deleted:
return
banner("Cleanup")
try:
fresh = api.reminders.get(state.created.id)
except LookupError:
print(f" [SKIP] Not found ({state.created.id})")
return
try:
api.reminders.delete(fresh)
state.deleted = True
print(f" [OK] Deleted {state.created.id}")
except Exception as exc: # pragma: no cover - live integration path
print(f" [WARN] Cleanup failed for {state.created.id}: {exc}")
def main() -> int:
args = parse_args()
tracker = ValidationTracker()
state = RunState()
api: Optional[PyiCloudService] = None
try:
api = authenticate(args)
reminders_api = api.reminders
banner("1) Discover Lists")
target_list = pick_target_list(reminders_api.lists(), args.list_name)
banner("2) sync_cursor() + create delta")
create_cursor = reminders_api.sync_cursor()
tracker.expect(
isinstance(create_cursor, str) and bool(create_cursor),
"sync_cursor() returns a non-empty cursor before create",
f"cursor={create_cursor!r}",
)
created = reminders_api.create(
list_id=target_list.id,
title=f"{args.prefix} | create",
desc="Dedicated reminder for delta-sync validation.",
)
state.created = created
print(f" [CREATE] delta reminder: {created.id}")
create_events = []
def create_visible() -> bool:
nonlocal create_events
create_events = list(reminders_api.iter_changes(since=create_cursor))
return any(
event.type == "updated"
and event.reminder_id == created.id
and event.reminder is not None
and event.reminder.title == created.title
for event in create_events
)
tracker.expect(
wait_until(
"delta create to appear in iter_changes() output",
create_visible,
timeout_seconds=args.consistency_timeout,
poll_interval=args.poll_interval,
),
"iter_changes(since=...) returns an updated event after create",
f"event_count={len(create_events)}",
)
tracker.expect(
all(
hasattr(event, "type") and hasattr(event, "reminder_id")
for event in create_events
),
"iter_changes() returns structured change events after create",
)
banner("3) sync_cursor() + update delta")
update_cursor = reminders_api.sync_cursor()
tracker.expect(
isinstance(update_cursor, str) and bool(update_cursor),
"sync_cursor() returns a non-empty cursor before update",
f"cursor={update_cursor!r}",
)
updated_title = f"{args.prefix} | updated"
updated_desc = "Updated delta-sync body."
updated = reminders_api.get(created.id)
updated.title = updated_title
updated.desc = updated_desc
reminders_api.update(updated)
update_events = []
def update_visible() -> bool:
nonlocal update_events
update_events = list(reminders_api.iter_changes(since=update_cursor))
return any(
event.type == "updated"
and event.reminder_id == created.id
and event.reminder is not None
and event.reminder.title == updated_title
and event.reminder.desc == updated_desc
for event in update_events
)
tracker.expect(
wait_until(
"delta update to appear in iter_changes() output",
update_visible,
timeout_seconds=args.consistency_timeout,
poll_interval=args.poll_interval,
),
"iter_changes(since=...) returns an updated event after update",
f"event_count={len(update_events)}",
)
banner("4) sync_cursor() + delete delta")
delete_cursor = reminders_api.sync_cursor()
tracker.expect(
isinstance(delete_cursor, str) and bool(delete_cursor),
"sync_cursor() returns a non-empty cursor before delete",
f"cursor={delete_cursor!r}",
)
reminders_api.delete(reminders_api.get(created.id))
state.deleted = True
delete_events = []
def delete_visible() -> bool:
nonlocal delete_events
delete_events = list(reminders_api.iter_changes(since=delete_cursor))
return any(
event.type == "deleted" and event.reminder_id == created.id
for event in delete_events
)
tracker.expect(
wait_until(
"delta delete to appear in iter_changes() output",
delete_visible,
timeout_seconds=args.consistency_timeout,
poll_interval=args.poll_interval,
),
"iter_changes(since=...) returns a deleted event after delete",
f"event_count={len(delete_events)}",
)
banner("Coverage Notes")
print("Validated delta capabilities in current service implementation:")
print(" - sync_cursor()")
print(" - iter_changes(since=...) after create")
print(" - iter_changes(since=...) after update")
print(" - iter_changes(since=...) after delete")
except Exception as exc: # pragma: no cover - live integration path
banner("Fatal Error")
print(str(exc))
if args.debug:
traceback.print_exc()
return 1
finally:
if args.cleanup and api is not None:
try:
cleanup_generated(api, state)
except Exception as cleanup_exc: # pragma: no cover
print(f"[WARN] Cleanup failed: {cleanup_exc}")
banner("Validation Summary")
print(f"Checks executed: {tracker.checks}")
print(f"Failures: {len(tracker.failures)}")
if tracker.failures:
print("\nFailure details:")
for failure in tracker.failures:
print(f" - {failure}")
return 2
print("All validations passed.")
return 0
if __name__ == "__main__":
sys.exit(main())