@@ -196,42 +196,30 @@ def _find_or_create_user_id():
196
196
IDs are generated randomly with UUID.
197
197
"""
198
198
199
- legacy_dvc_config_dir = user_config_dir ("dvc" , "iterative" )
200
- config_dir = user_config_dir ( os .path .join ("iterative" , "telemetry" ), "iterative" )
201
- legacy_user_id_file = os . path . join ( legacy_dvc_config_dir , "user_id" )
199
+ config_dir = user_config_dir (
200
+ os .path .join ("iterative" , "telemetry" ), "iterative"
201
+ )
202
202
user_id_file = os .path .join (config_dir , "user_id" )
203
203
lockfile = os .path .join (config_dir , "user_id.lock" )
204
204
205
+ # Since the `user_id_file` and `lockfile` are under the
206
+ # global config, we need to make sure such directory exist
207
+ # already.
208
+ os .makedirs (config_dir , exist_ok = True )
209
+
205
210
try :
206
211
with FileLock ( # pylint: disable=abstract-class-instantiated
207
212
lockfile , timeout = 5
208
213
):
209
- try :
210
-
211
- # Backwards compatibility with DVC legacy telemetry file location
212
- # Will only try to copy over if config_dir doesn't exist (first run per machine)
213
- if legacy_dvc_config_dir .exists () and not config_dir .exists ():
214
- with open (legacy_user_id_file , encoding = "utf8" ) as fobj_legacy :
215
- with open (user_id_file , "w" , encoding = "utf8" ) as fobj_new :
216
- user_id = json .load (fobj_legacy )["user_id" ]
217
- json .dump ({"user_id" : user_id }, fobj_new )
218
-
219
- except (FileNotFoundError , ValueError , KeyError ):
220
-
221
- # Fail silently
222
- pass
223
214
224
215
try :
225
-
226
- # Since the `user_id_file` and `lockfile` are under the global config,
227
- # we need to make sure such directory exist already.
228
- os .makedirs (config_dir , exist_ok = True )
229
-
230
216
with open (user_id_file , encoding = "utf8" ) as fobj :
231
217
user_id = json .load (fobj )["user_id" ]
232
218
233
219
except (FileNotFoundError , ValueError , KeyError ):
234
- user_id = str (uuid .uuid4 ())
220
+
221
+ # Backwards compatibility with DVC legacy telemetry location.
222
+ user_id = _try_read_legacy_user_id () or str (uuid .uuid4 ())
235
223
236
224
with open (user_id_file , "w" , encoding = "utf8" ) as fobj :
237
225
json .dump ({"user_id" : user_id }, fobj )
@@ -241,3 +229,18 @@ def _find_or_create_user_id():
241
229
except Timeout :
242
230
logger .debug ("Failed to acquire %s" , lockfile )
243
231
return None
232
+
233
+
234
+ def _try_read_legacy_user_id ():
235
+ legacy_user_id_file = os .path .join (
236
+ user_config_dir ("dvc" , "iterative" ), "user_id"
237
+ )
238
+
239
+ try :
240
+ with open (legacy_user_id_file , encoding = "utf8" ) as fobj :
241
+ return json .load (fobj )["user_id" ]
242
+
243
+ except (FileNotFoundError , ValueError , KeyError ):
244
+ pass
245
+
246
+ return None
0 commit comments