aboutsummaryrefslogtreecommitdiff
"""Where things live: the hardware repo root and KiCad's stock libraries.

ROOT is the hardware repo the tools work on, the directory that holds libraries/ (your HW
symbol, footprint and 3D model libraries). It is HW_ROOT if set, else the nearest directory
at or above the current one that has a libraries/ folder, else the parent of tools/.

KiCad's own environment variables win (KICAD10_SYMBOL_DIR, KICAD10_FOOTPRINT_DIR,
KICAD10_3DMODEL_DIR), then KICAD_SHARE (the directory that holds symbols/, footprints/,
3dmodels/), then the usual install locations on macOS, Linux and Windows.

    from kicad_paths import ROOT, SYMBOLS, FOOTPRINTS, MODELS3D
"""
import os


def repo_root():
    env = os.environ.get("HW_ROOT")
    if env:
        return os.path.abspath(env)
    d = os.getcwd()
    while True:
        if os.path.isdir(os.path.join(d, "libraries")):
            return d
        up = os.path.dirname(d)
        if up == d:
            break
        d = up
    return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


ROOT = repo_root()

_CANDIDATES = [
    "/Applications/KiCad/KiCad.app/Contents/SharedSupport",   # macOS
    "/usr/share/kicad",                                       # Linux packages
    "/usr/local/share/kicad",
    "/app/share/kicad",                                       # Flatpak
    r"C:\Program Files\KiCad\10.0\share\kicad",               # Windows
]


def share_dir():
    env = os.environ.get("KICAD_SHARE")
    if env:
        return env
    for c in _CANDIDATES:
        if os.path.isdir(os.path.join(c, "footprints")) or os.path.isdir(os.path.join(c, "symbols")):
            return c
    return _CANDIDATES[0]


SHARE = share_dir()
SYMBOLS = os.environ.get("KICAD10_SYMBOL_DIR") or os.path.join(SHARE, "symbols")
FOOTPRINTS = os.environ.get("KICAD10_FOOTPRINT_DIR") or os.path.join(SHARE, "footprints")
MODELS3D = os.environ.get("KICAD10_3DMODEL_DIR") or os.path.join(SHARE, "3dmodels")