Simplify build_telemetry by having it load config immediately when initializing class.

This avoid check for possibly null self._config as now it's always non null.

Change-Id: I74075fec96898996f10d1f2589fe3c9f6a6a6964
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/7259442
Commit-Queue: Alex Ovsienko <ovsienko@google.com>
Reviewed-by: Junji Watanabe <jwata@google.com>
This commit is contained in:
Alex Ovsienko
2025-12-15 00:17:56 -08:00
committed by LUCI CQ
parent a9534daf99
commit 01b382b301

View File

@@ -25,15 +25,7 @@ class Config:
def __init__(self, config_path, countdown): def __init__(self, config_path, countdown):
self._config_path = config_path self._config_path = config_path
self._config = None
self._notice_displayed = False self._notice_displayed = False
self._countdown = countdown
def load(self):
"""Loads the build telemetry config."""
if self._config:
return
config = {} config = {}
if os.path.isfile(self._config_path): if os.path.isfile(self._config_path):
with open(self._config_path) as f: with open(self._config_path) as f:
@@ -48,13 +40,11 @@ class Config:
config = { config = {
"user": check_auth().get("email", ""), "user": check_auth().get("email", ""),
"status": None, "status": None,
"countdown": self._countdown, "countdown": countdown,
"version": VERSION, "version": VERSION,
} }
if not config.get("user"): if not config.get("user"):
config["user"] = check_auth().get("email", "") config["user"] = check_auth().get("email", "")
self._config = config self._config = config
def save(self): def save(self):
@@ -75,29 +65,17 @@ class Config:
@property @property
def user(self): def user(self):
if not self._config:
return
return self._config.get("user", "") return self._config.get("user", "")
@property @property
def countdown(self): def countdown(self):
if not self._config:
return
return self._config.get("countdown") return self._config.get("countdown")
@property @property
def version(self): def version(self):
if not self._config:
return
return self._config.get("version") return self._config.get("version")
def enabled(self): def enabled(self):
if not self._config:
print("WARNING: depot_tools.build_telemetry: %s is not loaded." %
self._config_path,
file=sys.stderr)
return False
if not self.is_googler or not self.is_corp_machine: if not self.is_googler or not self.is_corp_machine:
return False return False
if self._config.get("status") == "opt-out": if self._config.get("status") == "opt-out":
@@ -147,9 +125,7 @@ class Config:
def load_config(cfg_path=_DEFAULT_CONFIG_PATH, countdown=_DEFAULT_COUNTDOWN): def load_config(cfg_path=_DEFAULT_CONFIG_PATH, countdown=_DEFAULT_COUNTDOWN):
"""Loads the config from the default location.""" """Loads the config from the default location."""
cfg = Config(cfg_path, countdown) return Config(cfg_path, countdown)
cfg.load()
return cfg
def check_auth(): def check_auth():
@@ -162,7 +138,7 @@ def check_auth():
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
timeout=3, timeout=3,
) )
except Exception as e: except Exception:
return {} return {}
try: try:
return json.loads(out) return json.loads(out)