-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynchronizer.py
More file actions
466 lines (394 loc) · 21.2 KB
/
Copy pathsynchronizer.py
File metadata and controls
466 lines (394 loc) · 21.2 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import os
import mimetypes
import subprocess
import sys
import logging
import json
from datetime import datetime, timezone
from typing import Optional, Dict, List
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
try:
from auth import get_drive_service
except ImportError:
from .auth import get_drive_service
class DataSynchronizer:
def __init__(self, headless: bool = False):
self.service = get_drive_service(headless)
self.failures: Dict[str, Dict[str, List[str]]] = {}
self.enable_date_check: bool = True
def get_or_create_folder(self, folder_name: str, parent_id: Optional[str] = None) -> Optional[str]:
"""Finds or creates a folder on Google Drive."""
query = f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
if parent_id:
query += f" and '{parent_id}' in parents"
try:
results = self.service.files().list(
q=query, fields="files(id, name)", supportsAllDrives=True, includeItemsFromAllDrives=True
).execute()
items = results.get('files', [])
if items:
return items[0]['id']
file_metadata = {'name': folder_name, 'mimeType': 'application/vnd.google-apps.folder'}
if parent_id:
file_metadata['parents'] = [parent_id]
folder = self.service.files().create(body=file_metadata, fields='id', supportsAllDrives=True).execute()
logging.info(f"Folder created: {folder_name}")
return folder.get('id')
except HttpError as error:
logging.error(f"Error managing folder '{folder_name}': {error}")
return None
def get_file_id(self, file_name: str, parent_id: str) -> Optional[str]:
"""Finds a file ID on Google Drive by name and parent ID."""
query = f"name = '{file_name}' and '{parent_id}' in parents and trashed = false"
try:
results = self.service.files().list(
q=query, fields="files(id, name)", supportsAllDrives=True, includeItemsFromAllDrives=True
).execute()
items = results.get('files', [])
return items[0]['id'] if items else None
except HttpError as error:
logging.error(f"Error checking file existence for '{file_name}': {error}")
return None
def create_file(self, local_path: str, parent_id: str, media: MediaFileUpload) -> Optional[str]:
"""Creates a new file on Google Drive."""
file_name = os.path.basename(local_path)
file_metadata = {'name': file_name, 'parents': [parent_id]}
try:
file = self.service.files().create(
body=file_metadata, media_body=media, fields='id', supportsAllDrives=True
).execute()
logging.info(f"File imported: {file_name}")
return file.get('id')
except HttpError as error:
self._handle_upload_error(file_name, error)
return None
def update_file(self, local_path: str, file_id: str, media: MediaFileUpload):
"""Updates an existing file on Google Drive."""
file_name = os.path.basename(local_path)
try:
self.service.files().update(
fileId=file_id, media_body=media, supportsAllDrives=True
).execute()
logging.info(f"File updated: {file_name}")
except HttpError as error:
self._handle_upload_error(file_name, error)
def _handle_upload_error(self, file_name: str, error: HttpError):
"""Helper to handle HTTP errors during upload/update operations."""
if error.resp.status == 403 and "storageQuotaExceeded" in str(error):
logging.error("Storage quota exceeded. Consider switching to OAuth2.")
else:
logging.error(f"Error importing/updating '{file_name}': {error}")
def _get_remote_modified_time(self, file_id: str) -> Optional[datetime]:
"""Returns the last modified time of a Google Drive file as a UTC datetime."""
try:
result = self.service.files().get(
fileId=file_id,
fields='modifiedTime',
supportsAllDrives=True
).execute()
remote_modified_time = result.get('modifiedTime')
if not remote_modified_time:
return None
if remote_modified_time.endswith('Z'):
remote_modified_time = remote_modified_time[:-1] + '+00:00'
return datetime.fromisoformat(remote_modified_time).astimezone(timezone.utc)
except Exception as error:
logging.warning(f"Unable to read modified time for file '{file_id}': {error}")
return None
def _should_skip_upload(self, local_path: str, file_id: str) -> bool:
"""Returns True when the remote file is already up to date or newer."""
if not file_id:
return False
local_modified_time = datetime.fromtimestamp(os.path.getmtime(local_path), tz=timezone.utc)
remote_modified_time = self._get_remote_modified_time(file_id)
if remote_modified_time is None:
return False
return remote_modified_time >= local_modified_time
def upload_file(self, local_path: str, parent_id: str):
"""Uploads or updates a file on Google Drive."""
file_name = os.path.basename(local_path)
file_id = self.get_file_id(file_name, parent_id)
if file_id and self.enable_date_check and self._should_skip_upload(local_path, file_id):
logging.info(f"Skipping upload for '{file_name}' because the Drive copy is up to date.")
return
try:
mime_type, _ = mimetypes.guess_type(local_path)
media = MediaFileUpload(local_path, mimetype=mime_type or 'application/octet-stream')
if file_id:
self.update_file(local_path, file_id, media)
else:
self.create_file(local_path, parent_id, media)
except Exception as e:
logging.error(f"Failed to prepare media upload for '{file_name}': {e}")
def run_verification(self, data_path: str, book: str = None, chapter: str = None, verse: str = None, preprocessor: str = None, books: List[str] = None, chapters: List[str] = None) -> bool:
"""Runs the external verification script and captures failures."""
logging.info("="*50)
logging.info("STARTING DATA VERIFICATION")
logging.info("="*50)
script_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'data-verification', 'verify_data.py'))
cmd = [sys.executable, script_path, '--data_folder', data_path, '--json']
if preprocessor: cmd.extend(['--preprocessor', preprocessor])
elif books: cmd.extend(['--books'] + books)
elif verse: cmd.extend(['--book', book, '--chapter', chapter, '--verse', verse])
elif chapters: cmd.extend(['--book', book, '--chapters'] + chapters)
elif chapter: cmd.extend(['--book', book, '--chapter', chapter])
elif book: cmd.extend(['--book', book])
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
output = result.stdout
# Extract JSON from output
if "---JSON_START---" in output:
try:
json_str = output.split("---JSON_START---")[1].split("---JSON_END---")[0].strip()
report = json.loads(json_str)
self.failures = report.get("failures", {})
if report.get("failed"):
logging.warning("Verification found issues. Some items will be skipped during synchronization.")
else:
logging.info("Data verification successful.\n")
return True # Continue with partial sync
except (IndexError, json.JSONDecodeError) as e:
logging.error(f"Failed to parse verification report: {e}")
return False
else:
if result.returncode != 0:
logging.error(f"Verification failed without JSON output. Stderr: {result.stderr}")
return False
logging.info("Data verification successful (no failures reported).\n")
return True
except Exception as e:
logging.error(f"Verification execution error: {e}")
return False
def is_failed(self, book: str, chapter: str = None, verse: str = None) -> bool:
"""Checks if a specific item failed verification."""
if book not in self.failures:
return False
chapter_failures = self.failures[book]
if chapter is None:
return False
if chapter not in chapter_failures:
return False
verse_failures = chapter_failures[chapter]
if verse is None:
# If the chapter text itself is missing or failed (usually represented as an empty list or specific error)
return False
return verse in verse_failures
def sync_preprocessor(self, data_path: str, preprocessor_name: str, parent_id: str):
"""Synchronizes all tasks assigned to a specific preprocessor."""
assignment_file = os.path.join(data_path, "assignment.json")
if not os.path.exists(assignment_file):
logging.error(f"Assignment file not found: {assignment_file}")
return
with open(assignment_file, 'r', encoding='utf-8') as f:
assignments = json.load(f)
if preprocessor_name not in assignments:
logging.error(f"Preprocessor '{preprocessor_name}' not found in assignment file.")
return
preprocessor_tasks = assignments[preprocessor_name]
logging.info(f"Syncing all tasks for preprocessor: {preprocessor_name}")
for book, chapters in preprocessor_tasks.items():
if book == "total_duration_hours":
continue
if chapters == "all":
self.sync_book(data_path, book, parent_id)
else:
book_id = self.get_or_create_folder(book, parent_id)
if not book_id: continue
for chapter in chapters:
self.sync_chapter(data_path, book, chapter, book_id)
def sync_verse(self, data_path: str, book: str, chapter: str, verse: str, parent_id: str):
"""Synchronizes a single verse."""
if self.is_failed(book, chapter, verse):
logging.warning(f"Skipping verse {verse} ({book}/{chapter}) due to verification failure.")
return
logging.info(f"Syncing verse: {verse} ({book}/{chapter})")
verse_id = self.get_or_create_folder(verse, parent_id)
if not verse_id: return
verse_local_path = os.path.join(data_path, book, chapter, verse)
if not os.path.isdir(verse_local_path):
logging.error(f"Verse folder '{verse_local_path}' not found.")
return
for file in os.listdir(verse_local_path):
file_path = os.path.join(verse_local_path, file)
if os.path.isfile(file_path):
self.upload_file(file_path, verse_id)
def sync_chapter(self, data_path: str, book: str, chapter: str, parent_id: str):
"""Synchronizes a single chapter."""
logging.info(f"Syncing chapter: {chapter} ({book})")
chapter_id = self.get_or_create_folder(chapter, parent_id)
if not chapter_id: return
chapter_local_path = os.path.join(data_path, book, chapter)
if not os.path.isdir(chapter_local_path):
logging.error(f"Chapter folder '{chapter_local_path}' not found.")
return
for item in os.listdir(chapter_local_path):
item_path = os.path.join(chapter_local_path, item)
if os.path.isfile(item_path):
self.upload_file(item_path, chapter_id)
elif os.path.isdir(item_path) and item.startswith('V_'):
self.sync_verse(data_path, book, chapter, item, chapter_id)
def sync_book(self, data_path: str, book: str, parent_id: str):
"""Synchronizes an entire book."""
logging.info(f"Syncing book: {book}")
book_id = self.get_or_create_folder(book, parent_id)
if not book_id: return
book_local_path = os.path.join(data_path, book)
if not os.path.isdir(book_local_path):
logging.error(f"Book folder '{book_local_path}' not found.")
return
for chapter in os.listdir(book_local_path):
chapter_path = os.path.join(book_local_path, chapter)
if os.path.isdir(chapter_path) and chapter.startswith(f"{book}_"):
self.sync_chapter(data_path, book, chapter, book_id)
def sync_books(self, data_path: str, books: List[str], parent_id: str):
"""Synchronizes multiple books."""
logging.info(f"Syncing books: {', '.join(books)}")
for book in books:
self.sync_book(data_path, book, parent_id)
def sync_chapters(self, data_path: str, book: str, chapters: List[str], parent_id: str):
"""Synchronizes multiple chapters."""
logging.info(f"Syncing chapters: {', '.join(chapters)} for book {book}")
for chapter in chapters:
self.sync_chapter(data_path, book, chapter, parent_id)
def get_folder_id(self, folder_name: str, parent_id: Optional[str] = None) -> Optional[str]:
"""Finds a folder on Google Drive without creating it."""
query = f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
if parent_id:
query += f" and '{parent_id}' in parents"
try:
results = self.service.files().list(
q=query, fields="files(id, name)", supportsAllDrives=True, includeItemsFromAllDrives=True
).execute()
items = results.get('files', [])
if items:
return items[0]['id']
return None
except HttpError as error:
logging.error(f"Error finding folder '{folder_name}': {error}")
return None
def list_folder_contents(self, folder_id: str) -> List[Dict]:
"""Lists all files and folders inside a given Google Drive folder."""
query = f"'{folder_id}' in parents and trashed = false"
try:
results = self.service.files().list(
q=query, fields="files(id, name, mimeType)", supportsAllDrives=True, includeItemsFromAllDrives=True
).execute()
return results.get('files', [])
except HttpError as error:
logging.error(f"Error listing folder contents: {error}")
return []
def download_file(self, file_id: str, local_path: str):
"""Downloads a file from Google Drive."""
from googleapiclient.http import MediaIoBaseDownload
import io
file_name = os.path.basename(local_path)
try:
os.makedirs(os.path.dirname(local_path), exist_ok=True)
request = self.service.files().get_media(fileId=file_id)
with io.FileIO(local_path, 'wb') as fh:
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
logging.info(f"File downloaded: {file_name}")
except HttpError as error:
logging.error(f"Error downloading '{file_name}': {error}")
def download_verse(self, local_data_path: str, book: str, chapter: str, verse: str, parent_id: str):
"""Downloads files for a single verse from Google Drive."""
logging.info(f"Downloading verse: {verse} ({book}/{chapter})")
verse_id = self.get_folder_id(verse, parent_id)
if not verse_id:
logging.warning(f"Verse folder '{verse}' not found on Google Drive under parent {parent_id}.")
return
verse_local_path = os.path.join(local_data_path, book, chapter, verse)
os.makedirs(verse_local_path, exist_ok=True)
items = self.list_folder_contents(verse_id)
for item in items:
if item['mimeType'] != 'application/vnd.google-apps.folder':
local_file_path = os.path.join(verse_local_path, item['name'])
self.download_file(item['id'], local_file_path)
def download_chapter(self, local_data_path: str, book: str, chapter: str, parent_id: str):
"""Downloads a single chapter."""
logging.info(f"Downloading chapter: {chapter} ({book})")
chapter_id = self.get_folder_id(chapter, parent_id)
if not chapter_id:
logging.warning(f"Chapter folder '{chapter}' not found on Google Drive.")
return
chapter_local_path = os.path.join(local_data_path, book, chapter)
os.makedirs(chapter_local_path, exist_ok=True)
items = self.list_folder_contents(chapter_id)
for item in items:
if item['mimeType'] == 'application/vnd.google-apps.folder':
if item['name'].startswith('V_'):
self.download_verse(local_data_path, book, chapter, item['name'], chapter_id)
else:
local_file_path = os.path.join(chapter_local_path, item['name'])
self.download_file(item['id'], local_file_path)
def download_book(self, local_data_path: str, book: str, parent_id: str):
"""Downloads an entire book."""
logging.info(f"Downloading book: {book}")
book_id = self.get_folder_id(book, parent_id)
if not book_id:
logging.warning(f"Book folder '{book}' not found on Google Drive.")
return
book_local_path = os.path.join(local_data_path, book)
os.makedirs(book_local_path, exist_ok=True)
items = self.list_folder_contents(book_id)
for item in items:
if item['mimeType'] == 'application/vnd.google-apps.folder':
if item['name'].startswith(f"{book}_"):
self.download_chapter(local_data_path, book, item['name'], book_id)
else:
local_file_path = os.path.join(book_local_path, item['name'])
self.download_file(item['id'], local_file_path)
def download_books(self, local_data_path: str, books: List[str], parent_id: str):
"""Downloads multiple books."""
logging.info(f"Downloading books: {', '.join(books)}")
for book in books:
self.download_book(local_data_path, book, parent_id)
def download_chapters(self, local_data_path: str, book: str, chapters: List[str], parent_id: str):
"""Downloads multiple chapters."""
logging.info(f"Downloading chapters: {', '.join(chapters)} for book {book}")
for chapter in chapters:
self.download_chapter(local_data_path, book, chapter, parent_id)
def download_preprocessor(self, local_data_path: str, preprocessor_name: str, parent_id: str):
"""Downloads all tasks assigned to a specific preprocessor."""
assignment_file = os.path.join(local_data_path, "assignment.json")
if not os.path.exists(assignment_file):
logging.info("Assignment file not found locally. Searching on Google Drive...")
assignment_query = f"name = 'assignment.json' and '{parent_id}' in parents and trashed = false"
try:
results = self.service.files().list(
q=assignment_query, fields="files(id, name)", supportsAllDrives=True, includeItemsFromAllDrives=True
).execute()
items = results.get('files', [])
if items:
os.makedirs(local_data_path, exist_ok=True)
self.download_file(items[0]['id'], assignment_file)
logging.info("Successfully downloaded assignment.json from Google Drive.")
else:
logging.error("assignment.json not found on Google Drive. Cannot sync preprocessor tasks.")
return
except HttpError as error:
logging.error(f"Error looking up assignment.json on Google Drive: {error}")
return
with open(assignment_file, 'r', encoding='utf-8') as f:
assignments = json.load(f)
if preprocessor_name not in assignments:
logging.error(f"Preprocessor '{preprocessor_name}' not found in assignment file.")
return
preprocessor_tasks = assignments[preprocessor_name]
logging.info(f"Downloading all tasks for preprocessor: {preprocessor_name}")
for book, chapters in preprocessor_tasks.items():
if book == "total_duration_hours":
continue
if chapters == "all":
self.download_book(local_data_path, book, parent_id)
else:
book_id = self.get_folder_id(book, parent_id)
if not book_id:
logging.warning(f"Book folder '{book}' not found on Google Drive.")
continue
for chapter in chapters:
self.download_chapter(local_data_path, book, chapter, book_id)