2022-05-24 21:33:59 +02:00
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import uuid
|
|
|
|
|
|
2023-07-18 23:56:24 +02:00
|
|
|
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.common.params import Params
|
2024-04-01 20:43:22 -04:00
|
|
|
from openpilot.system.hardware import PC
|
2024-02-08 16:38:41 -05:00
|
|
|
from openpilot.system.hardware.hw import Paths
|
|
|
|
|
from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT
|
2022-05-24 21:33:59 +02:00
|
|
|
|
2023-09-05 18:52:40 -07:00
|
|
|
class OpenpilotPrefix:
|
2025-07-31 23:42:02 -07:00
|
|
|
def __init__(self, prefix: str = None, create_dirs_on_enter: bool = True, clean_dirs_on_exit: bool = True, shared_download_cache: bool = False):
|
2023-09-07 11:32:47 -07:00
|
|
|
self.prefix = prefix if prefix else str(uuid.uuid4().hex[0:15])
|
2024-12-04 19:51:09 +01:00
|
|
|
self.msgq_path = os.path.join(Paths.shm_path(), self.prefix)
|
2025-07-31 23:42:02 -07:00
|
|
|
self.create_dirs_on_enter = create_dirs_on_enter
|
2023-07-18 23:56:24 +02:00
|
|
|
self.clean_dirs_on_exit = clean_dirs_on_exit
|
2024-01-22 16:30:59 -08:00
|
|
|
self.shared_download_cache = shared_download_cache
|
2022-05-24 21:33:59 +02:00
|
|
|
|
|
|
|
|
def __enter__(self):
|
2023-10-23 20:41:19 -04:00
|
|
|
self.original_prefix = os.environ.get('OPENPILOT_PREFIX', None)
|
2022-05-24 21:33:59 +02:00
|
|
|
os.environ['OPENPILOT_PREFIX'] = self.prefix
|
2025-07-31 23:42:02 -07:00
|
|
|
|
|
|
|
|
if self.create_dirs_on_enter:
|
|
|
|
|
self.create_dirs()
|
2022-05-24 21:33:59 +02:00
|
|
|
|
2024-01-22 16:30:59 -08:00
|
|
|
if self.shared_download_cache:
|
|
|
|
|
os.environ["COMMA_CACHE"] = DEFAULT_DOWNLOAD_CACHE_ROOT
|
|
|
|
|
|
2023-08-01 01:30:58 +02:00
|
|
|
return self
|
|
|
|
|
|
2022-05-24 21:33:59 +02:00
|
|
|
def __exit__(self, exc_type, exc_obj, exc_tb):
|
2023-07-18 23:56:24 +02:00
|
|
|
if self.clean_dirs_on_exit:
|
|
|
|
|
self.clean_dirs()
|
2023-09-05 18:52:40 -07:00
|
|
|
try:
|
|
|
|
|
del os.environ['OPENPILOT_PREFIX']
|
2023-10-23 20:41:19 -04:00
|
|
|
if self.original_prefix is not None:
|
|
|
|
|
os.environ['OPENPILOT_PREFIX'] = self.original_prefix
|
2023-09-05 18:52:40 -07:00
|
|
|
except KeyError:
|
|
|
|
|
pass
|
2023-07-18 23:56:24 +02:00
|
|
|
return False
|
2023-07-20 21:56:57 -07:00
|
|
|
|
2025-07-31 23:42:02 -07:00
|
|
|
def create_dirs(self):
|
|
|
|
|
try:
|
|
|
|
|
os.mkdir(self.msgq_path)
|
|
|
|
|
except FileExistsError:
|
|
|
|
|
pass
|
|
|
|
|
os.makedirs(Paths.log_root(), exist_ok=True)
|
|
|
|
|
|
2023-07-18 23:56:24 +02:00
|
|
|
def clean_dirs(self):
|
2022-05-24 21:33:59 +02:00
|
|
|
symlink_path = Params().get_param_path()
|
|
|
|
|
if os.path.exists(symlink_path):
|
|
|
|
|
shutil.rmtree(os.path.realpath(symlink_path), ignore_errors=True)
|
|
|
|
|
os.remove(symlink_path)
|
|
|
|
|
shutil.rmtree(self.msgq_path, ignore_errors=True)
|
2024-04-01 20:43:22 -04:00
|
|
|
if PC:
|
|
|
|
|
shutil.rmtree(Paths.log_root(), ignore_errors=True)
|
2024-01-22 16:30:59 -08:00
|
|
|
if not os.environ.get("COMMA_CACHE", False):
|
|
|
|
|
shutil.rmtree(Paths.download_cache_root(), ignore_errors=True)
|
2023-09-08 14:54:15 -07:00
|
|
|
shutil.rmtree(Paths.comma_home(), ignore_errors=True)
|