| 1 | """ |
|---|
| 2 | Extension functions for manipulating the current working directory (cwd). |
|---|
| 3 | |
|---|
| 4 | Commands: |
|---|
| 5 | |
|---|
| 6 | chdir -- push the cwd onto the directory stack & change to the new location. |
|---|
| 7 | popd -- change to the last directory on the directory stack. |
|---|
| 8 | """ |
|---|
| 9 | import os |
|---|
| 10 | |
|---|
| 11 | _dirstack = [] |
|---|
| 12 | |
|---|
| 13 | def chdir(where): |
|---|
| 14 | """ |
|---|
| 15 | >> chdir <where> |
|---|
| 16 | |
|---|
| 17 | Change to the new location, after saving the current directory onto |
|---|
| 18 | the directory stack. The global variable __dir__ is set to the cwd. |
|---|
| 19 | """ |
|---|
| 20 | from twill import commands |
|---|
| 21 | |
|---|
| 22 | cwd = os.getcwd() |
|---|
| 23 | _dirstack.append(cwd) |
|---|
| 24 | print cwd |
|---|
| 25 | |
|---|
| 26 | os.chdir(where) |
|---|
| 27 | print>>commands.OUT, 'changed directory to "%s"' % (where,) |
|---|
| 28 | |
|---|
| 29 | commands.setglobal('__dir__', where) |
|---|
| 30 | |
|---|
| 31 | def popd(): |
|---|
| 32 | """ |
|---|
| 33 | >> popd |
|---|
| 34 | |
|---|
| 35 | Change back to the last directory on the directory stack. The global |
|---|
| 36 | variable __dir__ is set to the cwd. |
|---|
| 37 | """ |
|---|
| 38 | from twill import commands |
|---|
| 39 | |
|---|
| 40 | where = _dirstack.pop() |
|---|
| 41 | os.chdir(where) |
|---|
| 42 | print>>commands.OUT, 'popped back to directory "%s"' % (where,) |
|---|
| 43 | |
|---|
| 44 | commands.setglobal('__dir__', where) |
|---|