|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import yaml |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import List |
| 7 | +from urllib.parse import urlparse, urlunparse |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class PolicyMetadata: |
| 11 | + dir: str |
| 12 | + version: str |
| 13 | + description: str |
| 14 | + path: str |
| 15 | + |
| 16 | +def find_policy_yaml(start_dir=".") -> List[str]: |
| 17 | + """Recursively search for files named 'Policy.yaml'.""" |
| 18 | + matches = [] |
| 19 | + for root, _, files in os.walk(start_dir): |
| 20 | + if "Policy.yaml" in files: |
| 21 | + matches.append(os.path.join(root, "Policy.yaml")) |
| 22 | + return matches |
| 23 | + |
| 24 | +def load_policy_metadata(file_path: str) -> PolicyMetadata: |
| 25 | + """Load a Policy.yaml file and unmarshal it into a PolicyMetadata object.""" |
| 26 | + with open(file_path, "r", encoding="utf-8") as f: |
| 27 | + data = yaml.safe_load(f) or {} |
| 28 | + |
| 29 | + dir_name = os.path.dirname(file_path) |
| 30 | + return PolicyMetadata( |
| 31 | + dir=data.get("dir", dir_name), |
| 32 | + version=data.get("version", ""), |
| 33 | + description=data.get("description", ""), |
| 34 | + path=file_path, |
| 35 | + ) |
| 36 | + |
| 37 | +def generate_markdown_table(policies: List[PolicyMetadata]) -> str: |
| 38 | + """Generate a Markdown table from a list of PolicyMetadata objects.""" |
| 39 | + header = "| URL | Version | Description | Link |\n" |
| 40 | + separator = "|------------|----------|-----------|---------| \n" |
| 41 | + rows = [] |
| 42 | + for p in policies: |
| 43 | + description = p.description.replace("\n", " ").strip() |
| 44 | + ghcr_path = f"ghcr.io/updatecli/policies/{os.path.basename(p.path)}" |
| 45 | + readme_url = replace_filename_in_url(f"https://github.com/updatecli/policies/tree/main/{p.path}", "README.md") |
| 46 | + rows.append(f"| `{ghcr_path or '-'}` | {p.version or '-'} | {description or '-'} | {f"[link]({readme_url})" } |") |
| 47 | + return header + separator + "\n".join(rows) |
| 48 | + |
| 49 | +def replace_filename_in_url(url: str, new_filename: str) -> str: |
| 50 | + # Parse the URL |
| 51 | + parsed = urlparse(url) |
| 52 | + |
| 53 | + # Split the path and replace the last part with the new filename |
| 54 | + path_parts = parsed.path.split('/') |
| 55 | + path_parts[-1] = new_filename |
| 56 | + new_path = '/'.join(path_parts) |
| 57 | + |
| 58 | + # Rebuild the URL with the new path |
| 59 | + new_url = urlunparse(parsed._replace(path=new_path)) |
| 60 | + return new_url |
| 61 | + |
| 62 | +# Example usage |
| 63 | +original_url = "https://github.com/updatecli/policies/blob/main/updatecli/policies/file/Policy.yaml" |
| 64 | +new_url = replace_filename_in_url(original_url, "Readme.md") |
| 65 | + |
| 66 | +def main(): |
| 67 | + policies = [] |
| 68 | + for policy_file in find_policy_yaml("."): |
| 69 | + try: |
| 70 | + metadata = load_policy_metadata(policy_file) |
| 71 | + policies.append(metadata) |
| 72 | + except Exception as e: |
| 73 | + print(f"⚠️ Error parsing {policy_file}: {e}") |
| 74 | + |
| 75 | + if not policies: |
| 76 | + print("No Policy.yaml files found.") |
| 77 | + return |
| 78 | + |
| 79 | + markdown = generate_markdown_table(policies) |
| 80 | + |
| 81 | + with open("POLICIES.md", "w", encoding="utf-8") as f: |
| 82 | + f.write(markdown) |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
0 commit comments