|
8 | 8 |
|
9 | 9 | import os |
10 | 10 | import logging |
| 11 | +import random |
| 12 | +import string |
11 | 13 | import shlex |
12 | 14 | from typing import Callable |
13 | 15 | import docker |
@@ -60,17 +62,35 @@ def run_in_container( |
60 | 62 | bash.pipe_logs(_logger, logs) |
61 | 63 |
|
62 | 64 |
|
63 | | -def restore_mtime_script(original_mtime: dict[str, int]) -> list[str]: |
| 65 | +def restore_mtime_script( |
| 66 | + src_dir: str, mnt_dir: str, original_mtime: dict[str, int] |
| 67 | +) -> list[str]: |
64 | 68 | """Restore original mtimes for files after they have been modified""" |
65 | | - script: list[str] = [] |
| 69 | + if not original_mtime: |
| 70 | + return [] |
| 71 | + |
| 72 | + script: list[str] = ["import os"] |
66 | 73 | for file_path, mtime in original_mtime.items(): |
67 | | - script.append( |
68 | | - 'echo "import os; os.utime(' |
69 | | - + f'\\"{file_path}\\", ns=({mtime}, {mtime})' |
70 | | - + ')" | python3 -u' |
71 | | - ) |
| 74 | + script.append(f'os.utime("{file_path}", ns=({mtime}, {mtime}))') |
| 75 | + |
| 76 | + while os.path.exists( |
| 77 | + (script_path := os.path.join(src_dir, f".{randomword(10)}.py")) |
| 78 | + ): |
| 79 | + pass |
| 80 | + |
| 81 | + with open(script_path, "w", encoding="utf-8") as f: |
| 82 | + _ = f.write("\n".join(script)) |
| 83 | + |
| 84 | + docker_path = shlex.quote( |
| 85 | + os.path.join(mnt_dir, os.path.relpath(script_path, src_dir)) |
| 86 | + ) |
| 87 | + return [f"python3 -u {docker_path}", f"rm {docker_path}"] |
| 88 | + |
72 | 89 |
|
73 | | - return script |
| 90 | +def randomword(length: int) -> str: |
| 91 | + """Create a random string""" |
| 92 | + letters = string.ascii_lowercase |
| 93 | + return "".join(random.choice(letters) for i in range(length)) |
74 | 94 |
|
75 | 95 |
|
76 | 96 | def register(builder: Builder) -> None: |
@@ -177,5 +197,5 @@ def docker_file_path(file_path: str) -> str: |
177 | 197 | os.path.relpath(file_path, src_dir), |
178 | 198 | ) |
179 | 199 |
|
180 | | - script += restore_mtime_script(original_mtime) |
| 200 | + script += restore_mtime_script(src_dir, MOUNT_SRC, original_mtime) |
181 | 201 | run_in_container(builder, src_dir, logger, script) |
0 commit comments