MCPcopy Create free account
hub / github.com/pytest-dev/pytest / maybe_delete_a_numbered_dir

Function maybe_delete_a_numbered_dir

src/_pytest/pathlib.py:264–289  ·  view source on GitHub ↗

Remove a numbered directory if its lock can be obtained and it does not seem to be in use.

(path: Path)

Source from the content-addressed store, hash-verified

262
263
264def maybe_delete_a_numbered_dir(path: Path) -> None:
265 """Remove a numbered directory if its lock can be obtained and it does
266 not seem to be in use."""
267 path = ensure_extended_length_path(path)
268 lock_path = None
269 try:
270 lock_path = create_cleanup_lock(path)
271 parent = path.parent
272
273 garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
274 path.rename(garbage)
275 rm_rf(garbage)
276 except OSError:
277 # known races:
278 # * other process did a cleanup at the same time
279 # * deletable folder was found
280 # * process cwd (Windows)
281 return
282 finally:
283 # If we created the lock, ensure we remove it even if we failed
284 # to properly remove the numbered dir.
285 if lock_path is not None:
286 try:
287 lock_path.unlink()
288 except OSError:
289 pass
290
291
292def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool:

Callers 4

try_cleanupFunction · 0.85

Calls 3

create_cleanup_lockFunction · 0.85
rm_rfFunction · 0.85