aboutsummaryrefslogtreecommitdiff
path: root/src/util.py
blob: 3b4de590c852454c6f2189aea5be4a4cdc324fd9 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import subprocess
import sys

import src.globalvars


def is_exe(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)


def find_cmd(cmd):
    fpath, fname = os.path.split(cmd)

    # Search for file
    if fpath:
        if is_exe(cmd):
            return cmd
    # Search in PATH
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, cmd)
            if is_exe(exe_file):
                return exe_file

    return None


def prefix_cmd_with_abspath(cmd):
    """Prefix cmd with the abspath of the first word

    Usefull if cmd should be executed by the loader of a custom glibc."""

    if os.path.isabs(cmd) or cmd == "":
        return cmd

    binary_end = cmd.find(" ")
    binary_end = None if binary_end == -1 else binary_end

    binary_abspath = subprocess.run(["whereis", cmd[0:binary_end]],
                                    stdout=subprocess.PIPE,
                                    universal_newlines=True).stdout.split()[1]

    return binary_abspath + " " + cmd[binary_end:]


def allocbench_msg(color, *objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 0:
        return

    color = {"YELLOW": "\x1b[33m",
             "GREEN": "\x1b[32m",
             "RED": "\x1b[31m"}[color]

    is_atty = sys.stdout.isatty()
    if is_atty:
        print(color, end="", file=file, flush=True)

    print(*objects, sep=sep, end=end, file=file)

    if is_atty:
        print("\x1b[0m", end="", file=file, flush=True)


def print_debug(*objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 3:
        return
    print(*objects, sep=sep, end=end, file=file)


def print_info(*objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 1:
        return
    print(*objects, sep=sep, end=end, file=file)


def print_info0(*objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 0:
        return
    print(*objects, sep=sep, end=end, file=file)


def print_info2(*objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 2:
        return
    print(*objects, sep=sep, end=end, file=file)


def print_status(*objects, sep=' ', end='\n', file=sys.stdout):
    allocbench_msg("GREEN", *objects, sep=sep, end=end, file=file)


def print_warn(*objects, sep=' ', end='\n', file=sys.stdout):
    if src.globalvars.verbosity < 1:
        return
    allocbench_msg("YELLOW", *objects, sep=sep, end=end, file=file)


def print_error(*objects, sep=' ', end='\n', file=sys.stderr):
    allocbench_msg("RED", *objects, sep=sep, end=end, file=file)