Skip to content

Commit 98afb40

Browse files
authored
Merge pull request #32 from iterative/fix-config-path
fix config path & format
2 parents 734d557 + 5c639b3 commit 98afb40

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

docs/gen_ref_pages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
elif parts[-1] == "__main__":
1919
continue
2020

21-
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
21+
with mkdocs_gen_files.open(full_doc_path, "w") as fobj:
2222
identifier = ".".join(parts)
23-
print("::: " + identifier, file=fd)
23+
print("::: " + identifier, file=fobj)
2424

2525
mkdocs_gen_files.set_edit_path(full_doc_path, path)

src/iterative_telemetry/__init__.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import uuid
1010
from functools import lru_cache
11+
from pathlib import Path
1112
from threading import Thread
1213
from typing import Any, Callable, Dict, Union
1314

@@ -74,8 +75,7 @@ def is_enabled(self):
7475
return (
7576
os.environ.get(DO_NOT_TRACK_ENV, None) is None and self.enabled()
7677
if callable(self.enabled)
77-
else self.enabled
78-
and _find_or_create_user_id() != DO_NOT_TRACK_VALUE
78+
else self.enabled and _find_or_create_user_id() is not None
7979
)
8080

8181
def send(
@@ -154,7 +154,7 @@ def _runtime_info(self):
154154
# "scm_class": _scm_in_use(),
155155
**_system_info(),
156156
"user_id": _find_or_create_user_id(),
157-
"group_id": _find_or_create_user_id(), # TODO
157+
"group_id": "", # TODO
158158
}
159159

160160

@@ -187,39 +187,64 @@ def _system_info():
187187
raise NotImplementedError
188188

189189

190+
def _generate_id():
191+
"""A randomly generated ID string"""
192+
return str(uuid.uuid4()) # TODO: CI env-based ID
193+
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+
190212
@lru_cache(None)
191213
def _find_or_create_user_id():
192214
"""
193215
The user's ID is stored on a file under the global config directory.
194216
The file should contain a JSON with a "user_id" key:
195217
{"user_id": "16fd2706-8baf-433b-82eb-8c7fada847da"}
196-
IDs are generated randomly with UUID.
218+
IDs are generated randomly with UUID4.
197219
"""
198-
199-
config_dir = user_config_dir("telemetry", "iterative")
200-
fname = os.path.join(config_dir, "user_id")
201-
lockfile = os.path.join(config_dir, "user_id.lock")
202-
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)
220+
config_file = Path(
221+
user_config_dir(os.path.join("iterative", "telemetry"), False)
222+
)
223+
config_file.parent.mkdir(mode=0o755, parents=True, exist_ok=True)
224+
lockfile = str(config_file.with_suffix(".lock"))
225+
# DVC backwards-compatibility
226+
config_file_old = Path(
227+
user_config_dir(os.path.join("dvc", "user_id"), "iterative")
228+
)
206229

207230
try:
208231
with FileLock( # pylint: disable=abstract-class-instantiated
209232
lockfile, timeout=5
210233
):
211-
try:
212-
with open(fname, encoding="utf8") as fobj:
213-
user_id = json.load(fobj)["user_id"]
214-
215-
except (FileNotFoundError, ValueError, KeyError):
216-
user_id = str(uuid.uuid4())
217-
218-
with open(fname, "w", encoding="utf8") as fobj:
234+
user_id = _read_user_id(config_file)
235+
if user_id is None:
236+
try:
237+
user_id = _read_user_id_locked(config_file_old)
238+
except Timeout:
239+
logger.debug(
240+
"Failed to acquire %s",
241+
config_file_old.with_suffix(".lock"),
242+
)
243+
return None
244+
if user_id is None:
245+
user_id = _generate_id()
246+
with config_file.open(mode="w", encoding="utf8") as fobj:
219247
json.dump({"user_id": user_id}, fobj)
220-
221-
return user_id
222-
223248
except Timeout:
224249
logger.debug("Failed to acquire %s", lockfile)
225-
return None
250+
return user_id if user_id.lower() != DO_NOT_TRACK_VALUE.lower() else None

0 commit comments

Comments
 (0)