From 3609a5b95bca80a462043c978c6ee1d7cff90ab6 Mon Sep 17 00:00:00 2001 From: wirawan Date: Fri, 8 Jan 2010 18:43:06 +0000 Subject: [PATCH] * Module wpylib.shell_tools will contain common utilities similar to those that I have been using in my shell. * Function added: mcd. --- shell_tools.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 shell_tools.py diff --git a/shell_tools.py b/shell_tools.py new file mode 100644 index 0000000..370b2fb --- /dev/null +++ b/shell_tools.py @@ -0,0 +1,34 @@ +# $Id: shell_tools.py,v 1.1 2010-01-08 18:43:06 wirawan Exp $ +# +# wpylib.shell_tools +# Created: 20100106 +# Wirawan Purwanto +# +# Simple and dirty tools like those I usually use in my shell +# scripts. +# + +import os +import subprocess +import sys + +def mcd(subdir): + # Assuming we have GNU coreutils' mkdir + cmd = ["mkdir", "-p", subdir] + try: + retcode = subprocess.call(cmd, shell=False) + if retcode == 0: + os.chdir(subdir) + return + + print >>sys.stderr, "mcd " + subdir + ": ", + if retcode < 0: + print >>sys.stderr, "mkdir was terminated by signal", -retcode + else: + print >>sys.stderr, "mkdir returned", retcode + raise RuntimeError, "Directory creation failure" + except OSError, e: + print >>sys.stderr, "mcd failed:", e + raise + +