2121
2222
2323class 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 )
2636
2737
2838@decorator
@@ -38,7 +48,7 @@ def _wrap_pydrive_retriable(call):
3848 "HttpError {}" .format (code ) in str (exception )
3949 for code in retry_codes
4050 ):
41- raise GDriveRetriableError (msg = "Google API request failed" )
51+ raise GDriveRetriableError ("Google API request failed" )
4252 raise
4353 return result
4454
@@ -78,7 +88,7 @@ def init_drive(self):
7888 "https://man.dvc.org/remote/add."
7989 )
8090 self .gdrive_user_credentials_path = (
81- tmp_fname (".dvc/tmp/" )
91+ tmp_fname (os . path . join ( self . repo . tmp_dir , "" ) )
8292 if os .getenv (RemoteGDrive .GDRIVE_USER_CREDENTIALS_DATA )
8393 else self .config .get (
8494 Config .SECTION_GDRIVE_USER_CREDENTIALS_FILE ,
@@ -161,6 +171,8 @@ def cached_ids(self):
161171 @property
162172 @wrap_with (threading .RLock ())
163173 def drive (self ):
174+ from pydrive .auth import RefreshError
175+
164176 if not hasattr (self , "_gdrive" ):
165177 from pydrive .auth import GoogleAuth
166178 from pydrive .drive import GoogleDrive
@@ -195,10 +207,27 @@ def drive(self):
195207
196208 # Pass non existent settings path to force DEFAULT_SETTINGS loading
197209 gauth = GoogleAuth (settings_file = "" )
198- gauth .CommandLineAuth ()
199210
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 )
202231
203232 self ._gdrive = GoogleDrive (gauth )
204233
0 commit comments