File: database.py

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (63 lines) | stat: -rw-r--r-- 1,694 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Database and Database Path Management
"""
import os
from os import path
import json
import subprocess


def get_trellis_root():
    """Return the absolute path to the Project Trellis repo root"""
    return path.abspath(path.join(__file__, "../../../"))


def get_db_root():
    """
    Return the path containing the Project Trellis database
    This is database/ in the repo, unless the `PRJTRELLIS_DB` environment
    variable is set to another value.
    """
    if "PRJTRELLIS_DB" in os.environ and os.environ["PRJTRELLIS_DB"] != "":
        return os.environ["PRJTRELLIS_DB"]
    else:
        return path.join(get_trellis_root(), "database")


def get_db_subdir(family = None, device = None, package = None):
    """
    Return the DB subdirectory corresponding to a family, device and
    package (all if applicable), creating it if it doesn't already
    exist.
    """
    subdir = get_db_root()
    dparts = [family, device, package]
    for dpart in dparts:
        if dpart is None:
            break
        subdir = path.join(subdir, dpart)
        if not path.exists(subdir):
            os.mkdir(subdir)
    return subdir


def get_tilegrid(family, device):
    """
    Return the deserialised tilegrid for a family, device
    """
    tgjson = path.join(get_db_subdir(family, device), "tilegrid.json")
    with open(tgjson, "r") as f:
        return json.load(f)


def get_devices():
    """
    Return the deserialised content of devices.json
    """
    djson = path.join(get_db_root(), "devices.json")
    with open(djson, "r") as f:
        return json.load(f)


def get_db_commit():
    return subprocess.getoutput('git -C "{}" rev-parse HEAD'.format(get_db_root()))