@@ -196,26 +196,44 @@ def _find_or_create_user_id():
196
196
IDs are generated randomly with UUID.
197
197
"""
198
198
199
- config_dir = user_config_dir ("telemetry" , "iterative" )
200
- fname = os .path .join (config_dir , "user_id" )
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" )
202
+ user_id_file = os .path .join (config_dir , "user_id" )
201
203
lockfile = os .path .join (config_dir , "user_id.lock" )
202
204
203
- # Since the `fname` and `lockfile` are under the global config,
204
- # we need to make sure such directory exist already.
205
- os .makedirs (config_dir , exist_ok = True )
206
-
207
205
try :
208
206
with FileLock ( # pylint: disable=abstract-class-instantiated
209
207
lockfile , timeout = 5
210
208
):
211
209
try :
212
- with open (fname , encoding = "utf8" ) as fobj :
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
+
224
+ 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
+ with open (user_id_file , encoding = "utf8" ) as fobj :
213
231
user_id = json .load (fobj )["user_id" ]
214
232
215
233
except (FileNotFoundError , ValueError , KeyError ):
216
234
user_id = str (uuid .uuid4 ())
217
235
218
- with open (fname , "w" , encoding = "utf8" ) as fobj :
236
+ with open (user_id_file , "w" , encoding = "utf8" ) as fobj :
219
237
json .dump ({"user_id" : user_id }, fobj )
220
238
221
239
return user_id
0 commit comments