Passed
Pull Request — develop (#201)
by
unknown
01:50
created

gitman.shell.pwd()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""Utilities to call shell programs."""
2
3 1
import logging
4 1
import os
5 1
import subprocess
6
7 1
from . import common
8 1
from .exceptions import ShellError
9
10 1
11 1
CMD_PREFIX = "$ "
12
OUT_PREFIX = "> "
13 1
14
log = logging.getLogger(__name__)
15
16 1
17
def call(name, *args, _show=True, _shell=False, _ignore=False):
18
    """Call a program with arguments.
19
20
    :param name: name of program to call
21
    :param args: list of command-line arguments
22
    :param _show: display the call on stdout
23
    :param _shell: force executing the program into a real shell
24
                   a Windows shell command (i.e: dir, echo) needs a real shell
25
                   but not a regular program (i.e: calc, git)
26
    :param _ignore: ignore non-zero return codes
27 1
    """
28
    program = show(name, *args, stdout=_show)
29 1
30
    command = subprocess.run(
31
        name if _shell else [name, *args],
32
        universal_newlines=True,
33
        stdout=subprocess.PIPE,
34
        stderr=subprocess.STDOUT,
35 1
        shell=_shell,
36 1
    )
37 1
38
    output = [line.strip() for line in command.stdout.splitlines()]
39 1
    for line in output:
40 1
        log.debug(OUT_PREFIX + line)
41
42 1
    if command.returncode == 0:
43 1
        return output
44 1
45
    if _ignore:
46
        log.debug("Ignored error from call to '%s'", name)
47 1
        return output
48
49
    message = (
50
        "An external program call failed." + "\n\n"
51
        "In working directory: " + os.getcwd() + "\n\n"
52
        "The following command produced a non-zero return code:"
53
        + "\n\n"
54 1
        + CMD_PREFIX
55
        + program
56
        + "\n"
57 1
        + command.stdout.strip()
58 1
    )
59 1
    raise ShellError(message, program=program, output=output)
60
61
62 1
def mkdir(path):
63
    if not os.path.exists(path):
64
        if os.name == 'nt':
65 1
            call("mkdir " + path, _shell=True)
66 1
        else:
67
            call('mkdir', '-p', path)
68
69 1
70 1
def cd(path, _show=True):
71
    if os.name == 'nt':
72
        show('cd', '/D', path, stdout=_show)
73 1
    else:
74 1
        show('cd', path, stdout=_show)
75
    os.chdir(path)
76
77 1
78 1
def pwd(_show=True):
79 1
    cwd = os.getcwd()
80 1
    if os.name == 'nt':
81
        cwd = cwd.replace(os.sep, '/')
82
    show('cwd', cwd, stdout=_show)
83 1
    return cwd
84 1
85
86
def ln(source, target):
87
    dirpath = os.path.dirname(target)
88
    if not os.path.isdir(dirpath):
89
        mkdir(dirpath)
90 1
    os.symlink(source, target)
91
92
93 1
def rm(path):
94 1
    if os.name == 'nt':
95 1
        if os.path.isfile(path):
96 1
            call("del /Q /F " + path, _shell=True)
97
        elif os.path.isdir(path):
98 1
            call("rmdir /Q /S " + path, _shell=True)
99 1
    else:
100
        call('rm', '-rf', path)
101
102
103
def show(name, *args, stdout=True):
104
    program = ' '.join([name, *args])
105
    if stdout:
106
        common.show(CMD_PREFIX + program, color='shell')
107
    else:
108
        log.debug(CMD_PREFIX + program)
109
    return program
110