Passed
Pull Request — develop (#201)
by
unknown
04:51
created

gitman.shell.pwd()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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
    show('cwd', cwd, stdout=_show)
81
    return cwd
82
83 1
84 1
def ln(source, target):
85
    dirpath = os.path.dirname(target)
86
    if not os.path.isdir(dirpath):
87
        mkdir(dirpath)
88
    os.symlink(source, target)
89
90 1
91
def rm(path):
92
    if os.name == 'nt':
93 1
        if os.path.isfile(path):
94 1
            call("del /Q /F " + path, _shell=True)
95 1
        elif os.path.isdir(path):
96 1
            call("rmdir /Q /S " + path, _shell=True)
97
    else:
98 1
        call('rm', '-rf', path)
99 1
100
101
def show(name, *args, stdout=True):
102
    program = ' '.join([name, *args])
103
    if stdout:
104
        common.show(CMD_PREFIX + program, color='shell')
105
    else:
106
        log.debug(CMD_PREFIX + program)
107
    return program
108