]> scripts.mit.edu Git - wizard.git/blob - lib/wizard/shell.py
Implement migration script, update TODO with new things to do.
[wizard.git] / lib / wizard / shell.py
1 import subprocess
2 from subprocess import CalledProcessError
3 import sys
4
5 class Shell(object):
6     """An advanced shell, with the ability to do dry-run and log commands"""
7     def __init__(self, verbose = False, dry = False):
8         """ `verbose`   Whether or not to print the command and outputs
9             `dry`       Whether or not to not run any commands, and just print"""
10         self.verbose = verbose
11         self.dry = dry
12     def call(self, *args):
13         if self.dry or self.verbose:
14             print "$ " + ' '.join(args)
15         if self.dry: return
16         proc = None
17         if self.verbose:
18             proc = subprocess.Popen(args, stdout=sys.stdout, stderr=sys.stderr)
19         else:
20             proc = subprocess.Popen(args)
21         proc.communicate()
22         if proc.returncode:
23             raise CalledProcessError(proc.returncode, args)
24