|
14 | 14 |
|
15 | 15 | from __future__ import annotations |
16 | 16 | from contextlib import contextmanager |
| 17 | +import functools |
17 | 18 | import operator |
18 | 19 | import re |
19 | 20 | import os |
|
22 | 23 | from pathlib import Path |
23 | 24 | import platform |
24 | 25 | import subprocess |
| 26 | +import time |
| 27 | +import venv |
25 | 28 |
|
26 | 29 | # a helper class which implements the same version ordering as RPM |
27 | 30 | class Version: |
@@ -129,13 +132,46 @@ def is_msys() -> bool: |
129 | 132 | def is_macos(): |
130 | 133 | return any(platform.mac_ver()[0]) |
131 | 134 |
|
| 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 | + |
132 | 168 | class FormattingError(Exception): |
133 | 169 | pass |
134 | 170 |
|
135 | 171 | def format_meson(files: T.Iterable[Path], *, check: bool = False) -> None: |
136 | 172 | if not files: |
137 | 173 | return |
138 | | - cmd: list[str | Path] = ['meson', 'format', '--configuration', './meson.format'] |
| 174 | + cmd: list[str | Path] = [venv_meson_path(), 'format', '--configuration', './meson.format'] |
139 | 175 | if check: |
140 | 176 | cmd.append('--check-only') |
141 | 177 | else: |
|
0 commit comments