diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ffd5b7..2b3c4bd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: - "-l DEBUG" - "-g dev" repo: https://github.com/phi-friday/sync-uv-pre-commit - rev: v0.6.3 + rev: v0.6.4 - hooks: - id: ruff diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 57b508f..136aceb 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,8 +1,8 @@ - id: sync-uv-pre-commit name: Sync uv and pre commit description: Sync uv and pre commit - language: python - entry: "sync_uv_pre_commit" + language: script + entry: "uv run script.py" minimum_pre_commit_version: "3.5.0" require_serial: true always_run: true \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index e8aca45..3592db6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ classifiers = [ "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: CPython", ] -scripts = { sync_uv_pre_commit = "sync_uv_pre_commit.cli:main" } +scripts = { generate_cli_script = "sync_uv_pre_commit.render:generate_script" } dependencies = [ "packaging", "pre-commit>=3.5.0", @@ -23,6 +23,7 @@ dependencies = [ dev = [ "ruff==0.8.4", "poethepoet>=0.27.0", + "jinja2>=3.1.5", ] [tool.uv] diff --git a/script.py b/script.py new file mode 100644 index 0000000..bfadcf1 --- /dev/null +++ b/script.py @@ -0,0 +1,23 @@ +# /// script +# requires-python = ">=3.13" +# dependencies = [ +# "packaging", +# "pre-commit>=3.5.0", +# "tomlkit>=0.13.2", +# "typing-extensions>=4.12.2", +# ] +# /// + +from __future__ import annotations + +import sys +from pathlib import Path + +WORKSPACE = Path(__file__).parent +sys.path.append(str(WORKSPACE / "src")) + + +if __name__ == "__main__": + from sync_uv_pre_commit.cli import main + + main() diff --git a/src/sync_uv_pre_commit/cli.py b/src/sync_uv_pre_commit/cli.py index e1a0ee4..13f9b53 100644 --- a/src/sync_uv_pre_commit/cli.py +++ b/src/sync_uv_pre_commit/cli.py @@ -227,6 +227,7 @@ def check_uv_version() -> None: sys.exit(ExitCode.PARSING) sys.exit(ExitCode.UNKNOWN) + logger.info("python version: %s", sys.version_info) version = process.stdout.strip().split()[1] logger.info("uv version: %s", version) diff --git a/src/sync_uv_pre_commit/export.py b/src/sync_uv_pre_commit/export.py new file mode 100644 index 0000000..0931a0e --- /dev/null +++ b/src/sync_uv_pre_commit/export.py @@ -0,0 +1,13 @@ +from __future__ import annotations + +import sys +from pathlib import Path + +WORKSPACE = Path(__file__).parent +sys.path.append(str(WORKSPACE / "src")) + + +if __name__ == "__main__": + from sync_uv_pre_commit.cli import main + + main() diff --git a/src/sync_uv_pre_commit/export.py.j2 b/src/sync_uv_pre_commit/export.py.j2 new file mode 100644 index 0000000..215d319 --- /dev/null +++ b/src/sync_uv_pre_commit/export.py.j2 @@ -0,0 +1,10 @@ +# /// script +# requires-python = "{{ requires_python }}" +# dependencies = [ +{% for dependency in dependencies -%} +# "{{ dependency }}", +{% endfor -%} +# ] +# /// + +{{ export_script }} \ No newline at end of file diff --git a/src/sync_uv_pre_commit/render.py b/src/sync_uv_pre_commit/render.py new file mode 100644 index 0000000..7527bcd --- /dev/null +++ b/src/sync_uv_pre_commit/render.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +from pathlib import Path + +import jinja2 + +from sync_uv_pre_commit.toml import read_pyproject + + +def generate_script() -> None: + """Generate the script.py file.""" + pyproject_file = Path(__file__).parent.parent.parent / "pyproject.toml" + export_template_file = Path(__file__).parent / "export.py.j2" + export_script_file = Path(__file__).parent / "export.py" + output = Path(__file__).parent.parent.parent / "script.py" + + pyproject = read_pyproject(pyproject_file) + with export_template_file.open("r") as f: + template = jinja2.Template(f.read()) + with export_script_file.open("r") as f: + export_script = f.read() + + script = template.render( + requires_python=">=3.13", + dependencies=pyproject["project"]["dependencies"], + export_script=export_script, + ) + with output.open("w+") as f: + f.write(script)