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( |
|
|
|
|
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
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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
|
|
|
|
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.