Skip to content

Commit 0ec63d9

Browse files
committed
test(build): expire old cache files before building
should not be kept indefinitely, since sources may've been changed already ci cache dir is expected to only survive for the current build session, prefer similar behaviour for local build tests
1 parent 84e43c7 commit 0ec63d9

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

code/scripts/test_build.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
CACHE_PATH = TEST_PATH / "cache"
3939
BUILD_PATH = ROOT_PATH / ".pio" / "build"
4040

41+
CACHE_TIMEDELTA = datetime.timedelta(days=1)
42+
TIMEDELTA_PARAM = {
43+
"d": "days",
44+
"h": "hours",
45+
"m": "minutes",
46+
"s": "seconds",
47+
}
48+
4149

4250
def bold(string: str):
4351
return clr(Color.BOLD, string)
@@ -54,7 +62,46 @@ def pluralize(string: str, length: int):
5462
return string
5563

5664

65+
def make_timedelta(string: str) -> datetime.timedelta:
66+
if not string:
67+
return datetime.timedelta(seconds=0)
68+
69+
if string.isdigit():
70+
suffix = "d"
71+
else:
72+
suffix = string[-1]
73+
string = string[:-1]
74+
75+
param = TIMEDELTA_PARAM[suffix]
76+
value = int(string, 10)
77+
78+
return datetime.timedelta(**{param: value})
79+
80+
81+
def cache_cleanup(cache_path: pathlib.Path, offset: datetime.timedelta):
82+
now = datetime.datetime.now()
83+
84+
for pair in cache_path.iterdir():
85+
# {CACHE_DIR} / AA / AA...rest of the hash...
86+
if not pair.is_dir():
87+
continue
88+
89+
for f in pair.iterdir():
90+
mtime_raw = f.stat().st_mtime
91+
mtime_dt = datetime.datetime.fromtimestamp(mtime_raw)
92+
93+
if now - mtime_dt > offset:
94+
f.unlink()
95+
96+
if not any(pair.iterdir()):
97+
pair.rmdir()
98+
99+
57100
def build_configurations(args: argparse.Namespace, configurations: list[pathlib.Path]):
101+
cache_path = args.cache_path.resolve()
102+
103+
cache_cleanup(cache_path, args.expire_cache)
104+
58105
cmd = ["platformio", "run"]
59106
if args.silent:
60107
cmd.extend(["-s"])
@@ -67,7 +114,7 @@ def build_configurations(args: argparse.Namespace, configurations: list[pathlib.
67114
log.info("%s contents\n%s", bold(cfg.name), cfg.read_text())
68115

69116
os_env = os.environ.copy()
70-
os_env["PLATFORMIO_BUILD_CACHE_DIR"] = args.cache_path.resolve().as_posix()
117+
os_env["PLATFORMIO_BUILD_CACHE_DIR"] = cache_path.resolve().as_posix()
71118
if args.single_source:
72119
os_env["ESPURNA_BUILD_SINGLE_SOURCE"] = "1"
73120

@@ -99,7 +146,10 @@ def build_configurations(args: argparse.Namespace, configurations: list[pathlib.
99146

100147
log.info(
101148
"%s finished in %s, %s is %s bytes",
102-
*(bold(str(x)) for x in (cfg, diff, firmware_bin, firmware_bin.stat().st_size)),
149+
*(
150+
bold(str(x))
151+
for x in (cfg, diff, firmware_bin, firmware_bin.stat().st_size)
152+
),
103153
)
104154

105155
build_time += diff
@@ -211,6 +261,13 @@ def main(args: argparse.Namespace):
211261
help="PlatformIO build path",
212262
)
213263

264+
parser.add_argument(
265+
"--expire-cache",
266+
default=CACHE_TIMEDELTA,
267+
type=make_timedelta,
268+
help="PlatformIO cache expiration time (NUMBER or NUMBER{d,h,m,s} for days, hours, minutes or seconds respectively)}",
269+
)
270+
214271
parser.add_argument(
215272
"configurations",
216273
nargs="*",

0 commit comments

Comments
 (0)