-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-test.py~
43 lines (35 loc) · 1.59 KB
/
cmd-test.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
import os, subprocess, sys
def main(argv):
run(argv)
def run(arg):
"""
Runs the given arg at the command line using the default shell. Outputs
when commands are run successfully.
Based on http://developer.spikesource.com/wiki/index.php/How_to_invoke_subprocesses_from_Python
@param Tuple args
A tuple of args, with the first being the command to be run, and
the remaining ones flags and arguments for the command. STDOUT and
STDERR are piped to tuple, waiting until the output is finished,
then writing both to the log file, if not empty.
Ex. ['apt-get', '-y', 'install', 'dnsmasq'], which installs
dnsmasq using apt-get, and assumes yes to questions.
"""
# Open output and write some info about the command to be written, including
# name of command and arguments.
with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
outFile.write("Command: ")
for a in arg:
outFile.write(a,)
outFile.write(" ")
outFile.write("\n")
# Open output and error log file and append to them the output of the commands
with open(os.path.join(os.curdir, "output.log"), 'a') as outFile:
with open(os.path.join(os.curdir, "error.log"), 'a') as errorFile:
# Call the subprocess using convenience method
retval = subprocess.call(arg, -1, None, None, outFile, errorFile)
# Check the process exit code, print error information if it exists
if not retval == 0:
errData = errorFile.read()
raise Exception("Error executing command: " + repr(errData))
if __name__=="__main__":
main(sys.argv[1:])