Skip to content

Commit

Permalink
feat: add supported packages to README automatically (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabDug committed Mar 24, 2024
1 parent 1d74a40 commit 3d2fbe7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- GENERATED-PACKAGES-LIST -->
<!-- @generated by scripts/db_md.py -->
- [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)
<!-- END-GENERATED-PACKAGES-LIST -->

## Improvement ideas

Feel free to open an issue or a PR if you have any idea, or if you want to help!
Expand Down
73 changes: 73 additions & 0 deletions scripts/db_md.py
Original file line number Diff line number Diff line change
@@ -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 = "<!-- @generated by scripts/db_md.py -->\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 = "<!-- GENERATED-PACKAGES-LIST -->"
end_comment = "<!-- END-GENERATED-PACKAGES-LIST -->"
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()
28 changes: 14 additions & 14 deletions src/sync_pre_commit_lock/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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}",
Expand Down

0 comments on commit 3d2fbe7

Please sign in to comment.