-
Notifications
You must be signed in to change notification settings - Fork 71
/
spec.py
704 lines (588 loc) · 22 KB
/
spec.py
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
import appdirs
import errno
import io
import logging
import os
import posixpath
import threading
from collections import defaultdict
from contextlib import contextmanager
from itertools import chain
from fsspec.spec import AbstractFileSystem
from funcy import cached_property, retry, wrap_prop, wrap_with
from tqdm.utils import CallbackIOWrapper
from pydrive2.drive import GoogleDrive
from pydrive2.fs.utils import IterStream
from pydrive2.auth import GoogleAuth
logger = logging.getLogger(__name__)
FOLDER_MIME_TYPE = "application/vnd.google-apps.folder"
COMMON_SETTINGS = {
"get_refresh_token": True,
"oauth_scope": [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.appdata",
],
}
class GDriveAuthError(Exception):
pass
def _gdrive_retry(func):
def should_retry(exc):
from pydrive2.files import ApiRequestError
if not isinstance(exc, ApiRequestError):
return False
error_code = exc.error.get("code", 0)
result = False
if 500 <= error_code < 600:
result = True
if error_code == 403:
result = exc.GetField("reason") in [
"userRateLimitExceeded",
"rateLimitExceeded",
]
if result:
logger.debug(f"Retrying GDrive API call, error: {exc}.")
return result
# 16 tries, start at 0.5s, multiply by golden ratio, cap at 20s
return retry(
16,
timeout=lambda a: min(0.5 * 1.618**a, 20),
filter_errors=should_retry,
)(func)
@contextmanager
def _wrap_errors():
try:
yield
except Exception as exc:
# Handle AuthenticationError, RefreshError and other auth failures
# It's hard to come up with a narrow exception, since PyDrive throws
# a lot of different errors - broken credentials file, refresh token
# expired, flow failed, etc.
raise GDriveAuthError("Failed to authenticate GDrive") from exc
def _client_auth(
client_id=None,
client_secret=None,
client_json=None,
client_json_file_path=None,
profile=None,
):
if client_json:
save_settings = {
"save_credentials_backend": "dictionary",
"save_credentials_dict": {"creds": client_json},
"save_credentials_key": "creds",
}
else:
creds_file = client_json_file_path
if not creds_file:
cache_dir = os.path.join(
appdirs.user_cache_dir("pydrive2fs", appauthor=False),
client_id,
)
os.makedirs(cache_dir, exist_ok=True)
profile = profile or "default"
creds_file = os.path.join(cache_dir, f"{profile}.json")
save_settings = {
"save_credentials_backend": "file",
"save_credentials_file": creds_file,
}
settings = {
**COMMON_SETTINGS,
"save_credentials": True,
**save_settings,
"client_config_backend": "settings",
"client_config": {
"client_id": client_id,
"client_secret": client_secret,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"revoke_uri": "https://oauth2.googleapis.com/revoke",
"redirect_uri": "",
},
}
auth = GoogleAuth(settings=settings)
with _wrap_errors():
auth.LocalWebserverAuth()
return auth
def _service_auth(
client_user_email=None,
client_json=None,
client_json_file_path=None,
):
settings = {
**COMMON_SETTINGS,
"client_config_backend": "service",
"service_config": {
"client_user_email": client_user_email,
"client_json": client_json,
"client_json_file_path": client_json_file_path,
},
}
auth = GoogleAuth(settings=settings)
with _wrap_errors():
auth.ServiceAuth()
return auth
class GDriveFileSystem(AbstractFileSystem):
"""Access to gdrive as an fsspec filesystem"""
def __init__(
self,
path,
google_auth=None,
client_id=None,
client_secret=None,
client_user_email=None,
client_json=None,
client_json_file_path=None,
use_service_account=False,
profile=None,
trash_only=True,
acknowledge_abuse=False,
**kwargs,
):
"""Create an instance of GDriveFileSystem.
:param path: gdrive path.
:type path: str.
:param google_auth: Authenticated GoogleAuth instance.
:type google_auth: GoogleAuth.
:param client_id: Client ID of the application.
:type client_id: str
:param client_secret: Client secret of the application.
:type client_secret: str.
:param client_user_email: User email that authority was delegated to
(only for service account).
:type client_user_email: str.
:param client_json: JSON keyfile loaded into a string.
:type client_json: str.
:param client_json_file_path: Path to JSON keyfile.
:type client_json_file_path: str.
:param use_service_account: Use service account.
:type use_service_account: bool.
:param profile: Profile name for caching credentials
(ignored for service account).
:param trash_only: Move files to trash instead of deleting.
:type trash_only: bool.
:param acknowledge_abuse: Acknowledging the risk and download file
identified as abusive.
:type acknowledge_abuse: bool
:type profile: str.
:raises: GDriveAuthError
"""
super().__init__(**kwargs)
self.path = path
self.root, self.base = self.split_path(self.path)
if not google_auth:
if (
not client_json
and not client_json_file_path
and not (client_id and client_secret)
):
raise ValueError(
"Specify credentials using one of these methods: "
"client_id/client_secret or "
"client_json or "
"client_json_file_path"
)
if use_service_account:
google_auth = _service_auth(
client_json=client_json,
client_json_file_path=client_json_file_path,
client_user_email=client_user_email,
)
else:
google_auth = _client_auth(
client_id=client_id,
client_secret=client_secret,
client_json=client_json,
client_json_file_path=client_json_file_path,
profile=profile,
)
self.client = GoogleDrive(google_auth)
self._trash_only = trash_only
self._acknowledge_abuse = acknowledge_abuse
def split_path(self, path):
parts = path.replace("//", "/").rstrip("/").split("/", 1)
if len(parts) == 2:
return parts
else:
return parts[0], ""
@wrap_prop(threading.RLock())
@cached_property
def _ids_cache(self):
cache = {
"dirs": defaultdict(list),
"ids": {},
"root_id": self._get_item_id(
self.path,
use_cache=False,
hint="Confirm the directory exists and you can access it.",
),
}
self._cache_path_id(self.base, cache["root_id"], cache=cache)
for item in self._gdrive_list(
"'{}' in parents and trashed=false".format(cache["root_id"])
):
item_path = posixpath.join(self.base, item["title"])
self._cache_path_id(item_path, item["id"], cache=cache)
return cache
def _cache_path_id(self, path, *item_ids, cache=None):
cache = cache or self._ids_cache
for item_id in item_ids:
cache["dirs"][path].append(item_id)
cache["ids"][item_id] = path
@cached_property
def _list_params(self):
params = {"corpora": "default"}
if self.root != "root" and self.root != "appDataFolder":
drive_id = self._gdrive_shared_drive_id(self.root)
if drive_id:
logger.debug(
"GDrive remote '{}' is using shared drive id '{}'.".format(
self.path, drive_id
)
)
params["driveId"] = drive_id
params["corpora"] = "drive"
return params
@_gdrive_retry
def _gdrive_shared_drive_id(self, item_id):
from pydrive2.files import ApiRequestError
param = {"id": item_id}
# it does not create a file on the remote
item = self.client.CreateFile(param)
# ID of the shared drive the item resides in.
# Only populated for items in shared drives.
try:
item.FetchMetadata("driveId")
except ApiRequestError as exc:
error_code = exc.error.get("code", 0)
if error_code == 404:
raise PermissionError from exc
raise
return item.get("driveId", None)
def _gdrive_list(self, query):
param = {"q": query, "maxResults": 1000}
param.update(self._list_params)
file_list = self.client.ListFile(param)
# Isolate and decorate fetching of remote drive items in pages.
get_list = _gdrive_retry(lambda: next(file_list, None))
# Fetch pages until None is received, lazily flatten the thing.
return chain.from_iterable(iter(get_list, None))
def _gdrive_list_ids(self, query_ids):
query = " or ".join(
f"'{query_id}' in parents" for query_id in query_ids
)
query = f"({query}) and trashed=false"
return self._gdrive_list(query)
def _get_remote_item_ids(self, parent_ids, title):
if not parent_ids:
return None
query = "trashed=false and ({})".format(
" or ".join(
f"'{parent_id}' in parents" for parent_id in parent_ids
)
)
query += " and title='{}'".format(title.replace("'", "\\'"))
# GDrive list API is case insensitive, we need to compare
# all results and pick the ones with the right title
return [
item["id"]
for item in self._gdrive_list(query)
if item["title"] == title
]
def _get_cached_item_ids(self, path, use_cache):
if not path:
return [self.root]
if use_cache:
return self._ids_cache["dirs"].get(path, [])
return []
def _path_to_item_ids(self, path, create=False, use_cache=True):
item_ids = self._get_cached_item_ids(path, use_cache)
if item_ids:
return item_ids
parent_path, title = posixpath.split(path)
parent_ids = self._path_to_item_ids(parent_path, create, use_cache)
item_ids = self._get_remote_item_ids(parent_ids, title)
if item_ids:
return item_ids
return (
[self._create_dir(min(parent_ids), title, path)] if create else []
)
def _get_item_id(self, path, create=False, use_cache=True, hint=None):
bucket, base = self.split_path(path)
assert bucket == self.root
item_ids = self._path_to_item_ids(base, create, use_cache)
if item_ids:
return min(item_ids)
assert not create
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), hint or path
)
@_gdrive_retry
def _gdrive_create_dir(self, parent_id, title):
parent = {"id": parent_id}
item = self.client.CreateFile(
{"title": title, "parents": [parent], "mimeType": FOLDER_MIME_TYPE}
)
item.Upload()
return item
@wrap_with(threading.RLock())
def _create_dir(self, parent_id, title, remote_path):
cached = self._ids_cache["dirs"].get(remote_path)
if cached:
return cached[0]
item = self._gdrive_create_dir(parent_id, title)
if parent_id == self._ids_cache["root_id"]:
self._cache_path_id(remote_path, item["id"])
return item["id"]
def exists(self, path):
try:
self._get_item_id(path)
except FileNotFoundError:
return False
else:
return True
@_gdrive_retry
def info(self, path):
bucket, base = self.split_path(path)
item_id = self._get_item_id(path)
gdrive_file = self.client.CreateFile({"id": item_id})
gdrive_file.FetchMetadata()
metadata = {"name": posixpath.join(bucket, base.rstrip("/"))}
if gdrive_file["mimeType"] == FOLDER_MIME_TYPE:
metadata["type"] = "directory"
metadata["size"] = 0
metadata["name"] += "/"
else:
metadata["type"] = "file"
metadata["size"] = int(gdrive_file.get("fileSize"))
metadata["checksum"] = gdrive_file.get("md5Checksum")
return metadata
def ls(self, path, detail=False):
bucket, base = self.split_path(path)
cached = base in self._ids_cache["dirs"]
if cached:
dir_ids = self._ids_cache["dirs"][base]
else:
dir_ids = self._path_to_item_ids(base)
if not dir_ids:
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), path
)
root_path = posixpath.join(bucket, base)
contents = []
for item in self._gdrive_list_ids(dir_ids):
item_path = posixpath.join(root_path, item["title"])
if item["mimeType"] == FOLDER_MIME_TYPE:
contents.append(
{
"type": "directory",
"name": item_path.rstrip("/") + "/",
"size": 0,
}
)
else:
size = item.get("fileSize")
contents.append(
{
"type": "file",
"name": item_path,
"size": int(size) if size is not None else size,
"checksum": item.get("md5Checksum"),
}
)
if not cached:
self._cache_path_id(root_path, *dir_ids)
if detail:
return contents
else:
return [content["name"] for content in contents]
def find(self, path, detail=False, **kwargs):
bucket, base = self.split_path(path)
seen_paths = set()
cached = base in self._ids_cache["dirs"]
if not cached:
dir_ids = self._path_to_item_ids(base)
self._cache_path_id(base, *dir_ids)
dir_ids = [self._ids_cache["ids"].copy()]
contents = []
while dir_ids:
query_ids = {
dir_id: dir_name
for dir_id, dir_name in dir_ids.pop().items()
if posixpath.commonpath([base, dir_name]) == base
if dir_id not in seen_paths
}
if not query_ids:
continue
seen_paths |= query_ids.keys()
new_query_ids = {}
dir_ids.append(new_query_ids)
for item in self._gdrive_list_ids(query_ids):
parent_id = item["parents"][0]["id"]
item_path = posixpath.join(query_ids[parent_id], item["title"])
if item["mimeType"] == FOLDER_MIME_TYPE:
new_query_ids[item["id"]] = item_path
self._cache_path_id(item_path, item["id"])
continue
size = item.get("fileSize")
contents.append(
{
"name": posixpath.join(bucket, item_path),
"type": "file",
"size": int(size) if size is not None else size,
"checksum": item.get("md5Checksum"),
}
)
if detail:
return {content["name"]: content for content in contents}
else:
return [content["name"] for content in contents]
def upload_fobj(self, stream, rpath, callback=None, **kwargs):
parent_id = self._get_item_id(self._parent(rpath), create=True)
if callback:
stream = CallbackIOWrapper(
callback.relative_update, stream, "read"
)
return self._gdrive_upload_fobj(
posixpath.basename(rpath.rstrip("/")), parent_id, stream
)
def put_file(self, lpath, rpath, callback=None, **kwargs):
if callback:
callback.set_size(os.path.getsize(lpath))
with open(lpath, "rb") as stream:
self.upload_fobj(stream, rpath, callback=callback)
@_gdrive_retry
def _gdrive_upload_fobj(self, title, parent_id, stream, callback=None):
item = self.client.CreateFile(
{"title": title, "parents": [{"id": parent_id}]}
)
item.content = stream
item.Upload()
return item
def cp_file(self, lpath, rpath, **kwargs):
"""In-memory streamed copy"""
with self.open(lpath) as stream:
# IterStream objects doesn't support full-length
# seek() calls, so we have to wrap the data with
# an external buffer.
buffer = io.BytesIO(stream.read())
self.upload_fobj(buffer, rpath)
@_gdrive_retry
def mv(self, path1, path2, maxdepth=None, **kwargs):
if maxdepth is not None:
raise NotImplementedError("Max depth move is not supported")
src_name = posixpath.basename(path1)
src_parent = self._parent(path1)
if self.exists(path2):
dst_name = src_name
dst_parent = path2
else:
dst_name = posixpath.basename(path2)
dst_parent = self._parent(path2)
file1_id = self._get_item_id(path1)
file1 = self.client.CreateFile({"id": file1_id})
if src_name != dst_name:
file1["title"] = dst_name
if src_parent != dst_parent:
file2_parent_id = self._get_item_id(dst_parent)
file1["parents"] = [{"id": file2_parent_id}]
# TODO need to invalidate the cache for the old path, see #232
file1.Upload()
def get_file(self, lpath, rpath, callback=None, block_size=None, **kwargs):
item_id = self._get_item_id(lpath)
return self._gdrive_get_file(
item_id, rpath, callback=callback, block_size=block_size
)
@_gdrive_retry
def _gdrive_get_file(self, item_id, rpath, callback=None, block_size=None):
param = {"id": item_id}
# it does not create a file on the remote
gdrive_file = self.client.CreateFile(param)
extra_args = {"acknowledge_abuse": self._acknowledge_abuse}
if block_size:
extra_args["chunksize"] = block_size
if callback:
def cb(value, _):
callback.absolute_update(value)
gdrive_file.FetchMetadata(fields="fileSize")
callback.set_size(int(gdrive_file.get("fileSize")))
extra_args["callback"] = cb
gdrive_file.GetContentFile(rpath, **extra_args)
def _open(self, path, mode, **kwargs):
assert mode in {"rb", "wb"}
if mode == "wb":
return GDriveBufferedWriter(self, path)
else:
item_id = self._get_item_id(path)
return self._gdrive_open_file(item_id)
@_gdrive_retry
def _gdrive_open_file(self, item_id):
param = {"id": item_id}
# it does not create a file on the remote
gdrive_file = self.client.CreateFile(param)
fd = gdrive_file.GetContentIOBuffer(
acknowledge_abuse=self._acknowledge_abuse
)
return IterStream(iter(fd))
def rm_file(self, path):
item_id = self._get_item_id(path)
self._gdrive_delete_file(item_id)
@_gdrive_retry
def _gdrive_delete_file(self, item_id):
from pydrive2.files import ApiRequestError
param = {"id": item_id}
# it does not create a file on the remote
item = self.client.CreateFile(param)
try:
item.Trash() if self._trash_only else item.Delete()
except ApiRequestError as exc:
http_error_code = exc.error.get("code", 0)
if (
http_error_code == 403
and self._list_params["corpora"] == "drive"
and exc.GetField("location") == "file.permissions"
):
raise PermissionError(
"Insufficient permissions to {}. You should have {} "
"access level for the used shared drive. More details "
"at {}.".format(
"move the file into Trash"
if self._trash_only
else "permanently delete the file",
"Manager or Content Manager"
if self._trash_only
else "Manager",
"https://support.google.com/a/answer/7337554",
)
) from exc
raise
class GDriveBufferedWriter(io.IOBase):
def __init__(self, fs, path):
self.fs = fs
self.path = path
self.buffer = io.BytesIO()
self._closed = False
def write(self, *args, **kwargs):
self.buffer.write(*args, **kwargs)
def readable(self):
return False
def writable(self):
return not self.readable()
def flush(self):
self.buffer.flush()
try:
self.fs.upload_fobj(self.buffer, self.path)
finally:
self._closed = True
def close(self):
if self._closed:
return None
self.flush()
self.buffer.close()
self._closed = True
def __enter__(self):
return self
def __exit__(self, *exc_info):
self.close()
@property
def closed(self):
return self._closed