-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathidentity.py
More file actions
66 lines (50 loc) · 2.03 KB
/
identity.py
File metadata and controls
66 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import uuid
from dataclasses import dataclass
from pathlib import Path
from timecapsulesmb.core.config import parse_env_value
from timecapsulesmb.core.paths import package_project_root, resolve_app_paths
BOOTSTRAP_PATH = package_project_root() / ".bootstrap"
@dataclass(frozen=True)
class InstallIdentity:
install_id: str | None
telemetry_enabled: bool
def default_bootstrap_path() -> Path:
return resolve_app_paths().bootstrap_path
def parse_bootstrap_values(path: Path | None = None) -> dict[str, str]:
resolved_path = path or default_bootstrap_path()
values: dict[str, str] = {}
try:
text = resolved_path.read_text()
except FileNotFoundError:
return values
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
values[key.strip()] = parse_env_value(value)
return values
def load_install_identity(path: Path | None = None) -> InstallIdentity:
values = parse_bootstrap_values(path)
telemetry_raw = values.get("TELEMETRY", "").strip().lower()
telemetry_enabled = telemetry_raw != "false"
return InstallIdentity(
install_id=values.get("INSTALL_ID") or None,
telemetry_enabled=telemetry_enabled,
)
def render_bootstrap_text(install_id: str, *, telemetry_enabled: bool = True) -> str:
lines = [f"INSTALL_ID={install_id}"]
if not telemetry_enabled:
lines.append("TELEMETRY=false")
lines.append("")
return "\n".join(lines)
def ensure_install_id(path: Path | None = None) -> str:
resolved_path = path or default_bootstrap_path()
identity = load_install_identity(resolved_path)
if identity.install_id:
return identity.install_id
install_id = str(uuid.uuid4())
resolved_path.parent.mkdir(parents=True, exist_ok=True)
resolved_path.write_text(render_bootstrap_text(install_id, telemetry_enabled=identity.telemetry_enabled))
return install_id