diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ea128d6..c968bdf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,6 +14,15 @@ repos: - id: check-merge-conflict - id: fix-byte-order-marker + - repo: local + hooks: + - id: export-supported-packages-to-readme + name: db_md + entry: python scripts/db_md.py + language: python + verbose: true + files: ^db\.py|README\.md$ + - rev: v0.2.2 repo: https://github.com/astral-sh/ruff-pre-commit hooks: diff --git a/README.md b/README.md index 6659f18..775915b 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,32 @@ poetry sync-pre-commit Both commands support `--dry-run` and verbosity options. +## Supported packages for pre-commits + +Here is the list of default packages supported by this plugin, from [`db.py`](https://github.com/GabDug/sync-pre-commit-lock/blob/main/src/sync_pre_commit_lock/db.py). You can add more packages using the `dependency-mapping` configuration. + + + +- [autopep8](https://github.com/hhatto/autopep8) +- [bandit](https://github.com/PyCQA/bandit) +- [black](https://github.com/psf/black-pre-commit-mirror) +- [check-jsonschema](https://github.com/python-jsonschema/check-jsonschema) +- [codespell](https://github.com/codespell-project/codespell) +- [commitizen](https://github.com/commitizen-tools/commitizen) +- [djhtml](https://github.com/rtts/djhtml) +- [docformatter](https://github.com/PyCQA/docformatter) +- [flake8](https://github.com/pycqa/flake8) +- [flakeheaven](https://github.com/flakeheaven/flakeheaven) +- [isort](https://github.com/pycqa/isort) +- [mypy](https://github.com/pre-commit/mirrors-mypy) +- [pycln](https://github.com/hadialqattan/pycln) +- [pyroma](https://github.com/regebro/pyroma) +- [pyupgrade](https://github.com/asottile/pyupgrade) +- [rtscheck](https://github.com/rstcheck/rstcheck) +- [ruff](https://github.com/astral-sh/ruff-pre-commit) +- [yamllint](https://github.com/adrienverge/yamllint) + + ## Improvement ideas Feel free to open an issue or a PR if you have any idea, or if you want to help! diff --git a/scripts/db_md.py b/scripts/db_md.py new file mode 100644 index 0000000..14b73ff --- /dev/null +++ b/scripts/db_md.py @@ -0,0 +1,73 @@ +from __future__ import annotations + +import importlib +import importlib.util +import sys +from pathlib import Path +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from sync_pre_commit_lock.db import PackageRepoMapping + + +def update_readme_with_packages_list() -> None: + """ + Update the README file with a list of supported packages. + + This function reads the DEPENDENCY_MAPPING from the db.py file and generates + a markdown content with a list of packages and their corresponding repositories. + It then updates the README file by replacing the content between the start and end + comments with the generated markdown content. + + Returns: + None + """ + readme_file = Path(__file__).resolve().parent.parent / "README.md" + + DEPENDENCY_MAPPING = import_db() + + # Generate the markdown content + markdown_content = "\n" + "\n".join( + f"- [{package}]({data['repo']})" for package, data in DEPENDENCY_MAPPING.items() + ) + + # Update the README file + with readme_file.open("r+") as f: + readme_content = f.read() + start_comment = "" + end_comment = "" + start_index = readme_content.find(start_comment) + len(start_comment) + end_index = readme_content.find(end_comment) + updated_readme_content = ( + readme_content[:start_index] + "\n" + markdown_content + "\n" + readme_content[end_index:] + ) + if updated_readme_content != readme_content: + f.seek(0) + f.write(updated_readme_content) + f.truncate() + print("Supported packages list has been added to the README file.") # noqa: T201 + sys.exit(1) + + +def import_db() -> PackageRepoMapping: + """ + Imports the database module and returns the DEPENDENCY_MAPPING. + + This function imports the database module located at 'src/sync_pre_commit_lock/db.py' + and returns the DEPENDENCY_MAPPING dictionary from the imported module. + + We don't import direcly because pre-commit may not have the Python environment configured. + + Returns: + dict: The DEPENDENCY_MAPPING dictionary from the imported database module. + """ + db_file = Path(__file__).resolve().parent.parent / "src/sync_pre_commit_lock/db.py" + # Rest of the code... + spec = importlib.util.spec_from_file_location("db", db_file) + db = importlib.util.module_from_spec(spec) + spec.loader.exec_module(db) + return db.DEPENDENCY_MAPPING + + +if __name__ == "__main__": + update_readme_with_packages_list() diff --git a/src/sync_pre_commit_lock/db.py b/src/sync_pre_commit_lock/db.py index 0f1d02a..d74bb4f 100644 --- a/src/sync_pre_commit_lock/db.py +++ b/src/sync_pre_commit_lock/db.py @@ -43,6 +43,10 @@ class RepoInfo(TypedDict): "repo": "https://github.com/rtts/djhtml", "rev": "${rev}", }, + "docformatter": { + "repo": "https://github.com/PyCQA/docformatter", + "rev": "${rev}", + }, "flake8": { "repo": "https://github.com/pycqa/flake8", "rev": "${rev}", @@ -59,30 +63,26 @@ class RepoInfo(TypedDict): "repo": "https://github.com/pre-commit/mirrors-mypy", "rev": "v${rev}", }, - "pyupgrade": { - "repo": "https://github.com/asottile/pyupgrade", + "pycln": { + "repo": "https://github.com/hadialqattan/pycln", "rev": "v${rev}", }, - "ruff": { - "repo": "https://github.com/astral-sh/ruff-pre-commit", + "pyroma": { + "repo": "https://github.com/regebro/pyroma", + "rev": "${rev}", + }, + "pyupgrade": { + "repo": "https://github.com/asottile/pyupgrade", "rev": "v${rev}", }, "rtscheck": { "repo": "https://github.com/rstcheck/rstcheck", "rev": "v${rev}", }, - "pycln": { - "repo": "https://github.com/hadialqattan/pycln", + "ruff": { + "repo": "https://github.com/astral-sh/ruff-pre-commit", "rev": "v${rev}", }, - "docformatter": { - "repo": "https://github.com/PyCQA/docformatter", - "rev": "${rev}", - }, - "pyroma": { - "repo": "https://github.com/regebro/pyroma", - "rev": "${rev}", - }, "yamllint": { "repo": "https://github.com/adrienverge/yamllint", "rev": "v${rev}",