|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +import yaml |
| 21 | + |
| 22 | + |
| 23 | +def generate_pre_commit_table(yaml_path): |
| 24 | + """ |
| 25 | + Generates a Markdown table from a pre-commit-config.yaml file. |
| 26 | + """ |
| 27 | + try: |
| 28 | + with open(yaml_path) as f: |
| 29 | + config = yaml.safe_load(f) |
| 30 | + except FileNotFoundError: |
| 31 | + return f"Error: The file '{yaml_path}' was not found." |
| 32 | + except yaml.YAMLError as e: |
| 33 | + return f"Error parsing YAML file: {e}" |
| 34 | + |
| 35 | + table_header = "| Hook ID | Language | Description | Version |\n" |
| 36 | + table_separator = "|---|---|---|---|\n" |
| 37 | + table_rows = [] |
| 38 | + |
| 39 | + for repo in config.get("repos", []): |
| 40 | + version = repo.get("rev", "N/A") |
| 41 | + url = repo.get("repo", "N/A") |
| 42 | + for hook in repo.get("hooks", []): |
| 43 | + hook_id = hook.get("id", "N/A") |
| 44 | + language = hook.get("language", "N/A") |
| 45 | + description = hook.get("description", "N/A") |
| 46 | + if description == "N/A": |
| 47 | + description = hook.get("name", "N/A") |
| 48 | + # args = ", ".join(hook.get("args", [])) if hook.get("args") else "N/A" |
| 49 | + |
| 50 | + if url not in ["local", "meta"]: |
| 51 | + entry = f"[{hook_id}]({url})" |
| 52 | + else: |
| 53 | + entry = f"{hook_id}" |
| 54 | + |
| 55 | + table_rows.append(f"| {entry} | {language} | {description} | {version} |\n") |
| 56 | + |
| 57 | + return table_header + table_separator + "".join(table_rows) |
| 58 | + |
| 59 | + |
| 60 | +def create_markdown_file(target_file_path, content_to_append): |
| 61 | + """ |
| 62 | + Creates a Markdown file with the content. |
| 63 | + """ |
| 64 | + try: |
| 65 | + with open(target_file_path, "w+") as f: |
| 66 | + f.seek(0) |
| 67 | + f.write( |
| 68 | + """<!-- |
| 69 | + Licensed to the Apache Software Foundation (ASF) under one |
| 70 | + or more contributor license agreements. See the NOTICE file |
| 71 | + distributed with this work for additional information |
| 72 | + regarding copyright ownership. The ASF licenses this file |
| 73 | + to you under the Apache License, Version 2.0 (the |
| 74 | + "License"); you may not use this file except in compliance |
| 75 | + with the License. You may obtain a copy of the License at |
| 76 | +
|
| 77 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 78 | +
|
| 79 | + Unless required by applicable law or agreed to in writing, |
| 80 | + software distributed under the License is distributed on an |
| 81 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 82 | + KIND, either express or implied. See the License for the |
| 83 | + specific language governing permissions and limitations |
| 84 | + under the License. |
| 85 | + --> |
| 86 | +
|
| 87 | +# pre-commit hook documentation |
| 88 | +
|
| 89 | + """ |
| 90 | + ) |
| 91 | + f.write(content_to_append) |
| 92 | + return f"File content successfully created at '{target_file_path}'." |
| 93 | + except OSError as e: |
| 94 | + return f"Error creating file: {e}" |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + |
| 99 | + pre_commit_yaml_path = ( |
| 100 | + ".pre-commit-config.yaml" # Assuming this file is in the same directory |
| 101 | + ) |
| 102 | + output_markdown_path = "PRE_COMMIT_HOOK_DOCS.md" |
| 103 | + |
| 104 | + # Generate the Markdown table |
| 105 | + markdown_table = generate_pre_commit_table(pre_commit_yaml_path) |
| 106 | + |
| 107 | + # Add the table to the target Markdown file |
| 108 | + if "Error" not in markdown_table: |
| 109 | + result = create_markdown_file(output_markdown_path, markdown_table) |
| 110 | + print(result) |
| 111 | + else: |
| 112 | + print(markdown_table) |
0 commit comments