-
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
- Loading branch information
Showing
4 changed files
with
101 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,52 @@ | ||
import importlib | ||
import importlib.util | ||
import sys | ||
from pathlib import Path | ||
|
||
|
||
def update_readme_with_packages_list(): | ||
""" | ||
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" | ||
|
||
# Import the DEPENDENCY_MAPPING from the db.py file directly | ||
db_file = Path(__file__).resolve().parent.parent / "src/sync_pre_commit_lock/db.py" | ||
spec = importlib.util.spec_from_file_location("db", db_file) | ||
db = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(db) | ||
DEPENDENCY_MAPPING = db.DEPENDENCY_MAPPING | ||
|
||
# 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) | ||
|
||
|
||
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