-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add supported packages to README automatically (#26)
- Loading branch information
Showing
4 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters