-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,44 @@ | ||
import os | ||
import subprocess | ||
from datetime import datetime | ||
|
||
# Directory containing blueprint files | ||
blueprint_directory = 'automations' | ||
# Path to the README.md file in the automations directory | ||
readme_path = 'automations/README.md' | ||
# Directory name to ignore | ||
ignore_folder = 'dev' | ||
|
||
def get_last_commit_date(file_path): | ||
"""Get the last commit date for a given file.""" | ||
result = subprocess.run(['git', 'log', '-1', '--format=%cd', '--', file_path], | ||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
# Parse the date to remove time information | ||
date = datetime.strptime(result.stdout.strip(), '%a %b %d %H:%M:%S %Y %z') | ||
return date.strftime('%Y-%m-%d') | ||
|
||
def get_blueprint_description(directory): | ||
"""Retrieve the first few lines of the README.md file for the blueprint description.""" | ||
readme_path = os.path.join(directory, 'README.md') | ||
if os.path.exists(readme_path): | ||
with open(readme_path, 'r') as readme_file: | ||
lines = readme_file.readlines() | ||
# Get the first few lines as the description | ||
description_lines = lines[:5] | ||
description = ''.join(description_lines).strip() | ||
return description | ||
return "No description available." | ||
|
||
def get_blueprints(directory, ignore_folder): | ||
"""Retrieve the list of blueprint files in the directory, ignoring specified folders.""" | ||
def update_readme(): | ||
blueprint_dir = 'automation' | ||
readme_file = os.path.join(blueprint_dir, 'README.md') | ||
blueprints = [] | ||
base_url = "https://github.com/asucrews/ha-blueprints/blob/main/" | ||
import_base_url = "https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=" | ||
|
||
for root, _, files in os.walk(directory): | ||
if ignore_folder not in root.split(os.sep): | ||
for filename in files: | ||
if filename.endswith('.yaml'): | ||
filepath = os.path.join(root, filename) | ||
name = os.path.splitext(filename)[0] | ||
formatted_name = ' '.join(word.capitalize() for word in name.split('_')) | ||
last_commit_date = get_last_commit_date(filepath) | ||
blueprint_url = f"{base_url}{filepath.replace(os.sep, '/')}" | ||
import_url = f"{import_base_url}{blueprint_url}" | ||
description = get_blueprint_description(root) | ||
blueprints.append({ | ||
"name": formatted_name, | ||
"blueprint_url": blueprint_url, | ||
"import_url": import_url, | ||
"last_commit_date": last_commit_date, | ||
"description": description | ||
}) | ||
return blueprints | ||
# Traverse the directory and gather blueprint directories containing README.md | ||
for root, dirs, files in os.walk(blueprint_dir): | ||
# Skip 'dev' folder | ||
dirs[:] = [d for d in dirs if d != 'dev'] | ||
|
||
for file in files: | ||
if file == 'README.md': | ||
blueprint_path = os.path.relpath(os.path.join(root, file), blueprint_dir) | ||
blueprint_name = os.path.basename(os.path.dirname(blueprint_path)) | ||
blueprints.append((blueprint_name, blueprint_path)) | ||
|
||
def update_readme(blueprints, readme_path): | ||
"""Update the README.md file with the list of blueprints.""" | ||
with open(readme_path, 'r') as file: | ||
lines = file.readlines() | ||
# Read the current README.md file | ||
with open(readme_file, 'r') as file: | ||
readme_lines = file.readlines() | ||
|
||
# Find the section to update | ||
start_line = None | ||
end_line = None | ||
for i, line in enumerate(lines): | ||
if line.strip() == "## Available Blueprints": | ||
start_line = i + 2 # Assume the list starts 2 lines after the header | ||
elif start_line and line.strip() == "## Usage": | ||
end_line = i | ||
break | ||
# Find the index where the Available Blueprints section starts | ||
start_index = readme_lines.index('## Available Blueprints\n') + 1 | ||
|
||
if start_line is None or end_line is None: | ||
print("Could not find the available blueprints section in README.md") | ||
return | ||
# Find the index where the Available Blueprints section ends | ||
end_index = start_index | ||
while end_index < len(readme_lines) and readme_lines[end_index].startswith('-'): | ||
end_index += 1 | ||
|
||
# Generate the new content | ||
blueprint_lines = [ | ||
f"### [{blueprint['name']}]({blueprint['blueprint_url']})\n" | ||
f"[![Import Blueprint](https://my.home-assistant.io/badges/blueprint_import.svg)]({blueprint['import_url']})\n\n" | ||
f"{blueprint['description']} (Last updated: {blueprint['last_commit_date']})\n" | ||
for blueprint in blueprints | ||
] | ||
new_content = lines[:start_line] + blueprint_lines + ["\n"] + lines[end_line:] | ||
# Generate the new content for the Available Blueprints section | ||
blueprint_lines = ['\n'] | ||
for name, path in blueprints: | ||
blueprint_lines.append(f'- [{name}](./{path})\n') | ||
|
||
# Write the updated content back to the file | ||
with open(readme_path, 'w') as file: | ||
file.writelines(new_content) | ||
# Replace the old Available Blueprints section with the new content | ||
new_readme_lines = readme_lines[:start_index] + blueprint_lines + readme_lines[end_index:] | ||
|
||
def main(): | ||
blueprints = get_blueprints(blueprint_directory, ignore_folder) | ||
update_readme(blueprints, readme_path) | ||
print("README.md updated successfully") | ||
# Write the updated content back to the README.md file | ||
with open(readme_file, 'w') as file: | ||
file.writelines(new_readme_lines) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
update_readme() |