aboutsummaryrefslogtreecommitdiffstats
path: root/util/runners.py
blob: dbfcf1f352ca2abf920a7774f9b4c258882089cb (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
import os
import re
from util.exceptions import StepFailedError
from termcolor import colored

HOME = os.getenv("HOME")
OVERWRITE_FILES = os.getenv("OVERWRITE_FILES")


def run_step(step):
    step_type = step["=="]

    if step_type == "run":
        do_run(step["command"])
    elif step_type == "link":
        do_link(step["from"], step["to"])


def do_run(command):
    print(f"! {command}")
    if not os.system(command) == 0:
        raise StepFailedError("Non-zero return code")


def do_link(source, destination):
    destination = re.sub(r"^~/", HOME + "/", destination)
    print(f"Linking {source} -> {destination}")
    if os.path.exists(destination):
        if OVERWRITE_FILES == "1":
            os.remove(destination)
            print(colored("  (File overwritten)", "yellow"))
        else:
            print(colored("  (File already exists)", "yellow"))
            return

    try:
        os.link(source, destination)
    except OSError as e:
        raise StepFailedError("Link raised exeption: " + str(e)) from e
    except Exception as e:
        raise StepFailedError("Link raised exeption: " + str(e)) from e