-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruff_rev_sync.py
More file actions
51 lines (40 loc) · 1.73 KB
/
ruff_rev_sync.py
File metadata and controls
51 lines (40 loc) · 1.73 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
from pathlib import Path
from re import search, sub
def main():
project_root = Path.cwd()
uv_lock_path = project_root / "uv.lock"
pre_commit_config_path = project_root / ".pre-commit-config.yaml"
if not uv_lock_path.exists():
print("uv.lock not found.")
return
if not pre_commit_config_path.exists():
print(".pre-commit-config.yaml not found.")
return
# Read uv.lock to find ruff version
uv_lock_content = uv_lock_path.read_text(encoding="utf-8")
# Look for the ruff package block
# [[package]]
# name = "ruff"
# version = "0.14.4"
pattern = r'name = "ruff"\nversion = "([^"]+)"'
matched = search(pattern, uv_lock_content)
if not matched:
print("ruff version not found in uv.lock")
return
ruff_version = matched.group(1)
print(f"Found ruff version in uv.lock: {ruff_version}")
# Read .pre-commit-config.yaml
config_content = pre_commit_config_path.read_text(encoding="utf-8")
# Pattern to find the ruff-pre-commit repo and its rev
# We look for the repo url, then capture the rev line following it
# We use a non-greedy match for content between repo and rev
pattern = r"(repo: https://github\.com/astral-sh/ruff-pre-commit\s+rev: )v?[\d.]+"
if search(pattern, config_content):
new_content = sub(pattern, f"\\1v{ruff_version}", config_content)
if new_content != config_content:
pre_commit_config_path.write_text(new_content, encoding="utf-8")
print(f"Updated .pre-commit-config.yaml ruff rev to v{ruff_version}")
else:
print(".pre-commit-config.yaml is already up to date.")
else:
print("ruff-pre-commit repo not found in .pre-commit-config.yaml")