Execute a block in a temporary directory and remove the directory and its contents at the end of the with block. YIELDS (Path): The path of the temp directory.
()
| 1115 | |
| 1116 | @contextmanager |
| 1117 | def make_tempdir() -> Generator[Path, None, None]: |
| 1118 | """Execute a block in a temporary directory and remove the directory and |
| 1119 | its contents at the end of the with block. |
| 1120 | YIELDS (Path): The path of the temp directory. |
| 1121 | """ |
| 1122 | d = Path(tempfile.mkdtemp()) |
| 1123 | yield d |
| 1124 | |
| 1125 | # On Windows, git clones use read-only files, which cause permission errors |
| 1126 | # when being deleted. This forcibly fixes permissions. |
| 1127 | def force_remove(rmfunc, path, ex): |
| 1128 | os.chmod(path, stat.S_IWRITE) |
| 1129 | rmfunc(path) |
| 1130 | |
| 1131 | try: |
| 1132 | if sys.version_info >= (3, 12): |
| 1133 | shutil.rmtree(str(d), onexc=force_remove) |
| 1134 | else: |
| 1135 | shutil.rmtree(str(d), onerror=force_remove) |
| 1136 | except PermissionError as e: |
| 1137 | warnings.warn(Warnings.W091.format(dir=d, msg=e)) |
| 1138 | |
| 1139 | |
| 1140 | def is_in_jupyter() -> bool: |
no outgoing calls
searching dependent graphs…