21
21
22
22
23
23
class GDriveRetriableError (DvcException ):
24
- def __init__ (self , msg ):
25
- super (GDriveRetriableError , self ).__init__ (msg )
24
+ def __init__ (self , msg , cause = None ):
25
+ super (GDriveRetriableError , self ).__init__ (msg , cause = cause )
26
+
27
+
28
+ class GDriveAccessTokenRefreshError (DvcException ):
29
+ def __init__ (self , msg , cause = None ):
30
+ super (GDriveAccessTokenRefreshError , self ).__init__ (msg , cause = cause )
31
+
32
+
33
+ class GDriveMissedCredentialKeyError (DvcException ):
34
+ def __init__ (self , msg , cause = None ):
35
+ super (GDriveMissedCredentialKeyError , self ).__init__ (msg , cause = cause )
26
36
27
37
28
38
@decorator
@@ -38,7 +48,7 @@ def _wrap_pydrive_retriable(call):
38
48
"HttpError {}" .format (code ) in str (exception )
39
49
for code in retry_codes
40
50
):
41
- raise GDriveRetriableError (msg = "Google API request failed" )
51
+ raise GDriveRetriableError ("Google API request failed" )
42
52
raise
43
53
return result
44
54
@@ -78,7 +88,7 @@ def init_drive(self):
78
88
"https://man.dvc.org/remote/add."
79
89
)
80
90
self .gdrive_user_credentials_path = (
81
- tmp_fname (".dvc/tmp/" )
91
+ tmp_fname (os . path . join ( self . repo . tmp_dir , "" ) )
82
92
if os .getenv (RemoteGDrive .GDRIVE_USER_CREDENTIALS_DATA )
83
93
else self .config .get (
84
94
Config .SECTION_GDRIVE_USER_CREDENTIALS_FILE ,
@@ -161,6 +171,8 @@ def cached_ids(self):
161
171
@property
162
172
@wrap_with (threading .RLock ())
163
173
def drive (self ):
174
+ from pydrive .auth import RefreshError
175
+
164
176
if not hasattr (self , "_gdrive" ):
165
177
from pydrive .auth import GoogleAuth
166
178
from pydrive .drive import GoogleDrive
@@ -195,10 +207,27 @@ def drive(self):
195
207
196
208
# Pass non existent settings path to force DEFAULT_SETTINGS loading
197
209
gauth = GoogleAuth (settings_file = "" )
198
- gauth .CommandLineAuth ()
199
210
200
- if os .getenv (RemoteGDrive .GDRIVE_USER_CREDENTIALS_DATA ):
201
- os .remove (self .gdrive_user_credentials_path )
211
+ try :
212
+ gauth .CommandLineAuth ()
213
+ except RefreshError as e :
214
+ raise GDriveAccessTokenRefreshError (
215
+ "Google Drive's access token refreshment is failed" , e
216
+ )
217
+ except KeyError as e :
218
+ raise GDriveMissedCredentialKeyError (
219
+ "Google Drive's user credentials file '{}' "
220
+ "misses value for key '{}'" .format (
221
+ self .gdrive_user_credentials_path , str (e )
222
+ ),
223
+ e ,
224
+ )
225
+ # Handle pydrive.auth.AuthenticationError and others auth failures
226
+ except Exception as e :
227
+ raise DvcException ("Google Drive authentication failed" , e )
228
+ finally :
229
+ if os .getenv (RemoteGDrive .GDRIVE_USER_CREDENTIALS_DATA ):
230
+ os .remove (self .gdrive_user_credentials_path )
202
231
203
232
self ._gdrive = GoogleDrive (gauth )
204
233
0 commit comments