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


def sh(prog, *args):
    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):
    return sh('git', *args)


def getenv(s):
    """ Convert string of key pairs to dictionary format
    """
    return dict([x.split('=', 1) for x in s.splitlines()])


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