Salome HOME
Improve doc layout
[modules/shaper.git] / doc / gui / build_index.py
1 # Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 ###
21 ## The script collects information about help documents in plugins
22 ## and prepares building of help documentation by sphinx
23 ###
24
25 import argparse
26 import os
27 import os.path as osp
28 import re
29 import shutil
30 import sys
31 from xml.dom.minidom import parse
32
33 def find_dir(src_path, cfg_file):
34     """Find a name of a directory where the given config file exists"""
35     entities = os.listdir(src_path)
36     for entity in entities:
37         path = osp.join(src_path, entity)
38         cfg_path = osp.join(path, cfg_file)
39         if not osp.isdir(path):
40             continue
41         if not osp.exists(cfg_path) and not osp.exists(cfg_path + '.in'):
42             continue
43         if osp.exists(osp.join(path, 'doc')):
44             return osp.join(path, 'doc')
45     return None
46
47 def main(src_dir, build_dir):
48     """Main function"""
49     src_path = osp.realpath(osp.join(src_dir, *[os.pardir]*2, 'src'))
50     config_file = osp.join(src_path, 'Config', 'plugins.xml.in')
51
52     ## Find accessible plugins from plugins.xml configuration file
53     processed = []
54
55     indices = []
56     tui_scripts = []
57
58     doc = parse(config_file)
59     plugins = doc.getElementsByTagName('plugin')
60     for plugin in plugins:
61         plugin_name = plugin.getAttribute('library')
62         if not plugin_name:
63             plugin_name = plugin.getAttribute('script')
64         if not plugin_name or plugin_name in processed:
65             continue
66         config = plugin.getAttribute('configuration')
67         if not config:
68             continue
69         lib_dir = find_dir(src_path, config)
70         if lib_dir:
71             ## Copy plugin's documenation folder to the build directory
72             dist_dir = osp.join(build_dir, plugin_name)
73             if osp.exists(dist_dir):
74                 shutil.rmtree(dist_dir)
75             shutil.copytree(lib_dir, dist_dir)
76             ## Collect index file
77             indices.append(osp.join(plugin_name, plugin_name + '.rst'))
78             ## Collect TUI scripts
79             tui_files = sorted(f for f in os.listdir(lib_dir) if \
80                                    osp.isfile(osp.join(lib_dir, f)) and re.match('TUI_.*\.rst', f))
81             tui_scripts += [osp.join('..', plugin_name, i) for i in tui_files]
82             ## Mark plugin as processed
83             processed.append(plugin_name)
84
85     ## Generate index file
86     in_file = osp.join(src_dir, 'index.rst.in')
87     out_file = osp.join(build_dir, 'index.rst')
88     with open(in_file, 'r') as fin, open(out_file, 'w') as fout:
89         lines = fin.readlines()
90         idx = lines.index('<insert here>\n')
91         lines = lines[:idx] + ['   {}\n'.format(i) for i in indices] + lines[idx+1:]
92         fout.writelines(lines)
93
94     ## Generate TUI scripts index
95     in_file = osp.join(src_dir, 'TUI_scripts.rst.in')
96     out_file = osp.join(build_dir, 'General', 'TUI_scripts.rst')
97     with open(in_file, 'r') as fin, open(out_file, 'w') as fout:
98         lines = fin.readlines()
99         idx = lines.index('<insert here>\n')
100         lines = lines[:idx] + ['   {}\n'.format(i) for i in tui_scripts] + lines[idx+1:]
101         fout.writelines(lines)
102
103 if __name__ == '__main__':
104     parser = argparse.ArgumentParser(description='Generate index file from source directory')
105     parser.add_argument('build_dir', help='build directory')
106     parser.add_argument('src_dir', help='source directory')
107     args = parser.parse_args()
108     sys.exit(main(args.src_dir, args.build_dir))