-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipetool.py
87 lines (68 loc) · 2.04 KB
/
pipetool.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3
"""
File:
- pipetool.py
Authors:
- Jacob Mills,
- Brandon Everhart,
- Taylor Shields
Date: 11/24/2018
Description:
- Utility to execute external processes inside of a subprocess
with the ability to specify stdin and stdout.
Changelog:
- 11/24 Documented
- 11/24 Cleaned formatting based on PyCharm, PyLint3, PEP8
- 11/24 Pylint score 1.00/10 --> 10.00/10
"""
import subprocess
def docs():
"""
Function:
pipetool.docs()
Description:
Prints all docstrings related to this file.
Parameters:
- None
Return:
- None
"""
print(__doc__)
print(docs.__doc__)
print(exec_quiet.__doc__)
def exec_quiet(cmd, stdin_filename=None):
"""
Function:
pipetool.exec_quiet()
Description:
- Executes supplied commands in a subprocess and retrieves the
stdout of that subprocess for our return value.
-If an input file is specified, open the input file and supply
it as stdin to the subprocess.
Parameters:
- cmd:
Description - process to execute with its arguments,
Data Type - list,
Requirement - mandatory,
Argument Type - Positional (1st)
-stdin_filename:
Description - specified file path for stdin,
Data Type - string,
Requirement - optional(default=None),
Argument Type - Positional (2nd)
Return:
- output:
Description - stdout of the executed process
Data Type - string
"""
output = ''
if stdin_filename:
pipe_in = open(stdin_filename, 'rb')
proc1 = subprocess.Popen(cmd, stdin=pipe_in, stdout=subprocess.PIPE)
output = proc1.stdout.read().decode('utf-8')
else:
proc1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc1.stdout.read().decode('utf-8')
return output
if __name__ == "__main__":
docs()