utils.py revision 5e573a97db593d54ab19da799ca2ef4fbea65c09
1"""Utility for changing directories and execution of commands in a subshell."""
2
3import os, shlex, subprocess
4
5def chdir(debugger, args, result, dict):
6    """Change the working directory, or cd to ${HOME}."""
7    dir = args.strip()
8    if dir:
9        os.chdir(args)
10    else:
11        os.chdir(os.path.expanduser('~'))
12    print "Current working directory: %s" % os.getcwd()
13
14def system(debugger, command_line, result, dict):
15    """Execute the command (a string) in a subshell."""
16    args = shlex.split(command_line)
17    process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18    output, error = process.communicate()
19    retcode = process.poll()
20    if output and error:
21        print "stdout=>\n", output
22        print "stderr=>\n", error
23    elif output:
24        print output
25    elif error:
26        print error
27    print "retcode:", retcode
28