aboutsummaryrefslogtreecommitdiff
path: root/src/common.py
blob: f1a64bf3570022e6923232d9da4d8b958da8771e (plain)
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
import yaml
import sys
import os
import subprocess
import questionary
import os
import shutil
import requests 
import xmltodict
from difflib import SequenceMatcher
from typing import TypedDict

REAL_REPO = ""
REPOS_LOCATION = "/var/db/repos"
REPO_LOC = ""
ROOT = "sudo"

try:
    with open(os.path.expanduser("~/.config/gpo-steal/config.yaml"), "r") as f:
        cfg = yaml.safe_load(f.read())
        REAL_REPO = cfg["repo"]
        REPO_LOC = cfg["repo_location"]
        ROOT = cfg["elevation"]
except FileNotFoundError:
    print("no configuration found, should be in ~/.config/gpo-steal/config.yaml")
    exit(1)

FULL_REPOS_LOCATION = os.path.join(REPOS_LOCATION, REAL_REPO)
REPO_CFG_LOC = os.path.join(REPO_LOC, "repos.yaml")
TRACKED_CFG_LOC = os.path.join(REPO_LOC, "tracked.yaml")

os.system(f"mkdir -p {REPO_LOC}")

class repo_pair(TypedDict):
    url: str
    name: str

repos = {}
tracked = {}

try:
    with open(REPO_CFG_LOC, "r") as f:
        repos = yaml.safe_load(f.read()) or repos
except:
    pass

try:
    with open(TRACKED_CFG_LOC, "r") as f:
        tracked = yaml.safe_load(f.read()) or tracked
except:
    pass

def save() -> None:
    with open(REPO_CFG_LOC, "w") as f:
        yaml.dump(repos, f)

    with open(TRACKED_CFG_LOC, "w") as f:
        yaml.dump(tracked, f)

def eexec(cmd) -> None:
    run = f"{ROOT} sh -c '{cmd}'"
    print(run)
    os.system(run)