Completed
Pull Request — develop (#140)
by
unknown
02:48 queued 01:14
created

mkdir()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 5
Bugs 3 Features 0
Metric Value
cc 3
c 5
b 3
f 0
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 3.072
rs 9.4285
1
"""Utilities to call shell programs."""
2
3 1
import os
4 1
import subprocess
5 1
import logging
6
7 1
from . import common
8 1
from .exceptions import ShellError
9
10 1
CMD_PREFIX = "$ "
11 1
OUT_PREFIX = "> "
12
13 1
log = logging.getLogger(__name__)
14
15
16 1
def call(name, *args, _show=True, _ignore=False, _shell=False):
17
    """Call a program with arguments.
18
19
    :param name: name of program to call
20
    :param args: list of command-line arguments
21
    :param _show: display the call on stdout
22
    :param _ignore: ignore non-zero return codes
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
    """
27 1
    program = show(name, *args, stdout=_show)
28
29 1
    command = subprocess.run(
0 ignored issues
show
Bug introduced by
The Module subprocess does not seem to have a member named run.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
30
        [name, *args], universal_newlines=True,
31
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
32
        shell=_shell
33
    )
34
35 1
    for line in command.stdout.splitlines():
36 1
        log.debug(OUT_PREFIX + line.strip())
37
38 1
    if command.returncode == 0:
39 1
        return command.stdout.strip()
40
41 1
    elif _ignore:
42 1
        log.debug("Ignored error from call to '%s'", name)
43
44
    else:
45 1
        message = (
46
            "An external program call failed." + "\n\n"
47
            "In working directory: " + os.getcwd() + "\n\n"
48
            "The following command produced a non-zero return code:" + "\n\n" +
49
            program + "\n" +
50
            command.stdout
51
        )
52 1
        raise ShellError(message)
53
54
55 1
def mkdir(path):
56 1
    if not os.path.exists(path):
57 1
        if os.name == 'nt':
58
            call('mkdir', path, _shell=True)
59
        else:
60 1
            call('mkdir', '-p', path)
61
62
63 1
def cd(path, _show=True):
64 1
    if os.name == 'nt':
65
        # NOTE : call('cd', '/D', _shell=True) have no effect
66
        show('cd', '/D', path, stdout=_show)
67
    else:
68 1
        show('cd', path, stdout=_show)
69 1
    os.chdir(path)
70
71
72 1
def ln(source, target):
73 1
    if os.name == 'nt':
74
        log.warning("Symlinks are not supported on Windows")
75
    else:
76 1
        dirpath = os.path.dirname(target)
77 1
        if not os.path.isdir(dirpath):
78 1
            mkdir(dirpath)
79 1
        call('ln', '-s', source, target)
80
81
82 1
def rm(path):
83 1
    if os.path.exists(path):
84 1
        if os.name == 'nt':
85
            if os.path.isdir(path):
86
                call(
87
                    'rmdir', '/Q', '/S',
88
                    path, _shell=True
89
                )
90
            else:
91
                call('del', '/Q', '/F', path, _shell=True)
92
        else:
93 1
            call('rm', '-rf', path)
94
95
96 1
def show(name, *args, stdout=True):
97 1
    program = CMD_PREFIX + ' '.join([name, *args])
98 1
    if stdout:
99 1
        common.show(program, color='shell')
100
    else:
101 1
        log.debug(program)
102
    return program
103