Team Ai
Modelpublic

AnnaMats/ppo-Pyramids-Training

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes110downloads
generate_markdown_docs.py101 linesDownload Raw Back to utils
1#!/usr/bin/env python32import os3import subprocess4import sys5import yaml6import argparse7import hashlib8 9 10# pydoc-markdown -I . -m module_name --render_toc > doc.md11 12 13def hash_file(filename):14    """15    Calculate the md5 hash of a file. Used to check for stale files.16 17    :param filename: The name of the file to check18    :type str:19    :return: A string containing the md5 hash of the file20    :rtype: str21    """22    if os.path.exists(filename):23        hasher = hashlib.md5()24        with open(filename, "rb") as file_to_hash:25            buffer = file_to_hash.read()26            hasher.update(buffer)27        return hasher.hexdigest()28    else:29        return 030 31 32def remove_trailing_whitespace(filename):33    """34    Removes trailing whitespace from a file.35 36    :param filename: The name of the file to process37    :type str:38    """39    num_changed = 040    # open the source file41    with open(filename, "rb") as f:42        source_file = f.read().decode()43    # grab all the lines, removing the trailing whitespace44    lines = [line.rstrip() for line in source_file.splitlines()]45 46    # process lines to construct destination file47    while lines and not lines[-1]:48        lines.pop(-1)49    lines.append("")50    destination_file = "\n".join(lines)51 52    # compare source and destination and write only if changed53    if source_file != destination_file:54        num_changed += 155        with open(filename, "wb") as f:56            f.write(destination_file.encode())57 58 59if __name__ == "__main__":60    """61    Pre-commit hook to generate Python API documentation using pydoc-markdown62    and write as markdown files. Each package should have a config file,63    pydoc-config.yaml, at the root level that provides configurations for64    the generation. Fails if documentation was updated, passes otherwise. This65    allows for pre-commit to fail in CI/CD and makes sure dev commits the doc66    updates.67    """68    parser = argparse.ArgumentParser()69    parser.add_argument("--package_dirs", nargs="+")70    args = parser.parse_args()71 72    ok = True73    for package_dir in args.package_dirs:74        config_path = os.path.join(os.getcwd(), package_dir, "pydoc-config.yaml")75        print(config_path)76        with open(config_path) as config_file:77            config = yaml.full_load(config_file)78            for module in config["modules"]:79                module_name = module["name"]80                submodules = module["submodules"]81                output_file_name = f"./{config['folder']}/{module['file_name']}"82                old_hash = hash_file(output_file_name)83                module_args = []84                for submodule in submodules:85                    module_args.append("-m")86                    module_args.append(f"{module_name}.{submodule}")87                with open(output_file_name, "w") as output_file:88                    subprocess_args = [89                        "pydoc-markdown",90                        "-I",91                        f"./{package_dir}",92                        *module_args,93                        "--render-toc",94                    ]95                    subprocess.check_call(subprocess_args, stdout=output_file)96                remove_trailing_whitespace(output_file_name)97                new_hash = hash_file(output_file_name)98                ok &= old_hash == new_hash99 100    sys.exit(0 if ok else 1)101