-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathmigrate_to_ruff.py
More file actions
executable file
·325 lines (262 loc) · 10.8 KB
/
migrate_to_ruff.py
File metadata and controls
executable file
·325 lines (262 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
"""Configuration converter script for migrating python-mode configs to Ruff.
This script helps users migrate their existing python-mode configuration
from the old linting tools (pylint, pyflakes, pycodestyle, etc.) to Ruff.
Usage:
python scripts/migrate_to_ruff.py [--vimrc-file <path>] [--output <path>]
"""
import argparse
import re
import sys
from pathlib import Path
from typing import List, Tuple, Optional
# Mapping of old linter names to Ruff rule categories
LINTER_TO_RUFF_RULES = {
'pyflakes': ['F'],
'pycodestyle': ['E', 'W'],
'pep8': ['E', 'W'],
'mccabe': ['C90'],
'pylint': ['PLE', 'PLR', 'PLW'],
'pydocstyle': ['D'],
'pep257': ['D'],
'autopep8': ['E', 'W'],
}
def find_vimrc_files() -> List[Path]:
"""Find common vimrc file locations."""
candidates = [
Path.home() / '.vimrc',
Path.home() / '.vim' / 'vimrc',
Path.home() / '.config' / 'nvim' / 'init.vim',
Path.home() / '.config' / 'nvim' / 'init.lua',
]
return [p for p in candidates if p.exists()]
def parse_vimrc_config(file_path: Path) -> dict:
"""Parse vimrc file and extract python-mode configuration."""
config = {
'lint_checkers': [],
'lint_ignore': [],
'lint_select': [],
'ruff_enabled': None,
'ruff_format_enabled': None,
'ruff_ignore': [],
'ruff_select': [],
'max_line_length': None,
'mccabe_complexity': None,
}
if not file_path.exists():
return config
content = file_path.read_text()
# Extract g:pymode_lint_checkers
checkers_match = re.search(r'let\s+g:pymode_lint_checkers\s*=\s*\[(.*?)\]', content)
if checkers_match:
checkers_str = checkers_match.group(1)
config['lint_checkers'] = [
c.strip().strip("'\"")
for c in re.findall(r"['\"]([^'\"]+)['\"]", checkers_str)
]
# Extract g:pymode_lint_ignore
ignore_match = re.search(r'let\s+g:pymode_lint_ignore\s*=\s*\[(.*?)\]', content)
if ignore_match:
ignore_str = ignore_match.group(1)
config['lint_ignore'] = [
i.strip().strip("'\"")
for i in re.findall(r"['\"]([^'\"]+)['\"]", ignore_str)
]
# Extract g:pymode_lint_select
select_match = re.search(r'let\s+g:pymode_lint_select\s*=\s*\[(.*?)\]', content)
if select_match:
select_str = select_match.group(1)
config['lint_select'] = [
s.strip().strip("'\"")
for s in re.findall(r"['\"]([^'\"]+)['\"]", select_str)
]
# Extract g:pymode_ruff_enabled
ruff_enabled_match = re.search(r'let\s+g:pymode_ruff_enabled\s*=\s*(\d+)', content)
if ruff_enabled_match:
config['ruff_enabled'] = ruff_enabled_match.group(1) == '1'
# Extract g:pymode_ruff_format_enabled
ruff_format_match = re.search(r'let\s+g:pymode_ruff_format_enabled\s*=\s*(\d+)', content)
if ruff_format_match:
config['ruff_format_enabled'] = ruff_format_match.group(1) == '1'
# Extract g:pymode_ruff_ignore
ruff_ignore_match = re.search(r'let\s+g:pymode_ruff_ignore\s*=\s*\[(.*?)\]', content)
if ruff_ignore_match:
ruff_ignore_str = ruff_ignore_match.group(1)
config['ruff_ignore'] = [
i.strip().strip("'\"")
for i in re.findall(r"['\"]([^'\"]+)['\"]", ruff_ignore_str)
]
# Extract g:pymode_ruff_select
ruff_select_match = re.search(r'let\s+g:pymode_ruff_select\s*=\s*\[(.*?)\]', content)
if ruff_select_match:
ruff_select_str = ruff_select_match.group(1)
config['ruff_select'] = [
s.strip().strip("'\"")
for s in re.findall(r"['\"]([^'\"]+)['\"]", ruff_select_str)
]
# Extract g:pymode_options_max_line_length
max_line_match = re.search(r'let\s+g:pymode_options_max_line_length\s*=\s*(\d+)', content)
if max_line_match:
config['max_line_length'] = int(max_line_match.group(1))
# Extract g:pymode_lint_options_mccabe_complexity
mccabe_match = re.search(r'let\s+g:pymode_lint_options_mccabe_complexity\s*=\s*(\d+)', content)
if mccabe_match:
config['mccabe_complexity'] = int(mccabe_match.group(1))
return config
def convert_to_ruff_config(old_config: dict) -> dict:
"""Convert old python-mode config to Ruff-specific config."""
ruff_config = {
'ruff_enabled': True,
'ruff_format_enabled': old_config.get('lint_checkers') and 'autopep8' in old_config['lint_checkers'],
'ruff_select': [],
'ruff_ignore': old_config.get('lint_ignore', []).copy(),
'max_line_length': old_config.get('max_line_length'),
'mccabe_complexity': old_config.get('mccabe_complexity'),
}
# Convert lint_checkers to ruff_select rules
select_rules = set()
# Add rules from explicit select
if old_config.get('lint_select'):
select_rules.update(old_config['lint_select'])
# Add rules from enabled linters
for linter in old_config.get('lint_checkers', []):
if linter in LINTER_TO_RUFF_RULES:
select_rules.update(LINTER_TO_RUFF_RULES[linter])
# If no specific rules selected, use a sensible default
if not select_rules:
select_rules = {'F', 'E', 'W'} # Pyflakes + pycodestyle by default
ruff_config['ruff_select'] = sorted(list(select_rules))
# If ruff-specific config already exists, preserve it
if old_config.get('ruff_enabled') is not None:
ruff_config['ruff_enabled'] = old_config['ruff_enabled']
if old_config.get('ruff_format_enabled') is not None:
ruff_config['ruff_format_enabled'] = old_config['ruff_format_enabled']
if old_config.get('ruff_ignore'):
ruff_config['ruff_ignore'] = old_config['ruff_ignore']
if old_config.get('ruff_select'):
ruff_config['ruff_select'] = old_config['ruff_select']
return ruff_config
def generate_vimrc_snippet(config: dict) -> str:
"""Generate VimScript configuration snippet."""
lines = [
'" Ruff configuration for python-mode',
'" Generated by migrate_to_ruff.py',
'',
]
if config.get('ruff_enabled'):
lines.append('let g:pymode_ruff_enabled = 1')
if config.get('ruff_format_enabled'):
lines.append('let g:pymode_ruff_format_enabled = 1')
if config.get('ruff_select'):
select_str = ', '.join(f'"{r}"' for r in config['ruff_select'])
lines.append(f'let g:pymode_ruff_select = [{select_str}]')
if config.get('ruff_ignore'):
ignore_str = ', '.join(f'"{i}"' for i in config['ruff_ignore'])
lines.append(f'let g:pymode_ruff_ignore = [{ignore_str}]')
if config.get('max_line_length'):
lines.append(f'let g:pymode_options_max_line_length = {config["max_line_length"]}')
if config.get('mccabe_complexity'):
lines.append(f'let g:pymode_lint_options_mccabe_complexity = {config["mccabe_complexity"]}')
lines.append('')
return '\n'.join(lines)
def generate_pyproject_toml(config: dict) -> str:
"""Generate pyproject.toml configuration snippet."""
lines = [
'[tool.ruff]',
]
if config.get('max_line_length'):
lines.append(f'line-length = {config["max_line_length"]}')
if config.get('ruff_select'):
select_str = ', '.join(f'"{r}"' for r in config['ruff_select'])
lines.append(f'select = [{select_str}]')
if config.get('ruff_ignore'):
ignore_str = ', '.join(f'"{i}"' for i in config['ruff_ignore'])
lines.append(f'ignore = [{ignore_str}]')
if config.get('mccabe_complexity'):
lines.append('')
lines.append('[tool.ruff.lint.mccabe]')
lines.append(f'max-complexity = {config["mccabe_complexity"]}')
lines.append('')
return '\n'.join(lines)
def main():
parser = argparse.ArgumentParser(
description='Convert python-mode configuration to Ruff',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Analyze default vimrc file
python scripts/migrate_to_ruff.py
# Analyze specific vimrc file
python scripts/migrate_to_ruff.py --vimrc-file ~/.vimrc
# Generate migration output to file
python scripts/migrate_to_ruff.py --output migration.txt
"""
)
parser.add_argument(
'--vimrc-file',
type=Path,
help='Path to vimrc file (default: auto-detect)'
)
parser.add_argument(
'--output',
type=Path,
help='Output file for migration suggestions (default: stdout)'
)
parser.add_argument(
'--format',
choices=['vimrc', 'pyproject', 'both'],
default='both',
help='Output format (default: both)'
)
args = parser.parse_args()
# Find vimrc file
if args.vimrc_file:
vimrc_path = args.vimrc_file
if not vimrc_path.exists():
print(f"Error: File not found: {vimrc_path}", file=sys.stderr)
sys.exit(1)
else:
vimrc_files = find_vimrc_files()
if not vimrc_files:
print("Error: Could not find vimrc file. Please specify with --vimrc-file", file=sys.stderr)
sys.exit(1)
vimrc_path = vimrc_files[0]
print(f"Found vimrc file: {vimrc_path}", file=sys.stderr)
# Parse configuration
old_config = parse_vimrc_config(vimrc_path)
# Check if already using Ruff
if old_config.get('ruff_enabled'):
print("Note: Ruff is already enabled in your configuration.", file=sys.stderr)
# Convert to Ruff config
ruff_config = convert_to_ruff_config(old_config)
# Generate output
output_lines = [
f"# Migration suggestions for {vimrc_path}",
"#",
"# Old configuration detected:",
f"# lint_checkers: {old_config.get('lint_checkers', [])}",
f"# lint_ignore: {old_config.get('lint_ignore', [])}",
f"# lint_select: {old_config.get('lint_select', [])}",
"#",
"# Recommended Ruff configuration:",
"",
]
if args.format in ('vimrc', 'both'):
output_lines.append("## VimScript Configuration (.vimrc)")
output_lines.append("")
output_lines.append(generate_vimrc_snippet(ruff_config))
if args.format in ('pyproject', 'both'):
output_lines.append("## pyproject.toml Configuration")
output_lines.append("")
output_lines.append("Add this to your pyproject.toml:")
output_lines.append("")
output_lines.append(generate_pyproject_toml(ruff_config))
output_text = '\n'.join(output_lines)
# Write output
if args.output:
args.output.write_text(output_text)
print(f"Migration suggestions written to: {args.output}", file=sys.stderr)
else:
print(output_text)
if __name__ == '__main__':
main()