From 10db1edc7f6eaba28e93479b11d483340bd3826b Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 19 Jan 2026 15:50:00 -0800 Subject: [PATCH] merge common.util and common.utils (#36951) * common: merge common.util and common.utils * cleanup * cleanup --- cereal/messaging/__init__.py | 2 +- common/realtime.py | 2 +- common/util.py | 46 --------------------------- common/utils.py | 53 ++++++++++++++++++++++++++++++-- system/hardware/tici/hardware.py | 2 +- system/sensord/sensord.py | 2 +- 6 files changed, 54 insertions(+), 53 deletions(-) delete mode 100644 common/util.py diff --git a/cereal/messaging/__init__.py b/cereal/messaging/__init__.py index 0ad846f0f..d5033cd63 100644 --- a/cereal/messaging/__init__.py +++ b/cereal/messaging/__init__.py @@ -13,7 +13,7 @@ from typing import Optional, List, Union, Dict from cereal import log from cereal.services import SERVICE_LIST -from openpilot.common.util import MovingAverage +from openpilot.common.utils import MovingAverage NO_TRAVERSAL_LIMIT = 2**64-1 diff --git a/common/realtime.py b/common/realtime.py index 57926b4c4..0b1468102 100644 --- a/common/realtime.py +++ b/common/realtime.py @@ -6,7 +6,7 @@ import time from setproctitle import getproctitle -from openpilot.common.util import MovingAverage +from openpilot.common.utils import MovingAverage from openpilot.system.hardware import PC diff --git a/common/util.py b/common/util.py deleted file mode 100644 index e6ddb46e7..000000000 --- a/common/util.py +++ /dev/null @@ -1,46 +0,0 @@ -import os -import subprocess - -def sudo_write(val: str, path: str) -> None: - try: - with open(path, 'w') as f: - f.write(str(val)) - except PermissionError: - os.system(f"sudo chmod a+w {path}") - try: - with open(path, 'w') as f: - f.write(str(val)) - except PermissionError: - # fallback for debugfs files - os.system(f"sudo su -c 'echo {val} > {path}'") - -def sudo_read(path: str) -> str: - try: - return subprocess.check_output(f"sudo cat {path}", shell=True, encoding='utf8').strip() - except Exception: - return "" - -class MovingAverage: - def __init__(self, window_size: int): - self.window_size: int = window_size - self.buffer: list[float] = [0.0] * window_size - self.index: int = 0 - self.count: int = 0 - self.sum: float = 0.0 - - def add_value(self, new_value: float): - # Update the sum: subtract the value being replaced and add the new value - self.sum -= self.buffer[self.index] - self.buffer[self.index] = new_value - self.sum += new_value - - # Update the index in a circular manner - self.index = (self.index + 1) % self.window_size - - # Track the number of added values (for partial windows) - self.count = min(self.count + 1, self.window_size) - - def get_average(self) -> float: - if self.count == 0: - return float('nan') - return self.sum / self.count diff --git a/common/utils.py b/common/utils.py index 71b29a0c4..caa9a5795 100644 --- a/common/utils.py +++ b/common/utils.py @@ -7,14 +7,61 @@ import time import functools from subprocess import Popen, PIPE, TimeoutExpired import zstandard as zstd -from openpilot.common.swaglog import cloudlog LOG_COMPRESSION_LEVEL = 10 # little benefit up to level 15. level ~17 is a small step change +def sudo_write(val: str, path: str) -> None: + try: + with open(path, 'w') as f: + f.write(str(val)) + except PermissionError: + os.system(f"sudo chmod a+w {path}") + try: + with open(path, 'w') as f: + f.write(str(val)) + except PermissionError: + # fallback for debugfs files + os.system(f"sudo su -c 'echo {val} > {path}'") + + +def sudo_read(path: str) -> str: + try: + return subprocess.check_output(f"sudo cat {path}", shell=True, encoding='utf8').strip() + except Exception: + return "" + + +class MovingAverage: + def __init__(self, window_size: int): + self.window_size: int = window_size + self.buffer: list[float] = [0.0] * window_size + self.index: int = 0 + self.count: int = 0 + self.sum: float = 0.0 + + def add_value(self, new_value: float): + # Update the sum: subtract the value being replaced and add the new value + self.sum -= self.buffer[self.index] + self.buffer[self.index] = new_value + self.sum += new_value + + # Update the index in a circular manner + self.index = (self.index + 1) % self.window_size + + # Track the number of added values (for partial windows) + self.count = min(self.count + 1, self.window_size) + + def get_average(self) -> float: + if self.count == 0: + return float('nan') + return self.sum / self.count + + class CallbackReader: """Wraps a file, but overrides the read method to also call a callback function with the number of bytes read so far.""" + def __init__(self, f, callback, *args): self.f = f self.callback = callback @@ -107,11 +154,11 @@ def retry(attempts=3, delay=1.0, ignore_failure=False): try: return func(*args, **kwargs) except Exception: - cloudlog.exception(f"{func.__name__} failed, trying again") + print(f"{func.__name__} failed, trying again") time.sleep(delay) if ignore_failure: - cloudlog.error(f"{func.__name__} failed after retry") + print(f"{func.__name__} failed after retry") else: raise Exception(f"{func.__name__} failed after retry") return wrapper diff --git a/system/hardware/tici/hardware.py b/system/hardware/tici/hardware.py index 3cdb33608..5a84afce0 100644 --- a/system/hardware/tici/hardware.py +++ b/system/hardware/tici/hardware.py @@ -8,7 +8,7 @@ from functools import cached_property, lru_cache from pathlib import Path from cereal import log -from openpilot.common.util import sudo_read, sudo_write +from openpilot.common.utils import sudo_read, sudo_write from openpilot.common.gpio import gpio_set, gpio_init, get_irqs_for_action from openpilot.system.hardware.base import HardwareBase, LPABase, ThermalConfig, ThermalZone from openpilot.system.hardware.tici import iwlist diff --git a/system/sensord/sensord.py b/system/sensord/sensord.py index cc0366881..62908c6f1 100755 --- a/system/sensord/sensord.py +++ b/system/sensord/sensord.py @@ -7,7 +7,7 @@ import threading import cereal.messaging as messaging from cereal.services import SERVICE_LIST -from openpilot.common.util import sudo_write +from openpilot.common.utils import sudo_write from openpilot.common.realtime import config_realtime_process, Ratekeeper from openpilot.common.swaglog import cloudlog from openpilot.common.gpio import gpiochip_get_ro_value_fd, gpioevent_data