Created
November 25, 2013 10:34
-
-
Save olegchir/7639418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_script(script, stdin=None): | |
"""Returns (stdout, stderr), raises error on non-zero return code""" | |
import subprocess | |
# Note: by using a list here (['bash', ...]) you avoid quoting issues, as the | |
# arguments are passed in exactly this order (spaces, quotes, and newlines won't | |
# cause problems): | |
proc = subprocess.Popen(['bash', '-c', script], | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
stdin=subprocess.PIPE) | |
stdout, stderr = proc.communicate() | |
if proc.returncode: | |
raise ScriptException(proc.returncode, stdout, stderr, script) | |
return stdout, stderr | |
class ScriptException(Exception): | |
def __init__(self, returncode, stdout, stderr, script): | |
self.returncode = returncode | |
self.stdout = stdout | |
self.stderr = stderr | |
Exception.__init__('Error in script') | |
run_script("/opt/local/bin/mc") | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment