aboutsummaryrefslogtreecommitdiff
path: root/src/common.py
blob: 416d83637cf007dee09bed2447bca4ecfc066264 (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
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 = ""

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"]
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 = {}

root = ""
if not shutil.which("sudo") is None:
    root = "sudo"
if not shutil.which("doas") is None:
    root = "doas"
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)