Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/shaper
[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             if tui_files:
82                 tui_list_file = osp.join(dist_dir, 'TUI_examples.rst')
83                 with open(tui_list_file, 'w') as flist:
84                     flist.write('.. _tui_{}:\n\n'.format(plugin_name.lower()))
85                     title = plugin_name.replace('Plugin', ' plug-in')
86                     flist.write('{}\n'.format(title))
87                     flist.write('{}\n\n'.format('='*len(title)))
88                     flist.write('.. toctree::\n')
89                     flist.write('   :titlesonly:\n')
90                     flist.write('   :maxdepth: 1\n\n')
91                     flist.writelines(['   {}\n'.format(i) for i in tui_files])
92                 tui_scripts.append(osp.join('..', plugin_name, 'TUI_examples.rst'))
93             ## Mark plugin as processed
94             processed.append(plugin_name)
95
96     ## Generate index file
97     in_file = osp.join(src_dir, 'index.rst.in')
98     out_file = osp.join(build_dir, 'index.rst')
99     with open(in_file, 'r') as fin, open(out_file, 'w') as fout:
100         lines = fin.readlines()
101         idx = lines.index('<insert here>\n')
102         lines = lines[:idx] + ['   {}\n'.format(i) for i in indices] + lines[idx+1:]
103         fout.writelines(lines)
104
105     ## Generate TUI scripts index
106     in_file = osp.join(src_dir, 'TUI_scripts.rst.in')
107     out_file = osp.join(build_dir, 'General', 'TUI_scripts.rst')
108     with open(in_file, 'r') as fin, open(out_file, 'w') as fout:
109         lines = fin.readlines()
110         idx = lines.index('<insert here>\n')
111         lines = lines[:idx] + ['   {}\n'.format(i) for i in tui_scripts] + lines[idx+1:]
112         fout.writelines(lines)
113
114 if __name__ == '__main__':
115     parser = argparse.ArgumentParser(description='Generate index file from source directory')
116     parser.add_argument('build_dir', help='build directory')
117     parser.add_argument('src_dir', help='source directory')
118     args = parser.parse_args()
119     sys.exit(main(args.src_dir, args.build_dir))