1
|
|
|
"""Utilities to call shell programs.""" |
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import subprocess |
5
|
1 |
|
import logging |
6
|
1 |
|
import shutil |
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, _ignore=False, _shell=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 _ignore: ignore non-zero return codes |
24
|
|
|
:param _shell: force executing the program into a real shell |
25
|
|
|
a windows shell command (i.e : dir, echo) needs a real shell |
26
|
|
|
but not a regular program (i.e : calc, git) |
27
|
|
|
""" |
28
|
1 |
|
program = CMD_PREFIX + ' '.join([name, *args]) |
29
|
1 |
|
if _show: |
30
|
1 |
|
common.show(program, color='shell') |
31
|
|
|
else: |
32
|
1 |
|
log.debug(program) |
33
|
|
|
|
34
|
1 |
|
if name == 'cd': |
35
|
1 |
|
return os.chdir(args[0]) # 'cd' has no effect in a subprocess |
36
|
|
|
|
37
|
1 |
|
command = subprocess.run( |
|
|
|
|
38
|
|
|
[name, *args], universal_newlines=True, |
39
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
40
|
|
|
shell=_shell |
41
|
|
|
) |
42
|
|
|
|
43
|
1 |
|
for line in command.stdout.splitlines(): |
44
|
1 |
|
log.debug(OUT_PREFIX + line.strip()) |
45
|
|
|
|
46
|
1 |
|
if command.returncode == 0: |
47
|
1 |
|
return command.stdout.strip() |
48
|
|
|
|
49
|
1 |
|
elif _ignore: |
50
|
1 |
|
log.debug("Ignored error from call to '%s'", program) |
51
|
|
|
|
52
|
|
|
else: |
53
|
1 |
|
message = ( |
54
|
|
|
"An external program call failed." + "\n\n" |
55
|
|
|
"In working directory: " + os.getcwd() + "\n\n" |
56
|
|
|
"The following command produced a non-zero return code:" + "\n\n" + |
57
|
|
|
program + "\n" + |
58
|
|
|
command.stdout |
59
|
|
|
) |
60
|
1 |
|
raise ShellError(message) |
61
|
|
|
|
62
|
|
|
|
63
|
1 |
|
def mkdir(path): |
64
|
1 |
|
if not os.path.exists(path): |
65
|
1 |
|
os.makedirs(path) |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
def cd(path, _show=True): |
69
|
1 |
|
call('cd', path, _show=_show) |
70
|
|
|
|
71
|
|
|
|
72
|
1 |
|
def ln(source, target): |
73
|
1 |
|
if not os.name == 'nt': |
74
|
1 |
|
dirpath = os.path.dirname(target) |
75
|
1 |
|
if not os.path.isdir(dirpath): |
76
|
1 |
|
mkdir(dirpath) |
77
|
1 |
|
call('ln', '-s', source, target) |
78
|
|
|
else: |
79
|
|
|
log.debug("symlinks are not supported on windows system") |
80
|
|
|
|
81
|
|
|
|
82
|
1 |
|
def rm(path): |
83
|
1 |
|
if os.path.exists(path): |
84
|
1 |
|
if not os.path.isdir(path): |
85
|
1 |
|
os.remove(path) |
86
|
|
|
else: |
87
|
|
|
shutil.rmtree(path) |
88
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.