Source/File Structure/validator

From Blender Developer Wiki
Jump to: navigation, search

This script validates the file structure located at: File Structure, by testing the paths against a Blender installation.

#!/usr/bin/env python3
"""
./validate.py --wiki=wiki.txt --source=/src/blender
"""

import os

CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))


def wiki_to_paths(wiki_text):
    file_paths = []
    for l in wiki_text.split("\n"):
        if l.startswith("| /"):
            # Convert:
            # "| /source/'''blender/'''" --> "/source/blender"
            p = l[3:].replace("'''", "").split(" ", 1)[0].rstrip("/")
            file_paths.append(p)
    return file_paths


def check_directory_structure(filename, source_dir):

    with open(filename, 'rU', encoding='utf-8') as f:
        final_paths = wiki_to_paths(f.read())

    if 1:
        print("\n"
            "Paths\n"
            "=====")
        for p in final_paths:
            print("-", p)

    if 1:
        # Check missing
        test = [p for p in final_paths
                if not os.path.exists(os.path.join(source_dir, p))]
        if test:
            print("\n"
                "Missing in Source Dir\n"
                "=====================")
            for p in test:
                print("-", p)
        else:
            print("\n"
                "Missing in Source Dir (none found)\n"
                "==================================")

    if 1:
        # Check incomplete
        basedirs = {os.path.dirname(p) for p in final_paths}
        test = []

        for base in sorted(basedirs):
            base_abs = os.path.join(source_dir, base)
            for p in os.listdir(base_abs):
                if not p.startswith("."):
                    p_abs = os.path.join(base_abs, p)
                    if os.path.isdir(p_abs):
                        p_rel = os.path.join(base, p)
                        if p_rel not in final_paths:
                            test.append(p_rel)
        if test:
            print("\n"
                "Missing Documentation\n"
                "=====================")
            for p in sorted(test):
                print("-", p)
        else:
            print("\n"
                "Missing Documentation (none found)\n"
                "==================================")


def create_parser():
    import argparse

    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument(
            "-w", "--wiki", dest="wiki_text", metavar='PATH', required=True,
            help="WIKI Filepath")

    parser.add_argument(
            "-s", "--source", dest="source_path", metavar='PATH', required=True,
            help="Blender source directory")

    return parser


if __name__ == "__main__":
    parser = create_parser()

    args = parser.parse_args()

    check_directory_structure(args.wiki_text, args.source_path)