|
27 | 27 | import re
|
28 | 28 | import string
|
29 | 29 | import time
|
| 30 | +import uuid |
30 | 31 |
|
31 | 32 | from absl import logging
|
32 | 33 | from etils import epath
|
@@ -115,6 +116,94 @@ def _get_incomplete_dir(dir_name: str) -> str:
|
115 | 116 | return f'{dir_name.parent}/{constants.INCOMPLETE_PREFIX}{random_suffix}_{dir_name.name}/'
|
116 | 117 |
|
117 | 118 |
|
| 119 | +def _tmp_file_prefix() -> str: |
| 120 | + return f'{constants.INCOMPLETE_PREFIX}{uuid.uuid4().hex}' |
| 121 | + |
| 122 | + |
| 123 | +def _tmp_file_name( |
| 124 | + path: epath.PathLike, |
| 125 | + subfolder: str | None = None, |
| 126 | +) -> epath.Path: |
| 127 | + """Returns the temporary file name for the given path. |
| 128 | +
|
| 129 | + Args: |
| 130 | + path: The path to the file. |
| 131 | + subfolder: The subfolder to use. If None, then the parent of the path will |
| 132 | + be used. |
| 133 | + """ |
| 134 | + path = epath.Path(path) |
| 135 | + file_name = f'{_tmp_file_prefix()}.{path.name}' |
| 136 | + if subfolder: |
| 137 | + return path.parent / subfolder / file_name |
| 138 | + else: |
| 139 | + return path.parent / file_name |
| 140 | + |
| 141 | + |
| 142 | +@contextlib.contextmanager |
| 143 | +def atomic_write(path: epath.PathLike, mode: str): |
| 144 | + """Writes to path atomically, by writing to temp file and renaming it.""" |
| 145 | + tmp_path = _tmp_file_name(path) |
| 146 | + with tmp_path.open(mode=mode) as file_: |
| 147 | + yield file_ |
| 148 | + tmp_path.replace(path) |
| 149 | + |
| 150 | + |
| 151 | +def is_incomplete_file(path: epath.Path) -> bool: |
| 152 | + """Returns whether the given filename suggests that it's incomplete.""" |
| 153 | + regex = rf'{re.escape(constants.INCOMPLETE_PREFIX)}[0-9a-fA-F]{{32}}\..+' |
| 154 | + return bool(re.search(rf'^{regex}$', path.name)) |
| 155 | + |
| 156 | + |
| 157 | +@contextlib.contextmanager |
| 158 | +def incomplete_file( |
| 159 | + path: epath.Path, |
| 160 | + subfolder: str | None = None, |
| 161 | +) -> Iterator[epath.Path]: |
| 162 | + """Writes to path atomically, by writing to temp file and renaming it.""" |
| 163 | + tmp_path = _tmp_file_name(path, subfolder=subfolder) |
| 164 | + tmp_path.parent.mkdir(exist_ok=True) |
| 165 | + try: |
| 166 | + yield tmp_path |
| 167 | + tmp_path.replace(path) |
| 168 | + finally: |
| 169 | + # Eventually delete the tmp_path if exception was raised |
| 170 | + tmp_path.unlink(missing_ok=True) |
| 171 | + |
| 172 | + |
| 173 | +@contextlib.contextmanager |
| 174 | +def incomplete_files( |
| 175 | + path: epath.Path, |
| 176 | +) -> Iterator[epath.Path]: |
| 177 | + """Writes to path atomically, by writing to temp file and renaming it.""" |
| 178 | + tmp_file_prefix = _tmp_file_prefix() |
| 179 | + tmp_path = path.parent / f'{tmp_file_prefix}.{path.name}' |
| 180 | + try: |
| 181 | + yield tmp_path |
| 182 | + # Rename all tmp files to their final name. |
| 183 | + for tmp_file in path.parent.glob(f'{tmp_file_prefix}.*'): |
| 184 | + file_name = tmp_file.name.removeprefix(tmp_file_prefix + '.') |
| 185 | + tmp_file.replace(path.parent / file_name) |
| 186 | + finally: |
| 187 | + # Eventually delete the tmp_path if exception was raised |
| 188 | + for tmp_file in path.parent.glob(f'{tmp_file_prefix}.*'): |
| 189 | + tmp_file.unlink(missing_ok=True) |
| 190 | + |
| 191 | + |
| 192 | +def clean_up_incomplete_files(path: epath.Path) -> None: |
| 193 | + """Deletes all incomplete files in the given path.""" |
| 194 | + deleted_incomplete_files = [] |
| 195 | + for f in path.glob(f'*{constants.INCOMPLETE_PREFIX}*'): |
| 196 | + if is_incomplete_file(f): |
| 197 | + deleted_incomplete_files.append(os.fspath(f)) |
| 198 | + f.unlink() |
| 199 | + if deleted_incomplete_files: |
| 200 | + logging.info( |
| 201 | + 'Deleted %d incomplete files. A small selection: %s', |
| 202 | + len(deleted_incomplete_files), |
| 203 | + '\n'.join(deleted_incomplete_files[:3]), |
| 204 | + ) |
| 205 | + |
| 206 | + |
118 | 207 | @contextlib.contextmanager
|
119 | 208 | def incomplete_dir(
|
120 | 209 | dirname: PathLike,
|
|
0 commit comments