Debian Codegraph @

setup/fmt.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
3
4 import hashlib
5 import os
6 import subprocess
7 import sys
8 from concurrent.futures import ThreadPoolExecutor, as_completed
9
10 from setup import Command, iswindows
11
12 C_EXTENSIONS = ('.c', '.cpp', '.h', '.m')
13 C_FORMAT_DIRS = ('src', 'bypy/linux', 'bypy/macos', 'bypy/windows')
14
15
16 def md5_of_file(path):
17 h = hashlib.md5()
18 with open(path, 'rb') as f:
19 for chunk in iter(lambda: f.read(65536), b''):
20 h.update(chunk)
21 return h.hexdigest()
22
23
24 def run_captured(cmd, cwd):
25 return subprocess.run(cmd, cwd=cwd, capture_output=True)
26
27
28 def show_output(result):
29 if result.stdout:
30 sys.stdout.buffer.write(result.stdout)
31 sys.stdout.buffer.flush()
32 if result.stderr:
33 sys.stderr.buffer.write(result.stderr)
34 sys.stderr.buffer.flush()
35
36
37 class AutoFormat(Command):
38 description = 'Autoformat source code'
39 usage_help = 'To format specific files specify them on the command line'
40 require_venv = True
41
42 def add_options(self, parser):
43 parser.add_option('--check-only', default=False, action='store_true', help='Only check for formatting issues dont fix them')
44
45 def clang_format_cache_dir(self):
46 d = self.j(self.PROJECT_ROOT, '.cache', 'clang-format')
47 os.makedirs(d, exist_ok=True)
48 return d
49
50 def clang_format_file(self, path, cache_dir, check_only):
51 # Cache file keyed by path hash; stores MD5 of last formatted content
52 path_hash = hashlib.md5(os.path.abspath(path).encode()).hexdigest()
53 cache_file = os.path.join(cache_dir, path_hash)
54 current_md5 = md5_of_file(path)
55
56 if not check_only:
57 try:
58 with open(cache_file) as f:
59 if f.read().strip() == current_md5:
60 return None # already formatted, skip
61 except FileNotFoundError:
62 pass
63
64 cf = self.j(self.PROJECT_ROOT, '.venv/bin/clang-format')
65 cmd = [cf, '-style=file']
66 if check_only:
67 cmd += ['--dry-run', '--Werror', path]
68 else:
69 cmd += ['-i', path]
70
71 result = subprocess.run(cmd, capture_output=True)
72
73 if result.returncode == 0 and not check_only:
74 new_md5 = md5_of_file(path)
75 tmp = cache_file + '.tmp'
76 with open(tmp, 'w') as f:
77 f.write(new_md5)
78 os.replace(tmp, cache_file)
79
80 return result
81
82 def find_c_files(self):
83 files = []
84 for d in C_FORMAT_DIRS:
85 dirpath = self.j(self.PROJECT_ROOT, d)
86 for root, _, filenames in os.walk(dirpath):
87 for fn in filenames:
88 if fn.endswith(C_EXTENSIONS):
89 files.append(os.path.join(root, fn))
90 return files
91
92 def run(self, opts):
93 rapydscript = self.j(self.PROJECT_ROOT, '.venv/bin/rapydscript')
94 ruff = self.j(self.PROJECT_ROOT, '.venv/bin/ruff')
95 if iswindows:
96 rapydscript += '.exe'
97 ruff += '.exe'
98
99 py_files = ()
100 pyj_files = ()
101 c_files = None # None means auto-discover all
102
103 if opts.cli_args:
104 py_files = tuple(x for x in opts.cli_args if x.endswith(('.py', '.recipe')))
105 pyj_files = tuple(x for x in opts.cli_args if x.endswith('.pyj'))
106 c_files = [x for x in opts.cli_args if x.endswith(C_EXTENSIONS)]
107 if not py_files and not pyj_files and not c_files:
108 return
109 else:
110 c_files = self.find_c_files()
111
112 run_py = not opts.cli_args or bool(py_files)
113 run_pyj = not opts.cli_args or bool(pyj_files)
114
115 import tomllib
116
117 with open(self.j(self.PROJECT_ROOT, 'pyproject.toml')) as f:
118 m = tomllib.loads(f.read())
119 line_length = m['tool']['ruff']['line-length']
120
121 failed = []
122
123 # Phase 1: ruff check --fix-only must complete before ruff format runs
124 if run_py:
125 cmd = [ruff, 'check', '--fix-only']
126 if opts.check_only:
127 cmd.append('--diff')
128 result = run_captured(cmd + list(py_files), self.PROJECT_ROOT)
129 if result.returncode != 0:
130 show_output(result)
131 failed.append('ruff check --fix-only')
132
133 if failed:
134 raise SystemExit(1)
135
136 # Phase 2: ruff format, rapydscript fmt, and clang-format all in parallel
137 cache_dir = self.clang_format_cache_dir()
138
139 with ThreadPoolExecutor() as executor:
140 futures = {}
141
142 if run_py:
143 cmd = [ruff, 'format']
144 if opts.check_only:
145 cmd.append('--check')
146 futures[executor.submit(run_captured, cmd + list(py_files), self.PROJECT_ROOT)] = 'ruff format'
147
148 if run_pyj:
149 cmd = [rapydscript, 'fmt', '--line-length', str(line_length)]
150 if opts.check_only:
151 cmd.append('--check-only')
152 futures[executor.submit(run_captured, cmd + (list(pyj_files) or ['src/pyj']), self.PROJECT_ROOT)] = 'rapydscript fmt'
153
154 for path in c_files:
155 rel = os.path.relpath(path, self.PROJECT_ROOT)
156 fut = executor.submit(self.clang_format_file, path, cache_dir, opts.check_only)
157 futures[fut] = f'clang-format:{rel}'
158
159 for fut in as_completed(futures):
160 label = futures[fut]
161 result = fut.result()
162 if result is None:
163 continue # cache hit, skipped
164 if result.returncode != 0:
165 show_output(result)
166 failed.append(label)
167
168 if failed:
169 print(f'Formatting failed: {", ".join(failed)}', file=sys.stderr)
170 raise SystemExit(1)