| 14 | |
| 15 | @contextmanager |
| 16 | def subst_path_windows(filepath: Path): |
| 17 | for c in ascii_lowercase[7:]: # Create a subst drive from H-Z. |
| 18 | c += ":" |
| 19 | if not os.path.exists(c): |
| 20 | drive = c |
| 21 | break |
| 22 | else: |
| 23 | raise AssertionError("Unable to find suitable drive letter for subst.") |
| 24 | |
| 25 | directory = filepath.parent |
| 26 | basename = filepath.name |
| 27 | |
| 28 | args = ["subst", drive, str(directory)] |
| 29 | subprocess.check_call(args) |
| 30 | assert os.path.exists(drive) |
| 31 | try: |
| 32 | filename = Path(drive, os.sep, basename) |
| 33 | yield filename |
| 34 | finally: |
| 35 | args = ["subst", "/D", drive] |
| 36 | subprocess.check_call(args) |
| 37 | |
| 38 | |
| 39 | @contextmanager |