Skip to content

Commit 06b8850

Browse files
authored
Support pex3 cache prune --older-than .... (#2586)
Work towards #2528.
1 parent c81de55 commit 06b8850

35 files changed

+2301
-303
lines changed

CHANGES.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Release Notes
22

3+
## 2.24.0
4+
5+
This release adds `pex3 cache prune` as a likely more useful Pex cache
6+
management command than the existing `pex3 cache purge`. By default
7+
`pex3 cache prune` prunes any cached items not used for the last 2
8+
weeks and is likely suitable for use as a daily cron job to keep Pex
9+
cache sizes down. The default age of 2 weeks can be overridden by
10+
specifying `--older-than "1 week"` or `--last-access-before 14/3/2024`,
11+
etc. See `pex3 cache prune --help` for more details.
12+
13+
* Support `pex3 cache prune --older-than ...`. (#2586)
14+
315
## 2.23.0
416

517
This release adds support for drawing requirements from

pex/cache/access.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
from __future__ import absolute_import, print_function
55

66
import fcntl
7+
import itertools
78
import os
9+
import time
810
from contextlib import contextmanager
911

1012
from pex.common import safe_mkdir
1113
from pex.typing import TYPE_CHECKING
1214
from pex.variables import ENV
1315

1416
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
1620

1721

1822
# 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():
99103
lock_file = _lock(exclusive=False)
100104
yield lock_file
101105
_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

Comments
 (0)