-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdev-tasks.py
executable file
·97 lines (75 loc) · 2.56 KB
/
dev-tasks.py
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
#! /usr/bin/env -S uv run -q
#
# GT4Py - GridTools Framework
#
# Copyright (c) 2014-2024, ETH Zurich
# All rights reserved.
#
# Please, refer to the LICENSE file in the root directory.
# SPDX-License-Identifier: BSD-3-Clause
#
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "typer>=0.12.3",
# ]
# [tool.uv]
# exclude-newer = "2025-01-31T00:00:00Z"
# ///
"""Script for running recurrent development tasks."""
from __future__ import annotations
import pathlib
import subprocess
from typing import Final
import typer
ROOT_DIR: Final = pathlib.Path(__file__).parent
# -- Helpers --
def gather_versions() -> dict[str, str]:
with subprocess.Popen(
[*"uv export --frozen --no-hashes --project".split(), ROOT_DIR], stdout=subprocess.PIPE
) as proc:
return dict(
line.split("==")
for line in proc.stdout.read().decode().splitlines()
if not any(line.startswith(c) for c in ["-", "#"])
)
# -- CLI --
app = typer.Typer(no_args_is_help=True)
@app.command()
def sync_precommit() -> None:
"""Sync versions of tools used in pre-commit hooks with the project versions."""
versions = gather_versions()
# Update ruff version in pre-commit config
subprocess.run(
f"""uvx -q --from 'yamlpath' yaml-set --mustexist --change='repos[.repo%https://github.com/astral-sh/ruff-pre-commit].rev' --value='v{versions["ruff"]}' .pre-commit-config.yaml""",
cwd=ROOT_DIR,
shell=True,
check=True,
)
# Update tach version in pre-commit config
subprocess.run(
f"""uvx -q --from 'yamlpath' yaml-set --mustexist --change='repos[.repo%https://github.com/gauge-sh/tach-pre-commit].rev' --value='v{versions["tach"]}' .pre-commit-config.yaml""",
cwd=ROOT_DIR,
shell=True,
check=True,
)
# Format yaml files
subprocess.run(
f"uv run --project {ROOT_DIR} pre-commit run pretty-format-yaml --all-files", shell=True
)
@app.command()
def update_precommit() -> None:
"""Update and sync pre-commit hooks with the latest compatible versions."""
subprocess.run(f"uv run --project {ROOT_DIR} pre-commit autoupdate", shell=True)
sync_precommit()
@app.command()
def update_versions() -> None:
"""Update all project dependencies to their latest compatible versions."""
subprocess.run("uv lock --upgrade", cwd=ROOT_DIR, shell=True, check=True)
@app.command()
def update_all() -> None:
"""Update all project dependencies and pre-commit hooks."""
update_versions()
update_precommit()
if __name__ == "__main__":
app()