File size: 1,163 Bytes
3d46076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Resource path resolution for source runs AND frozen bundles (PyInstaller).

Read-only resources (datasets, shaders, UI assets, manifests) ship inside the
bundle; user/scientific data (diagnostics/, checkpoints, models) stays relative
to the working directory so a portable build never writes into its own program
files.
"""
import os
import sys


def is_frozen() -> bool:
    return getattr(sys, "frozen", False)


def base_dir() -> str:
    """Directory containing bundled read-only resources."""
    if is_frozen():
        # PyInstaller onefile: extracted bundle root
        meipass = getattr(sys, "_MEIPASS", None)
        if meipass:
            return meipass
        return os.path.dirname(sys.executable)
    # source run: project root (two levels above this file)
    return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def resource(rel: str) -> str:
    """Absolute path to a bundled read-only resource."""
    return os.path.join(base_dir(), *rel.split("/"))


def data_dir(rel: str = "") -> str:
    """Absolute path for user/scientific data (relative to CWD)."""
    return os.path.join(os.getcwd(), rel) if rel else os.getcwd()