Galanakis, Minos | 80efacd | 2019-07-03 15:20:20 +0100 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | #------------------------------------------------------------------------------- |
| 3 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | #------------------------------------------------------------------------------# |
| 8 | |
| 9 | # Interface module for alligning project's Sphinx Compatibility with |
| 10 | # readthedocs service |
| 11 | |
| 12 | # |
| 13 | # This file is mimicing the behavior of tf-m/cmake/SphinxCopyDoc.cmake |
| 14 | # which when triggered by CMAKE collects all documents in a intermediate |
| 15 | # location before calling sphinx-build. |
| 16 | |
| 17 | # It can be triggered by simply importing the module will should produce a |
| 18 | # an identical output, while retaining standard Sphinx behavior. |
| 19 | |
| 20 | |
| 21 | import os |
| 22 | import re |
| 23 | from shutil import copy2 |
| 24 | from glob import glob |
| 25 | |
| 26 | # Determine absolute paths for tf-m project and current directory |
| 27 | read_the_doc_root = os.path.dirname(os.path.abspath(__file__)) |
| 28 | tfm_root = os.path.dirname(read_the_doc_root) |
| 29 | doc_root = os.path.join(tfm_root, "docs") |
| 30 | |
| 31 | doc_files = [] |
| 32 | |
| 33 | # Recursively list all files with extensions and add them |
| 34 | for ext in [".rst", ".md", ".txt", ".png", ".jpg"]: |
| 35 | doc_files.extend([f for f in glob(os.path.join(tfm_root, "**/*%s" % ext), |
| 36 | recursive=True)]) |
| 37 | |
| 38 | # Do not add files from this folder |
| 39 | doc_files = filter(lambda x: read_the_doc_root not in x, doc_files) |
| 40 | |
| 41 | for df in list(doc_files): |
| 42 | |
| 43 | # Set the target filename to be cwd + relative to root path of origin |
| 44 | target_f = df.replace(tfm_root, "").lstrip("/") |
| 45 | target_f = os.path.join(os.getcwd(), target_f) |
| 46 | # Create path for file (nested) without exception if exists |
| 47 | os.makedirs(os.path.dirname(target_f), exist_ok=True) |
| 48 | |
| 49 | # Copy the file to new location |
| 50 | print("Copying %s %s -> %s" % (df, " " * (90 - len(df)), target_f)) |
| 51 | copy2(df, target_f) |
| 52 | |
| 53 | index_f_origin = os.path.join(doc_root, "index.rst.in") |
| 54 | index_f = os.path.join(read_the_doc_root, "index.rst") |
| 55 | |
| 56 | |
| 57 | # Copy the index from docs directory and strip the CMAKE variable references |
| 58 | copy2(index_f_origin, index_f) |
| 59 | |
| 60 | with open(index_f, "r") as F: |
| 61 | index_data = F.read() |
| 62 | index_data = re.sub(r'@[A-Z\_]+@', "", index_data) |
| 63 | |
| 64 | with open(index_f, "w") as F: |
| 65 | F.write(index_data) |