|
4 | 4 | from __future__ import absolute_import, print_function
|
5 | 5 |
|
6 | 6 | import fcntl
|
| 7 | +import itertools |
7 | 8 | import os
|
| 9 | +import time |
8 | 10 | from contextlib import contextmanager
|
9 | 11 |
|
10 | 12 | from pex.common import safe_mkdir
|
11 | 13 | from pex.typing import TYPE_CHECKING
|
12 | 14 | from pex.variables import ENV
|
13 | 15 |
|
14 | 16 | if TYPE_CHECKING:
|
15 |
| - from typing import Iterator, Optional, Tuple |
| 17 | + from typing import Iterator, Optional, Tuple, Union |
| 18 | + |
| 19 | + from pex.cache.dirs import AtomicCacheDir, UnzipDir, VenvDirs # noqa |
16 | 20 |
|
17 | 21 |
|
18 | 22 | # N.B.: The lock file path is last in the lock state tuple to allow for a simple encoding scheme in
|
@@ -99,3 +103,30 @@ def await_delete_lock():
|
99 | 103 | lock_file = _lock(exclusive=False)
|
100 | 104 | yield lock_file
|
101 | 105 | _lock(exclusive=True)
|
| 106 | + |
| 107 | + |
| 108 | +def record_access( |
| 109 | + atomic_cache_dir, # type: AtomicCacheDir |
| 110 | + last_access=None, # type: Optional[float] |
| 111 | +): |
| 112 | + # type: (...) -> None |
| 113 | + |
| 114 | + # N.B.: We explicitly set atime and do not rely on the filesystem implicitly setting it when the |
| 115 | + # directory is read since filesystems may be mounted noatime, nodiratime or relatime on Linux |
| 116 | + # and similar toggles exist, at least in part, for some macOS file systems. |
| 117 | + atime = last_access or time.time() |
| 118 | + mtime = os.stat(atomic_cache_dir.path).st_mtime |
| 119 | + os.utime(atomic_cache_dir.path, (atime, mtime)) |
| 120 | + |
| 121 | + |
| 122 | +def iter_all_cached_pex_dirs(): |
| 123 | + # type: () -> Iterator[Tuple[Union[UnzipDir, VenvDirs], float]] |
| 124 | + |
| 125 | + from pex.cache.dirs import UnzipDir, VenvDirs |
| 126 | + |
| 127 | + pex_dirs = itertools.chain( |
| 128 | + UnzipDir.iter_all(), VenvDirs.iter_all() |
| 129 | + ) # type: Iterator[Union[UnzipDir, VenvDirs]] |
| 130 | + for pex_dir in pex_dirs: |
| 131 | + last_access = os.stat(pex_dir.path).st_atime |
| 132 | + yield pex_dir, last_access |
0 commit comments