-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshells.py
70 lines (60 loc) · 2.23 KB
/
shells.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Python standard library
from __future__ import print_function
from subprocess import CalledProcessError
import os, subprocess
# Local imports
from utils import fatal, err
def set_options(strict):
"""
Changes behavior of default shell and get overrides options
to run bash in a strict mode.
@param strict <bool>:
Overrides default shell options and runs shell in strict or
less permissive mode.
@return prefix <int>:
Returns overrides options to run bash in a strict mode
"""
prefix = '' # permissive shell option
if strict:
# Changes behavior of default shell
# set -e: exit immediately upon error
# set -u: treats unset variables as an error
# set -o pipefail: exits if a error occurs in any point of a pipeline
prefix = 'set -euo pipefail; '
return prefix
def bash(cmd, interpreter='/bin/bash', strict=set_options(True), cwd=os.getcwd(), **kwargs):
"""
Interface to run a process or bash command. Using subprocess.call_check()
due to portability across most python versions. It was introduced in python 2.5
and it is also interoperabie across all python 3 versions.
@param cmd <str>:
Shell command to run
@param interpreter <str>:
Interpreter for command to run [default: bash]
@pararm strict <bool>:
Prefixes any command with 'set -euo pipefail' to ensure process fail with
the expected exit-code
@params kwargs <check_call()>:
Keyword arguments to modify subprocess.check_call() behavior
@return exitcode <int>:
Returns the exit code of the run command, failures return non-zero exit codes
"""
try:
exitcode = subprocess.check_call(strict + cmd,
shell=True,
executable=interpreter,
cwd=cwd,
**kwargs
)
except CalledProcessError as e:
exitcode = e.returncode
err("""WARNING: Failed to run '{}' command!
└── Command returned a non-zero exitcode of '{}'.""".format(strict + cmd, exitcode)
)
return exitcode
if __name__ == '__main__':
# Tests
bash('ls -la /home/')
bash('ls -la /fake/dne/path')