blob: 360b1dcba752ca3df3ea787b7100b3a7efe1cbbf [file] [log] [blame]
Galanakis, Minos80efacd2019-07-03 15:20:20 +01001# -*- 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
21import os
22import re
23from shutil import copy2
24from glob import glob
25
26# Determine absolute paths for tf-m project and current directory
27read_the_doc_root = os.path.dirname(os.path.abspath(__file__))
28tfm_root = os.path.dirname(read_the_doc_root)
29doc_root = os.path.join(tfm_root, "docs")
30
31doc_files = []
32
33# Recursively list all files with extensions and add them
34for 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
39doc_files = filter(lambda x: read_the_doc_root not in x, doc_files)
40
41for 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
53index_f_origin = os.path.join(doc_root, "index.rst.in")
54index_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
58copy2(index_f_origin, index_f)
59
60with open(index_f, "r") as F:
61 index_data = F.read()
62 index_data = re.sub(r'@[A-Z\_]+@', "", index_data)
63
64with open(index_f, "w") as F:
65 F.write(index_data)