Johnny Chen | de6ade0 | 2011-10-12 19:16:06 +0000 | [diff] [blame] | 1 | """Utility for changing directories and execution of commands in a subshell.""" |
| 2 | |
| 3 | import os, shlex, subprocess |
| 4 | |
| 5 | def 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 | |
| 14 | def 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 |