| Conditions | 7 |
| Total Lines | 20 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 27 | @contextlib.contextmanager |
||
| 28 | def pushd_popd(newcwd=None, tempdir=False): |
||
| 29 | if newcwd and tempdir: |
||
| 30 | raise Exception("pushd_popd can accept either newcwd or tempdir, not both") |
||
| 31 | try: |
||
| 32 | oldcwd = getcwd() |
||
| 33 | except FileNotFoundError as e: # pylint: disable=unused-variable |
||
| 34 | # This happens when a directory is deleted before the context is exited |
||
| 35 | oldcwd = '/tmp' |
||
| 36 | try: |
||
| 37 | if tempdir: |
||
| 38 | with TemporaryDirectory() as tempcwd: |
||
| 39 | chdir(tempcwd) |
||
| 40 | yield tempcwd |
||
| 41 | else: |
||
| 42 | if newcwd: |
||
| 43 | chdir(newcwd) |
||
| 44 | yield newcwd |
||
| 45 | finally: |
||
| 46 | chdir(oldcwd) |
||
| 47 | |||
| 57 |