Completed
Push — develop ( 7e574a...8a3090 )
by Jace
02:50
created

rm()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.9849

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 4
c 4
b 0
f 1
dl 0
loc 8
ccs 3
cts 7
cp 0.4286
crap 6.9849
rs 9.2
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__)
0 ignored issues
show
Coding Style Naming introduced by
The name log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style Naming introduced by
The name cd does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
64 1
    if os.name == 'nt':
65
        show('cd', '/D', path, stdout=_show)
66
    else:
67 1
        show('cd', path, stdout=_show)
68 1
    os.chdir(path)
69
70
71 1
def ln(source, target):
0 ignored issues
show
Coding Style Naming introduced by
The name ln does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
72 1
    if os.name == 'nt':
73
        log.warning("Symlinks are not supported on Windows")
74
    else:
75 1
        dirpath = os.path.dirname(target)
76 1
        if not os.path.isdir(dirpath):
77 1
            mkdir(dirpath)
78 1
        call('ln', '-s', source, target)
79
80
81 1
def rm(path):
0 ignored issues
show
Coding Style Naming introduced by
The name rm does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
82 1
    if os.name == 'nt':
83
        if os.path.isfile(path):
84
            call('del', '/Q', '/F', path, _shell=True)
85
        elif os.path.isdir(path):
86
            call('rmdir', '/Q', '/S', path, _shell=True)
87
    else:
88 1
        call('rm', '-rf', path)
89
90
91 1
def show(name, *args, stdout=True):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
92 1
    program = CMD_PREFIX + ' '.join([name, *args])
93 1
    if stdout:
94 1
        common.show(program, color='shell')
95
    else:
96 1
        log.debug(program)
97
    return program
98