1
|
|
|
"""Utilities to call shell programs.""" |
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import logging |
5
|
|
|
|
6
|
1 |
|
from sh import Command, ErrorReturnCode |
|
|
|
|
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): |
|
|
|
|
46
|
1 |
|
call('mkdir', '-p', path) |
47
|
|
|
|
48
|
|
|
|
49
|
1 |
|
def cd(path, _show=True): |
|
|
|
|
50
|
1 |
|
call('cd', path, _show=_show) |
51
|
|
|
|
52
|
|
|
|
53
|
1 |
|
def ln(source, target): |
|
|
|
|
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): |
|
|
|
|
61
|
|
|
call('rm', '-rf', path) |
62
|
|
|
|
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.
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.