Return a context manager that changes the current working directory. Arguments: path: the directory to use as the temporary current working directory. quiet: if False (the default), the context manager raises an exception on error. Otherwise, it issues only a warning and
(path, quiet=False)
| 547 | |
| 548 | @contextlib.contextmanager |
| 549 | def change_cwd(path, quiet=False): |
| 550 | """Return a context manager that changes the current working directory. |
| 551 | |
| 552 | Arguments: |
| 553 | |
| 554 | path: the directory to use as the temporary current working directory. |
| 555 | |
| 556 | quiet: if False (the default), the context manager raises an exception |
| 557 | on error. Otherwise, it issues only a warning and keeps the current |
| 558 | working directory the same. |
| 559 | |
| 560 | """ |
| 561 | saved_dir = os.getcwd() |
| 562 | try: |
| 563 | os.chdir(os.path.realpath(path)) |
| 564 | except OSError as exc: |
| 565 | if not quiet: |
| 566 | raise |
| 567 | logging.getLogger(__name__).warning( |
| 568 | 'tests may fail, unable to change the current working directory ' |
| 569 | 'to %r: %s', |
| 570 | path, |
| 571 | exc, |
| 572 | exc_info=exc, |
| 573 | stack_info=True, |
| 574 | stacklevel=3, |
| 575 | ) |
| 576 | try: |
| 577 | yield os.getcwd() |
| 578 | finally: |
| 579 | os.chdir(saved_dir) |
| 580 | |
| 581 | |
| 582 | @contextlib.contextmanager |
searching dependent graphs…