Skip to content

Commit efb7f27

Browse files
committed
Use latest Meson prerelease for 'meson format'
Formatting rules change over time, and CI uses the latest Meson prerelease, so we should match that version on developers' systems. If not running in CI, install Meson into a virtualenv that we update at most once per day, and use it for format checks in sanity_checks.py and format.py.
1 parent c1fe16b commit efb7f27

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ __pycache__
22
.idea
33
.vscode
44
*~
5+
6+
tools/mesonenv

tools/utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616
from contextlib import contextmanager
17+
import functools
1718
import operator
1819
import re
1920
import os
@@ -22,6 +23,8 @@
2223
from pathlib import Path
2324
import platform
2425
import subprocess
26+
import time
27+
import venv
2528

2629
# a helper class which implements the same version ordering as RPM
2730
class Version:
@@ -129,13 +132,46 @@ def is_msys() -> bool:
129132
def is_macos():
130133
return any(platform.mac_ver()[0])
131134

135+
@functools.cache
136+
def venv_meson_path() -> Path:
137+
if os.getenv('CI', 'false') == 'true':
138+
# assume CI already has a current Meson
139+
return Path('meson')
140+
141+
env_dir = Path(__file__).parent / 'mesonenv'
142+
if not env_dir.exists():
143+
venv.create(env_dir, with_pip=True)
144+
145+
if platform.system() == 'Windows':
146+
for subdir in 'Scripts', 'bin':
147+
if (env_dir / subdir).exists():
148+
meson = env_dir / subdir / 'meson.exe'
149+
break
150+
else:
151+
raise Exception("Couldn't find venv bin dir")
152+
else:
153+
meson = env_dir / 'bin/meson'
154+
155+
try:
156+
if meson.stat().st_mtime + 86400 >= time.time():
157+
return meson
158+
except FileNotFoundError:
159+
pass
160+
161+
subprocess.run([
162+
meson.with_stem('pip'), 'install', '--disable-pip-version-check',
163+
'-qU', '--pre', 'meson'
164+
], check=True)
165+
os.utime(meson)
166+
return meson
167+
132168
class FormattingError(Exception):
133169
pass
134170

135171
def format_meson(files: T.Iterable[Path], *, check: bool = False) -> None:
136172
if not files:
137173
return
138-
cmd: list[str | Path] = ['meson', 'format', '--configuration', './meson.format']
174+
cmd: list[str | Path] = [venv_meson_path(), 'format', '--configuration', './meson.format']
139175
if check:
140176
cmd.append('--check-only')
141177
else:

0 commit comments

Comments
 (0)