Skip to content

Commit 55ee3fb

Browse files
committed
Logic pass
1 parent 6927957 commit 55ee3fb

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/iterative_telemetry/__init__.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,42 +196,30 @@ def _find_or_create_user_id():
196196
IDs are generated randomly with UUID.
197197
"""
198198

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+
)
202202
user_id_file = os.path.join(config_dir, "user_id")
203203
lockfile = os.path.join(config_dir, "user_id.lock")
204204

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+
205210
try:
206211
with FileLock( # pylint: disable=abstract-class-instantiated
207212
lockfile, timeout=5
208213
):
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
223214

224215
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-
230216
with open(user_id_file, encoding="utf8") as fobj:
231217
user_id = json.load(fobj)["user_id"]
232218

233219
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())
235223

236224
with open(user_id_file, "w", encoding="utf8") as fobj:
237225
json.dump({"user_id": user_id}, fobj)
@@ -241,3 +229,18 @@ def _find_or_create_user_id():
241229
except Timeout:
242230
logger.debug("Failed to acquire %s", lockfile)
243231
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

Comments
 (0)