aboutsummaryrefslogtreecommitdiff
path: root/delivery_merge/utils.py
blob: e6ed117821b82aefb5b4356c0da721e9f46c14e7 (plain) (blame)
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
import os
from contextlib import contextmanager
from subprocess import run


def sh(prog, *args):
    """ Execute a program with arguments
    :param prog: str: path to program
    :param args: tuple: variadic arguments
                 Accepts any combination of strings passed as arguments
    :returns: subprocess.CompletedProcess
    """
    command = [prog]
    tmp = []
    for arg in args:
        tmp += arg.split()

    command += tmp
    print(f'Running: {" ".join(command)}')
    return run(command, capture_output=True)


def git(*args):
    """ Execute git commands
    :param args: tuple: variadic arguments to pass to git
    :returns: subprocess.CompletedProcess
    """
    return sh('git', *args)


def getenv(s):
    """ Convert string of key pairs to dictionary format
    :param s: str: key pairs separated by newlines
    :returns: dict: converted key pairs
    """
    return dict([x.split('=', 1) for x in s.splitlines() if x])


@contextmanager
def pushd(path):
    """ Equivalent to shell pushd/popd behavior
    :param path: str: path to directory
    """
    last = os.path.abspath(os.getcwd())
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(last)