Skip to content

Commit c1fe16b

Browse files
committed
Add utility function to run 'meson format'
1 parent e25f75e commit c1fe16b

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

tools/format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from pathlib import Path
2121
import subprocess
2222

23+
from utils import format_meson
24+
2325
FORMAT_FILES = {'meson.build', 'meson_options.txt', 'meson.options'}
2426

2527
def main() -> None:
@@ -38,9 +40,7 @@ def main() -> None:
3840
patch_dir = Path('subprojects', 'packagefiles', patch_dir_name)
3941
files += [f for f in patch_dir.rglob('*') if f.name in FORMAT_FILES]
4042

41-
if files:
42-
cmd = ['meson', 'format', '--configuration', './meson.format', '--inplace']
43-
subprocess.run(cmd + files, check=True)
43+
format_meson(files)
4444

4545

4646
if __name__ == '__main__':

tools/sanity_checks.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import shutil
3030

3131
from pathlib import Path
32-
from utils import Version, ci_group, is_ci, is_alpinelike, is_debianlike, is_macos, is_windows, is_msys
32+
from utils import Version, ci_group, is_ci, is_alpinelike, is_debianlike, is_macos, is_windows, is_msys, FormattingError, format_meson
3333

3434
PERMITTED_FILES = {'generator.sh', 'meson.build', 'meson_options.txt', 'meson.options', 'LICENSE.build'}
3535
PER_PROJECT_PERMITTED_FILES: dict[str, set[str]] = {
@@ -606,19 +606,6 @@ def is_permitted_file(self, subproject: str, filename: str) -> bool:
606606
return True
607607
return False
608608

609-
def is_formatted_correctly(self, file: Path) -> bool:
610-
res = subprocess.run(
611-
[
612-
"meson",
613-
"format",
614-
"--check-only",
615-
"--configuration",
616-
"./meson.format",
617-
file.absolute(),
618-
]
619-
)
620-
return res.returncode == 0
621-
622609
def check_project_args(self, name: str, dir: Path) -> None:
623610
if not dir.exists():
624611
# build has not run and unpacked the source; do that
@@ -684,8 +671,11 @@ def check_files(self, subproject: str, patch_path: Path) -> None:
684671
for f in patch_path.rglob('*'):
685672
if f.is_dir():
686673
continue
687-
if f.name in FORMAT_CHECK_FILES and not self.is_formatted_correctly(f):
688-
unformatted.append(f)
674+
if f.name in FORMAT_CHECK_FILES:
675+
try:
676+
format_meson([f], check=True)
677+
except FormattingError:
678+
unformatted.append(f)
689679
if not self.is_permitted_file(subproject, f.name):
690680
not_permitted.append(f)
691681
elif f.name in NO_TABS_FILES and '\t' in f.read_text(encoding='utf-8'):

tools/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
1516
from contextlib import contextmanager
1617
import operator
1718
import re
1819
import os
1920
import sys
2021
import typing as T
22+
from pathlib import Path
2123
import platform
24+
import subprocess
2225

2326
# a helper class which implements the same version ordering as RPM
2427
class Version:
@@ -125,3 +128,20 @@ def is_msys() -> bool:
125128

126129
def is_macos():
127130
return any(platform.mac_ver()[0])
131+
132+
class FormattingError(Exception):
133+
pass
134+
135+
def format_meson(files: T.Iterable[Path], *, check: bool = False) -> None:
136+
if not files:
137+
return
138+
cmd: list[str | Path] = ['meson', 'format', '--configuration', './meson.format']
139+
if check:
140+
cmd.append('--check-only')
141+
else:
142+
cmd.append('--inplace')
143+
cmd.extend(files)
144+
try:
145+
subprocess.run(cmd, check=True)
146+
except subprocess.CalledProcessError as ex:
147+
raise FormattingError from ex

0 commit comments

Comments
 (0)