You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1021 B
39 lines
1021 B
3 years ago
|
import os
|
||
|
import shutil
|
||
|
import uuid
|
||
|
|
||
2 years ago
|
from typing import Optional
|
||
2 years ago
|
|
||
2 years ago
|
from openpilot.common.params import Params
|
||
3 years ago
|
|
||
2 years ago
|
class OpenpilotPrefix:
|
||
2 years ago
|
def __init__(self, prefix: Optional[str] = None, clean_dirs_on_exit: bool = True):
|
||
3 years ago
|
self.prefix = prefix if prefix else str(uuid.uuid4())
|
||
|
self.msgq_path = os.path.join('/dev/shm', self.prefix)
|
||
2 years ago
|
self.clean_dirs_on_exit = clean_dirs_on_exit
|
||
3 years ago
|
|
||
|
def __enter__(self):
|
||
|
os.environ['OPENPILOT_PREFIX'] = self.prefix
|
||
|
try:
|
||
|
os.mkdir(self.msgq_path)
|
||
|
except FileExistsError:
|
||
|
pass
|
||
|
|
||
2 years ago
|
return self
|
||
|
|
||
3 years ago
|
def __exit__(self, exc_type, exc_obj, exc_tb):
|
||
2 years ago
|
if self.clean_dirs_on_exit:
|
||
|
self.clean_dirs()
|
||
2 years ago
|
try:
|
||
|
del os.environ['OPENPILOT_PREFIX']
|
||
|
except KeyError:
|
||
|
pass
|
||
2 years ago
|
return False
|
||
2 years ago
|
|
||
2 years ago
|
def clean_dirs(self):
|
||
3 years ago
|
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)
|