Minos Galanakis | d19a19f | 2020-06-03 15:38:03 +0100 | [diff] [blame] | 1 | # ----------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | # ----------------------------------------------------------------------------- |
| 7 | |
| 8 | # Interface module for pre-processing the documentation content |
| 9 | # before sphynx-build is called |
| 10 | # |
| 11 | # It collects files from multiple sources in a intermediate location |
| 12 | # before calling sphinx-build, thus overriding the limitation |
| 13 | # of having the documentation share a common root directory. |
| 14 | # |
| 15 | # It can be triggered by simply importing the module |
| 16 | import os |
| 17 | from shutil import copy2, copytree, rmtree, move |
| 18 | from glob import glob |
| 19 | from tfm_cmake_defaults import * |
| 20 | from subprocess import call |
| 21 | |
Anton Komlev | 3356ba3 | 2022-03-31 22:02:11 +0100 | [diff] [blame^] | 22 | def copytree_overwrite(src, dest): |
| 23 | if os.path.isdir(src): |
| 24 | if not os.path.isdir(dest): |
| 25 | os.makedirs(dest) |
| 26 | files = os.listdir(src) |
| 27 | for f in files: |
| 28 | copytree_overwrite(os.path.join(src, f), |
| 29 | os.path.join(dest, f)) |
| 30 | else: |
| 31 | copy2(src, dest) |
Minos Galanakis | d19a19f | 2020-06-03 15:38:03 +0100 | [diff] [blame] | 32 | |
| 33 | def tfm_copy_files(): |
| 34 | doc_files = [] |
| 35 | |
| 36 | # Recursively list all files with extensions and add them |
| 37 | for _path in document_scan_dirs: |
| 38 | _path = os.path.abspath(os.path.join(tfm_def_root_dir, _path)) |
| 39 | for ext in document_scan_ext: |
| 40 | doc_files.extend([f for f in glob(os.path.join(_path, |
| 41 | "**/*%s" % ext), |
| 42 | recursive=True)]) |
| 43 | |
| 44 | # Add the extra files |
| 45 | for _doc_file in documents_extra: |
| 46 | _doc_file = os.path.abspath(os.path.join(tfm_def_root_dir, _doc_file)) |
| 47 | if os.path.isfile(_doc_file): |
| 48 | doc_files.append(_doc_file) |
| 49 | |
| 50 | # Clean up all files in target dir except conf.py and tfm_env.py |
| 51 | files = [f for f in |
| 52 | glob("*", recursive=False) if f not in ["conf.py", |
| 53 | tfm_def_conf_out_file, |
| 54 | os.path.basename( |
| 55 | tfm_def_doxy_output_dir), |
| 56 | "tfm_env.py"]] |
| 57 | for f in files: |
| 58 | if os.path.isfile(f): |
| 59 | os.remove(f) |
| 60 | elif os.path.isdir(f): |
| 61 | rmtree(f) |
| 62 | |
| 63 | # Copy the documentation folder as is |
Anton Komlev | 3356ba3 | 2022-03-31 22:02:11 +0100 | [diff] [blame^] | 64 | copytree_overwrite(tfm_def_doc_root, tfm_def_copy_doc_root) |
Minos Galanakis | d19a19f | 2020-06-03 15:38:03 +0100 | [diff] [blame] | 65 | |
| 66 | for df in list(doc_files): |
| 67 | # Set the target filename to be cwd + relative to root path of origin |
| 68 | target_f = os.path.relpath(df, tfm_def_root_dir) |
| 69 | target_f = os.path.join(tfm_def_copy_dir, target_f) |
| 70 | # Create path for file (nested) without exception if exists |
| 71 | os.makedirs(os.path.dirname(target_f), exist_ok=True) |
| 72 | # Copy the file to new location |
| 73 | copy2(df, target_f) |
| 74 | |
| 75 | |
| 76 | # Build Doxygen Documnetation |
| 77 | if tfm_def_build_doxygen: |
| 78 | # if conf file is not provided by cmake |
| 79 | if tfm_def_render_cmake: |
| 80 | render_cmake_file(cmake_env, |
| 81 | tfm_def_doxygen_in_file, |
| 82 | tfm_def_doxygen_out_file) |
| 83 | # Call doxygen to generate the documentation |
| 84 | doxygen_bin = find_package("doxygen") |
| 85 | call([doxygen_bin, tfm_def_doxygen_out_file]) |
| 86 | |
| 87 | # Only act if requested by defaults |
| 88 | if tfm_def_copy_files: |
| 89 | tfm_copy_files() |
| 90 | |
| 91 | if tfm_def_render_cmake: |
| 92 | # Render the conf_py file |
| 93 | render_cmake_file(cmake_env, tfm_def_conf_in_file, tfm_def_conf_out_file) |