Skip to content

Instantly share code, notes, and snippets.

@jonasbits
Forked from adamv/executil.py
Created May 28, 2014 19:58
Show Gist options
  • Save jonasbits/99ee45aee41e1965c646 to your computer and use it in GitHub Desktop.
Save jonasbits/99ee45aee41e1965c646 to your computer and use it in GitHub Desktop.
import sys
import os
import contextlib
from subprocess import Popen, PIPE
def die(message, error_code=1):
print message
sys.exit(error_code)
def system(command, print_command=False):
"Run a command, returning the exit code and output."
if print_command: print command
p = Popen(command)
return p.returncode
def run(command, print_command=False):
"Run a command, returning the exit code and output."
if print_command: print command
p = Popen(command, stdout=PIPE)
output, errput = p.communicate()
return p.returncode, output
def run2(command, print_command=False):
"Run a command, returning the exit code, output, and stderr."
p = Popen(command, stdout=PIPE, stderr=PIPE)
if print_command: print command
output, errput = p.communicate()
return p.returncode, output, errput
@contextlib.contextmanager
def working_folder(path):
"""Change the local current directory within a with block."""
last_folder = os.getcwd()
os.chdir(path)
yield path
os.chdir(last_folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment