61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
import os
|
|
import time
|
|
import shutil
|
|
import uuid
|
|
|
|
|
|
MANAGER_GROUP = "manager"
|
|
DEVELOPER_GROUP = "developers"
|
|
USER_GROUP = "user"
|
|
ALLOWED_GROUPS = {MANAGER_GROUP, DEVELOPER_GROUP, USER_GROUP}
|
|
|
|
|
|
def normalize_user_group(user_group: str) -> str:
|
|
group = (user_group or USER_GROUP).strip().lower()
|
|
return group if group in ALLOWED_GROUPS else USER_GROUP
|
|
|
|
|
|
def pdk_root_for_group(user_group: str, repo_root: str) -> str:
|
|
group = normalize_user_group(user_group)
|
|
if group == MANAGER_GROUP:
|
|
return os.path.abspath(os.environ.get(
|
|
"MXPIC_PDK_ATLAS_ROOT",
|
|
os.path.join(repo_root, "opt_pdk_atlas", "foundries"),
|
|
))
|
|
return os.path.abspath(os.environ.get(
|
|
"MXPIC_PDK_PUBLIC_ROOT",
|
|
os.path.join(repo_root, "opt_pdk_public", "foundries"),
|
|
))
|
|
|
|
|
|
def pdk_root_for_session(session_obj, repo_root: str) -> str:
|
|
return pdk_root_for_group(session_obj.get("user_group"), repo_root)
|
|
|
|
|
|
def prefer_full_gds_for_session(session_obj) -> bool:
|
|
return normalize_user_group(session_obj.get("user_group")) == MANAGER_GROUP
|
|
|
|
|
|
def create_export_path(export_root: str, project_name: str) -> tuple[str, str, str]:
|
|
export_id = uuid.uuid4().hex
|
|
filename = f"{project_name}.gds"
|
|
export_dir = os.path.abspath(os.path.join(export_root, export_id))
|
|
os.makedirs(export_dir, exist_ok=True)
|
|
return export_id, filename, os.path.join(export_dir, filename)
|
|
|
|
|
|
def cleanup_expired_exports(export_root: str, max_age_seconds: int = 86400) -> None:
|
|
if not os.path.isdir(export_root):
|
|
return
|
|
now = time.time()
|
|
for name in os.listdir(export_root):
|
|
path = os.path.join(export_root, name)
|
|
if not os.path.isdir(path):
|
|
continue
|
|
try:
|
|
age = now - os.path.getmtime(path)
|
|
if age > max_age_seconds:
|
|
shutil.rmtree(path, ignore_errors=True)
|
|
except OSError:
|
|
continue
|