| 1 |
#! /usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <[email protected]> |
| 5 |
# |
| 6 |
# SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
| 8 |
import argparse |
| 9 |
import os |
| 10 |
import logging |
| 11 |
import shutil |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
import tempfile |
| 15 |
import time |
| 16 |
|
| 17 |
from kapidox import utils |
| 18 |
|
| 19 |
DESCRIPTION = """ |
| 20 |
Generate Graphviz dot files for one or all frameworks. |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
def generate_dot(fw_dir, fw_name, output_dir): |
| 25 |
"""Calls cmake to generate the dot file for a framework. |
| 26 |
|
| 27 |
Returns true on success, false on failure""" |
| 28 |
dot_path = os.path.join(output_dir, fw_name + ".dot") |
| 29 |
build_dir = tempfile.mkdtemp(prefix="depdiagram-prepare-build-") |
| 30 |
try: |
| 31 |
ret = subprocess.call(["cmake", fw_dir, f"--graphviz={dot_path}"], |
| 32 |
stdout=open("/dev/null", "w"), |
| 33 |
cwd=build_dir) |
| 34 |
if ret != 0: |
| 35 |
if os.path.exists(dot_path): |
| 36 |
os.remove(dot_path) |
| 37 |
logging.error(f"Generating dot file for {fw_name} failed.") |
| 38 |
return False |
| 39 |
# Add a timestamp and version info to help with diagnostics |
| 40 |
with open(dot_path, "a") as f: |
| 41 |
f.write(f"\n# Generated on {time.ctime()}\n") |
| 42 |
version = utils.get_kapidox_version() |
| 43 |
if version: |
| 44 |
f.write(f"# By {sys.argv[0]} {version}\n") |
| 45 |
finally: |
| 46 |
shutil.rmtree(build_dir) |
| 47 |
return True |
| 48 |
|
| 49 |
|
| 50 |
def prepare_one(fw_dir, output_dir): |
| 51 |
fw_name = utils.parse_fancyname(fw_dir) |
| 52 |
if fw_name is None: |
| 53 |
return False |
| 54 |
|
| 55 |
yaml_path = os.path.join(fw_dir, "metainfo.yaml") |
| 56 |
if not os.path.exists(yaml_path): |
| 57 |
logging.error(f"'{fw_dir}' is not a framework: '{yaml_path}' does not exist.") |
| 58 |
return False |
| 59 |
|
| 60 |
if not os.path.exists(output_dir): |
| 61 |
os.makedirs(output_dir) |
| 62 |
|
| 63 |
if not generate_dot(fw_dir, fw_name, output_dir): |
| 64 |
return False |
| 65 |
shutil.copyfile(yaml_path, os.path.join(output_dir, fw_name + ".yaml")) |
| 66 |
return True |
| 67 |
|
| 68 |
|
| 69 |
def prepare_all(fw_base_dir, dot_dir): |
| 70 |
"""Generate dot files for all frameworks. |
| 71 |
|
| 72 |
Looks for frameworks in `fw_base_dir`. Output the dot files in sub dirs of |
| 73 |
`dot_dir`. |
| 74 |
""" |
| 75 |
lst = os.listdir(fw_base_dir) |
| 76 |
fails = [] |
| 77 |
for idx, fw_name in enumerate(lst): |
| 78 |
fw_dir = os.path.join(fw_base_dir, fw_name) |
| 79 |
if not os.path.isdir(fw_dir): |
| 80 |
continue |
| 81 |
|
| 82 |
progress = int(100 * (idx + 1) / len(lst)) |
| 83 |
print(f'{progress}% {fw_name}') |
| 84 |
if not prepare_one(fw_dir, dot_dir): |
| 85 |
fails.append(fw_name) |
| 86 |
return fails |
| 87 |
|
| 88 |
|
| 89 |
def main(): |
| 90 |
parser = argparse.ArgumentParser(description=DESCRIPTION) |
| 91 |
|
| 92 |
group = parser.add_mutually_exclusive_group(required=True) |
| 93 |
group.add_argument("-s", "--single", |
| 94 |
help="Generate dot files for the framework stored in DIR", |
| 95 |
metavar="DIR") |
| 96 |
group.add_argument("-a", "--all", |
| 97 |
help="Generate dot files for all frameworks whose dir is in BASE_DIR", |
| 98 |
metavar="BASE_DIR") |
| 99 |
parser.add_argument("dot_dir", |
| 100 |
help="Destination dir where dot files will be generated") |
| 101 |
|
| 102 |
args = parser.parse_args() |
| 103 |
|
| 104 |
dot_dir = os.path.abspath(args.dot_dir) |
| 105 |
|
| 106 |
if args.single: |
| 107 |
fw_dir = os.path.abspath(args.single) |
| 108 |
if prepare_one(fw_dir, dot_dir): |
| 109 |
return 0 |
| 110 |
else: |
| 111 |
return 1 |
| 112 |
else: |
| 113 |
fw_base_dir = os.path.abspath(args.all) |
| 114 |
fails = prepare_all(fw_base_dir, dot_dir) |
| 115 |
if fails: |
| 116 |
logging.error(f"{len(fails)} framework(s) failed: {', '.join(fails)}") |
| 117 |
return 1 |
| 118 |
else: |
| 119 |
return 0 |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == "__main__": |
| 123 |
sys.exit(main()) |