3
3
import os
4
4
import posixpath
5
5
import logging
6
+ import uuid
6
7
7
8
from funcy import cached_property , retry , compose , decorator
8
9
from funcy .py3 import cat
@@ -46,6 +47,10 @@ def _wrap_pydrive_retriable(call):
46
47
)
47
48
48
49
50
+ def get_tmp_filepath ():
51
+ return posixpath .join (".dvc" , "tmp" , str (uuid .uuid4 ()))
52
+
53
+
49
54
class RemoteGDrive (RemoteBASE ):
50
55
scheme = Schemes .GDRIVE
51
56
path_cls = CloudURLInfo
@@ -74,18 +79,22 @@ def init_drive(self):
74
79
"secret in DVC's config. Learn more at "
75
80
"https://man.dvc.org/remote/add."
76
81
)
77
- self .gdrive_user_credentials_path = self .config .get (
78
- Config .SECTION_GDRIVE_USER_CREDENTIALS_FILE ,
79
- self .DEFAULT_USER_CREDENTIALS_FILE ,
82
+ self .gdrive_user_credentials_path = (
83
+ get_tmp_filepath ()
84
+ if os .getenv (RemoteGDrive .GDRIVE_USER_CREDENTIALS_DATA )
85
+ else self .config .get (
86
+ Config .SECTION_GDRIVE_USER_CREDENTIALS_FILE ,
87
+ self .DEFAULT_USER_CREDENTIALS_FILE ,
88
+ )
80
89
)
81
90
82
91
self .root_id = self .get_path_id (self .path_info , create = True )
83
92
self .cached_dirs , self .cached_ids = self .cache_root_dirs ()
84
93
85
- def request_list_file (self , query ):
86
- return self .drive .ListFile ({"q" : query , "maxResults" : 1000 }).GetList ()
94
+ def gdrive_list_file (self , query ):
95
+ return self .drive .ListFile ({"q" : query , "maxResults" : 1 }).GetList ()
87
96
88
- def request_create_folder (self , title , parent_id ):
97
+ def gdrive_create_folder (self , title , parent_id ):
89
98
item = self .drive .CreateFile (
90
99
{
91
100
"title" : title ,
@@ -96,7 +105,7 @@ def request_create_folder(self, title, parent_id):
96
105
item .Upload ()
97
106
return item
98
107
99
- def request_upload_file (
108
+ def gdrive_upload_file (
100
109
self , args , no_progress_bar = True , from_file = "" , progress_name = ""
101
110
):
102
111
item = self .drive .CreateFile (
@@ -113,7 +122,7 @@ def upload_file(self, item, no_progress_bar, from_file, progress_name):
113
122
item .content = opened_file
114
123
item .Upload ()
115
124
116
- def request_download_file (
125
+ def gdrive_download_file (
117
126
self , file_id , to_file , progress_name , no_progress_bar
118
127
):
119
128
from dvc .progress import Tqdm
@@ -125,7 +134,7 @@ def request_download_file(
125
134
if not no_progress_bar :
126
135
tqdm .close ()
127
136
128
- def list_drive_item (self , query ):
137
+ def gdrive_list_item (self , query ):
129
138
file_list = self .drive .ListFile ({"q" : query , "maxResults" : 1000 })
130
139
131
140
# Isolate and decorate fetching of remote drive items in pages
@@ -137,7 +146,7 @@ def list_drive_item(self, query):
137
146
def cache_root_dirs (self ):
138
147
cached_dirs = {}
139
148
cached_ids = {}
140
- for dir1 in self .list_drive_item (
149
+ for dir1 in self .gdrive_list_item (
141
150
"'{}' in parents and trashed=false" .format (self .root_id )
142
151
):
143
152
cached_dirs .setdefault (dir1 ["title" ], []).append (dir1 ["id" ])
@@ -189,7 +198,7 @@ def drive(self):
189
198
190
199
def create_drive_item (self , parent_id , title ):
191
200
return gdrive_retry (
192
- lambda : self .request_create_folder (title , parent_id )
201
+ lambda : self .gdrive_create_folder (title , parent_id )
193
202
)()
194
203
195
204
def get_drive_item (self , name , parents_ids ):
@@ -201,7 +210,7 @@ def get_drive_item(self, name, parents_ids):
201
210
202
211
query += " and trashed=false and title='{}'" .format (name )
203
212
204
- item_list = gdrive_retry (lambda : self .request_list_file (query ))()
213
+ item_list = gdrive_retry (lambda : self .gdrive_list_file (query ))()
205
214
return next (iter (item_list ), None )
206
215
207
216
def resolve_remote_file (self , parents_ids , path_parts , create ):
@@ -260,7 +269,7 @@ def _upload(self, from_file, to_info, name, no_progress_bar):
260
269
parent_id = to_info .bucket
261
270
262
271
gdrive_retry (
263
- lambda : self .request_upload_file (
272
+ lambda : self .gdrive_upload_file (
264
273
{"title" : to_info .name , "parent_id" : parent_id },
265
274
no_progress_bar ,
266
275
from_file ,
@@ -271,7 +280,7 @@ def _upload(self, from_file, to_info, name, no_progress_bar):
271
280
def _download (self , from_info , to_file , name , no_progress_bar ):
272
281
file_id = self .get_path_id (from_info )
273
282
gdrive_retry (
274
- lambda : self .request_download_file (
283
+ lambda : self .gdrive_download_file (
275
284
file_id , to_file , name , no_progress_bar
276
285
)
277
286
)()
@@ -290,7 +299,7 @@ def list_file_path(self, drive_file):
290
299
yield drive_file ["title" ]
291
300
292
301
def list_path (self , parent_id ):
293
- for file1 in self .list_drive_item (
302
+ for file1 in self .gdrive_list_item (
294
303
"'{}' in parents and trashed=false" .format (parent_id )
295
304
):
296
305
for path in self .list_file_path (file1 ):
@@ -305,7 +314,7 @@ def all(self):
305
314
)
306
315
307
316
query += " and trashed=false"
308
- for file1 in self .list_drive_item (query ):
317
+ for file1 in self .gdrive_list_item (query ):
309
318
parent_id = file1 ["parents" ][0 ]["id" ]
310
319
path = posixpath .join (self .cached_ids [parent_id ], file1 ["title" ])
311
320
try :
0 commit comments