-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_dev_dependencies.py
More file actions
executable file
·452 lines (360 loc) · 14.9 KB
/
sync_dev_dependencies.py
File metadata and controls
executable file
·452 lines (360 loc) · 14.9 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
"""Sync dev tool version pins from autofix-versions.env to pyproject.toml.
This script updates the [project.optional-dependencies] dev section in pyproject.toml
to use the pinned versions from the central autofix-versions.env file.
It handles both exact pins (==) and minimum version pins (>=) in pyproject.toml,
converting them to exact pins for reproducibility.
If no dev dependencies section exists, it can create one with --create-if-missing.
Usage:
python sync_dev_dependencies.py --check # Verify versions match
python sync_dev_dependencies.py --apply # Update pyproject.toml
python sync_dev_dependencies.py --apply --create-if-missing # Create dev deps if missing
python sync_dev_dependencies.py --apply # Syncs requirements.lock automatically if it exists
"""
from __future__ import annotations
import argparse
import re
import sys
from pathlib import Path
# Default paths (can be overridden for testing)
PIN_FILE = Path(".github/workflows/autofix-versions.env")
PYPROJECT_FILE = Path("pyproject.toml")
LOCKFILE_FILE = Path("requirements.lock")
# Map env file keys to package names
# Format: ENV_KEY -> (package_name, optional_alternative_names)
TOOL_MAPPING: dict[str, tuple[str, ...]] = {
"RUFF_VERSION": ("ruff",),
"BLACK_VERSION": ("black",),
"ISORT_VERSION": ("isort",),
"MYPY_VERSION": ("mypy",),
"PYTEST_VERSION": ("pytest",),
"PYTEST_COV_VERSION": ("pytest-cov",),
"PYTEST_XDIST_VERSION": ("pytest-xdist",),
"COVERAGE_VERSION": ("coverage",),
"DOCFORMATTER_VERSION": ("docformatter",),
"HYPOTHESIS_VERSION": ("hypothesis",),
}
# Core dev tools to include when creating a new dev section
# (subset of TOOL_MAPPING - only the most essential ones)
CORE_DEV_TOOLS = [
"RUFF_VERSION",
"MYPY_VERSION",
"PYTEST_VERSION",
"PYTEST_COV_VERSION",
]
LOCKFILE_PATTERN = re.compile(
r"^(?P<lead>\s*)(?P<name>[A-Za-z0-9_.-]+)==(?P<version>[^\s#]+)(?P<trail>\s*(?:#.*)?)$"
)
def parse_env_file(path: Path) -> dict[str, str]:
"""Parse the autofix-versions.env file into a dict of key=value pairs."""
if not path.exists():
print(f"Warning: Pin file '{path}' not found, skipping version sync")
return {}
values: dict[str, str] = {}
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
values[key.strip()] = value.strip()
return values
def find_dev_dependencies_section(content: str) -> tuple[int, int, str] | None:
"""Find the dev dependencies section in pyproject.toml.
Returns (start_index, end_index, section_content) or None if not found.
"""
# Look for [project.optional-dependencies] section with dev = [...]
# Handle both inline and multi-line formats
# Pattern for multi-line dev dependencies
pattern = re.compile(r"^dev\s*=\s*\[\s*\n(.*?)\n\s*\]", re.MULTILINE | re.DOTALL)
match = pattern.search(content)
if match:
return match.start(), match.end(), match.group(0)
# Try inline format: dev = ["pkg1", "pkg2"]
inline_pattern = re.compile(r"^dev\s*=\s*\[(.*?)\]", re.MULTILINE)
match = inline_pattern.search(content)
if match:
return match.start(), match.end(), match.group(0)
return None
def find_optional_dependencies_section(content: str) -> int | None:
"""Find the [project.optional-dependencies] section header.
Returns the index after the section header, or None if not found.
"""
pattern = re.compile(r"^\[project\.optional-dependencies\]\s*$", re.MULTILINE)
match = pattern.search(content)
if match:
return match.end()
return None
def find_project_section_end(content: str) -> int | None:
"""Find a good place to insert [project.optional-dependencies].
Returns the index after the [project] section ends (before next section).
"""
# Find [project] section
project_match = re.search(r"^\[project\]\s*$", content, re.MULTILINE)
if not project_match:
return None
# Find the next section header after [project]
next_section = re.search(r"^\[", content[project_match.end() :], re.MULTILINE)
if next_section:
return project_match.end() + next_section.start()
# No next section, return end of content
return len(content)
def create_dev_dependencies_section(pins: dict[str, str], use_exact_pins: bool = True) -> str:
"""Create a new dev dependencies section with core tools."""
op = "==" if use_exact_pins else ">="
deps = []
for env_key in CORE_DEV_TOOLS:
if env_key in pins:
pkg_name = TOOL_MAPPING[env_key][0]
version = pins[env_key]
deps.append(f' "{pkg_name}{op}{version}",')
if not deps:
return ""
return "dev = [\n" + "\n".join(deps) + "\n]"
def extract_dependencies(section: str) -> list[tuple[str, str, str]]:
"""Extract dependencies from a dev section.
Returns list of (package_name, operator, version) tuples.
"""
deps = []
# Match patterns like "package>=1.0.0" or "package==1.0.0" or just "package"
# Be precise: package name followed by optional version specifier
pattern = re.compile(r'"([a-zA-Z0-9_-]+)(?:(>=|==|~=|>|<|<=|!=)([^"\[\]]+))?(?:\[.*?\])?"')
for match in pattern.finditer(section):
package = match.group(1)
operator = match.group(2) or ""
version = match.group(3) or ""
deps.append((package, operator, version))
return deps
def update_dependency_in_section(
section: str, package: str, new_version: str, use_exact_pin: bool = True
) -> tuple[str, bool]:
"""Update a single dependency version within a section.
IMPORTANT: This only updates exact package name matches, not partial matches.
For example, "pytest" will NOT match "pytest-cov" or "pytest-xdist".
Returns (new_section, was_changed).
"""
# Pattern to match EXACT package name with any version specifier
# The key is using word boundaries and ensuring we match the exact package
# Pattern: "package" or "package>=version" or "package[extras]>=version"
# We need to be careful not to match "pytest" when looking at "pytest-cov"
# Match: "package" + optional version spec, NOT followed by more pkg name chars
# The negative lookahead (?!-) ensures we don't match "pytest" in "pytest-cov"
pattern = re.compile(
rf'"({re.escape(package)})(?![-\w])(>=|==|~=|>|<|<=|!=)?([^"\[\]]*)?(\[.*?\])?"',
re.IGNORECASE,
)
def replacer(m: re.Match) -> str:
pkg_name = m.group(1)
extras = m.group(4) or ""
op = "==" if use_exact_pin else ">="
return f'"{pkg_name}{op}{new_version}{extras}"'
new_section, count = pattern.subn(replacer, section)
return new_section, count > 0
def sync_pyproject(
pyproject_path: Path,
pins: dict[str, str],
apply: bool = False,
use_exact_pins: bool = True,
create_if_missing: bool = False,
) -> tuple[list[str], list[str]]:
"""Sync versions from pin file to pyproject.toml.
Returns (changes_made, errors).
"""
changes: list[str] = []
errors: list[str] = []
# Read pyproject.toml
if not pyproject_path.exists():
return [], [f"pyproject.toml not found at {pyproject_path}"]
content = pyproject_path.read_text(encoding="utf-8")
original_content = content
# Find dev section
section_info = find_dev_dependencies_section(content)
if not section_info:
if not create_if_missing:
return [], ["No dev dependencies section found in pyproject.toml"]
# Create new dev dependencies section
new_section = create_dev_dependencies_section(pins, use_exact_pins)
if not new_section:
return [], ["Could not create dev dependencies section - no pins available"]
# Find where to insert
opt_deps_pos = find_optional_dependencies_section(content)
if opt_deps_pos is not None:
# Add after [project.optional-dependencies] header
content = content[:opt_deps_pos] + "\n" + new_section + "\n" + content[opt_deps_pos:]
else:
# Need to add [project.optional-dependencies] section
insert_pos = find_project_section_end(content)
if insert_pos is None:
return [], ["Could not find [project] section to add optional-dependencies"]
section_to_add = "\n[project.optional-dependencies]\n" + new_section + "\n"
content = content[:insert_pos] + section_to_add + content[insert_pos:]
op = "==" if use_exact_pins else ">="
for env_key in CORE_DEV_TOOLS:
if env_key in pins:
pkg_name = TOOL_MAPPING[env_key][0]
changes.append(f"{pkg_name}: (new) -> {op}{pins[env_key]}")
if apply:
pyproject_path.write_text(content, encoding="utf-8")
return changes, errors
# Extract the section boundaries and content
section_start, section_end, section = section_info
# Extract current dependencies from the section
current_deps = extract_dependencies(section)
current_packages = {pkg.lower(): (pkg, op, ver) for pkg, op, ver in current_deps}
# Work on a copy of just the section
new_section = section
# Check each pinned tool
for env_key, package_names in TOOL_MAPPING.items():
if env_key not in pins:
continue
target_version = pins[env_key]
# Find if any of the package names exist in current deps
for pkg_name in package_names:
pkg_lower = pkg_name.lower()
if pkg_lower in current_packages:
actual_pkg, current_op, current_ver = current_packages[pkg_lower]
# Check if version differs
if current_ver != target_version:
new_section, changed = update_dependency_in_section(
new_section, actual_pkg, target_version, use_exact_pins
)
if changed:
op = "==" if use_exact_pins else ">="
changes.append(
f"{actual_pkg}: {current_op}{current_ver} -> {op}{target_version}"
)
break
# Replace the section in the full content
if new_section != section:
content = content[:section_start] + new_section + content[section_end:]
# Apply changes if requested
if apply and content != original_content:
pyproject_path.write_text(content, encoding="utf-8")
return changes, errors
def _build_lockfile_targets(pins: dict[str, str]) -> dict[str, str]:
targets: dict[str, str] = {}
for env_key, package_names in TOOL_MAPPING.items():
if env_key not in pins:
continue
for name in package_names:
targets[name.lower()] = pins[env_key]
return targets
def sync_lockfile(
lockfile_path: Path, pins: dict[str, str], apply: bool = False
) -> tuple[list[str], list[str]]:
"""Sync versions from pin file to requirements.lock."""
if not lockfile_path.exists():
return [], []
content = lockfile_path.read_text(encoding="utf-8")
lines = content.splitlines()
targets = _build_lockfile_targets(pins)
changes: list[str] = []
updated_lines: list[str] = []
for line in lines:
match = LOCKFILE_PATTERN.match(line)
if not match:
updated_lines.append(line)
continue
name = match.group("name")
version = match.group("version")
target_version = targets.get(name.lower())
if target_version and version != target_version:
changes.append(f"requirements.lock:{name}: {version} -> =={target_version}")
if apply:
updated_lines.append(
f"{match.group('lead')}{name}=={target_version}{match.group('trail')}"
)
else:
updated_lines.append(line)
else:
updated_lines.append(line)
if apply:
new_content = "\n".join(updated_lines)
if content.endswith("\n"):
new_content += "\n"
if new_content != content:
lockfile_path.write_text(new_content, encoding="utf-8")
return changes, []
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Sync dev dependency versions from autofix-versions.env to pyproject.toml"
)
parser.add_argument(
"--check",
action="store_true",
help="Check if versions are in sync (exit 1 if not)",
)
parser.add_argument(
"--apply",
action="store_true",
help="Apply version updates to pyproject.toml",
)
parser.add_argument(
"--create-if-missing",
action="store_true",
help="Create dev dependencies section if it doesn't exist",
)
parser.add_argument(
"--use-minimum-pins",
action="store_true",
help="Use >= instead of == for version pins",
)
parser.add_argument(
"--lockfile",
action="store_true",
help="Force lockfile sync even if requirements.lock doesn't exist (no-op)",
)
parser.add_argument(
"--pin-file",
type=Path,
default=PIN_FILE,
help="Path to the version pins file",
)
parser.add_argument(
"--pyproject",
type=Path,
default=PYPROJECT_FILE,
help="Path to pyproject.toml",
)
args = parser.parse_args(argv)
if args.check and args.apply:
parser.error("--check and --apply are mutually exclusive")
if not args.check and not args.apply:
args.check = True # Default to check mode
use_exact_pins = not args.use_minimum_pins
pins = parse_env_file(args.pin_file)
if not pins:
print("Error: No pins found in env file", file=sys.stderr)
return 2
changes, errors = sync_pyproject(
args.pyproject,
pins,
apply=args.apply,
use_exact_pins=use_exact_pins,
create_if_missing=args.create_if_missing,
)
lockfile_enabled = args.lockfile or LOCKFILE_FILE.exists()
if lockfile_enabled:
lock_changes, lock_errors = sync_lockfile(LOCKFILE_FILE, pins, apply=args.apply)
changes.extend(lock_changes)
errors.extend(lock_errors)
if errors:
for err in errors:
print(f"Error: {err}", file=sys.stderr)
return 2
if changes:
print(f"{'Applied' if args.apply else 'Found'} {len(changes)} version updates:")
for change in changes:
print(f" - {change}")
if args.check:
print("\nRun with --apply to update dependency files")
return 1
else:
print("\n✓ Dependency files updated")
return 0
else:
print("✓ All dev dependency versions are in sync")
return 0
if __name__ == "__main__":
sys.exit(main())