@@ -187,11 +187,28 @@ def _system_info():
187
187
raise NotImplementedError
188
188
189
189
190
- def generate_id ():
190
+ def _generate_id ():
191
191
"""A randomly generated ID string"""
192
192
return str (uuid .uuid4 ()) # TODO: CI env-based ID
193
193
194
194
195
+ def _read_user_id (config_file : Path ):
196
+ try :
197
+ with config_file .open (encoding = "utf8" ) as fobj :
198
+ return json .load (fobj )["user_id" ]
199
+ except (FileNotFoundError , ValueError , KeyError ):
200
+ pass
201
+ return None
202
+
203
+
204
+ def _read_user_id_locked (config_file : Path ):
205
+ lockfile = str (config_file .with_suffix (".lock" ))
206
+ if config_file .parent .is_dir ():
207
+ with FileLock (lockfile , timeout = 5 ):
208
+ return _read_user_id (config_file )
209
+ return None
210
+
211
+
195
212
@lru_cache (None )
196
213
def _find_or_create_user_id ():
197
214
"""
@@ -209,19 +226,16 @@ def _find_or_create_user_id():
209
226
config_file_old = Path (
210
227
user_config_dir (os .path .join ("dvc" , "user_id" ), "iterative" )
211
228
)
212
- lockfile_old = str (config_file_old .with_suffix (".lock" ))
213
229
214
230
try :
215
231
with FileLock ( # pylint: disable=abstract-class-instantiated
216
232
lockfile , timeout = 5
217
233
):
218
- user_id = read_user_id (config_file )
234
+ user_id = _read_user_id (config_file )
219
235
if user_id is None :
220
- if config_file_old .parent .is_dir ():
221
- with FileLock (lockfile_old , timeout = 5 ):
222
- user_id = read_user_id (config_file_old )
223
- if user_id is None :
224
- user_id = generate_id ()
236
+ user_id = _read_user_id_locked (config_file_old )
237
+ if user_id is None :
238
+ user_id = _generate_id ()
225
239
with config_file .open (mode = "w" , encoding = "utf8" ) as fobj :
226
240
json .dump ({"user_id" : user_id }, fobj )
227
241
@@ -230,12 +244,3 @@ def _find_or_create_user_id():
230
244
except Timeout :
231
245
logger .debug ("Failed to acquire %s" , lockfile )
232
246
return None
233
-
234
-
235
- def read_user_id (config_file : Path ):
236
- try :
237
- with config_file .open (encoding = "utf8" ) as fobj :
238
- return json .load (fobj )["user_id" ]
239
- except (FileNotFoundError , ValueError , KeyError ):
240
- pass
241
- return None
0 commit comments