gdm.call()   D
last analyzed

Complexity

Conditions 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8
Metric Value
dl 0
loc 26
rs 4
ccs 20
cts 20
cp 1
cc 8
crap 8
1
"""Utilities to call shell programs."""
2
3 1
import os
4 1
import logging
5
6 1
from sh import Command, ErrorReturnCode
0 ignored issues
show
Configuration introduced by
The import sh could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8 1
from . import common
9 1
from .exceptions import ShellError
10
11 1
CMD_PREFIX = "$ "
12 1
OUT_PREFIX = "> "
13
14 1
log = logging.getLogger(__name__)
15
16
17 1
def call(name, *args, _show=True, _capture=False, _ignore=False):
18
    """Call a shell program with arguments."""
19 1
    msg = CMD_PREFIX + ' '.join([name] + list(args))
20 1
    if _show:
21 1
        common.show(msg)
22
    else:
23 1
        log.debug(msg)
24
25 1
    if name == 'cd' and len(args) == 1:
26 1
        return os.chdir(args[0])
27
28 1
    try:
29 1
        program = Command(name)
30 1
        if _capture:
31 1
            line = program(*args).strip()
32 1
            log.debug(OUT_PREFIX + line)
33 1
            return line
34
        else:
35 1
            for line in program(*args, _iter='err'):
36 1
                log.debug(OUT_PREFIX + line.strip())
37 1
    except ErrorReturnCode as exc:
38 1
        msg = "\n  IN: '{}'{}".format(os.getcwd(), exc)
39 1
        if _ignore:
40 1
            log.debug("Ignored error from call to '%s'", name)
41
        else:
42 1
            raise ShellError(msg)
43
44
45 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...
46 1
    call('mkdir', '-p', path)
47
48
49 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...
50 1
    call('cd', path, _show=_show)
51
52
53 1
def ln(source, target):
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...
54 1
    dirpath = os.path.dirname(target)
55 1
    if not os.path.isdir(dirpath):
56 1
        mkdir(dirpath)
57 1
    call('ln', '-s', source, target)
58
59
60 1
def rm(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...
61
    call('rm', '-rf', path)
62