[RFC,v1,3/3] utils: checkstyle.py: Add `MesonFormatter`
diff mbox series

Message ID 20260202112511.640320-4-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • meson: Add `meson.format` file
Related show

Commit Message

Barnabás Pőcze Feb. 2, 2026, 11:25 a.m. UTC
Add support for diagnosing meson file style issues using `meson format`.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
This probably makes `MesonChecker` unnecessary.
---
 utils/checkstyle.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

--
2.52.0

Patch
diff mbox series

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index 06b9f199a..2dfff0473 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -919,6 +919,36 @@  class StripTrailingSpaceFormatter(Formatter):
             lines[i] = lines[i].rstrip() + '\n'
         return ''.join(lines)

+class MesonFormatter(Formatter):
+    @staticmethod
+    def _check_meson_version():
+        # 1.5.0 for `meson format`
+        # 1.7.0 for stdin input
+        # 1.9.0 for `--source-file-path`
+        VERSION_REQ = (1, 9, 0)
+
+        ret = subprocess.run(['meson', '--version'],
+                             stdin=subprocess.DEVNULL,
+                             stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+
+        ver = tuple(map(int, ret.stdout.decode('utf-8').split('.')))
+        if ver < VERSION_REQ:
+            return [
+                CommitIssue(f'Missing meson {".".join(map(str, VERSION_REQ))} to run `meson format`')
+            ]
+
+
+    dependencies = (('meson', _check_meson_version), )
+    patterns = ('meson.build', )
+
+    @classmethod
+    def format(cls, filename, data):
+        ret = subprocess.run(['meson', 'format', '-',
+                              '--source-file-path', filename],
+                             input=data.encode('utf-8'), stdout=subprocess.PIPE)
+        return ret.stdout.decode('utf-8')
+

 # ------------------------------------------------------------------------------
 # Style checking