OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import cStringIO |
| 7 import fnmatch |
| 8 import optparse |
| 9 import os |
| 10 import sys |
| 11 |
| 12 VALID_CHANNELS = ('stable', 'beta', 'dev') |
| 13 |
| 14 ROOT_FILE_CONTENTS = """.. _pepper_%(channel)s_index: |
| 15 |
| 16 :orphan: |
| 17 |
| 18 .. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py. |
| 19 |
| 20 ######################################## |
| 21 Pepper API Reference (%(channel_title)s) |
| 22 ######################################## |
| 23 |
| 24 :ref:`Pepper C API Reference <pepper_%(channel)s_c_index>` |
| 25 =========================================================== |
| 26 |
| 27 :ref:`Pepper C++ API Reference <pepper_%(channel)s_cpp_index>` |
| 28 =============================================================== |
| 29 |
| 30 """ |
| 31 |
| 32 C_FILE_CONTENTS = """.. _pepper_%(channel)s_c_index: |
| 33 |
| 34 .. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py. |
| 35 |
| 36 ########################################## |
| 37 Pepper C API Reference (%(channel_title)s) |
| 38 ########################################## |
| 39 |
| 40 `Interfaces <group___interfaces.html>`_ |
| 41 ======================================= |
| 42 %(interfaces)s |
| 43 |
| 44 `Structures <group___structs.html>`_ |
| 45 ==================================== |
| 46 %(structures)s |
| 47 |
| 48 `Functions <group___functions.html>`_ |
| 49 ===================================== |
| 50 |
| 51 `Enums <group___enums.html>`_ |
| 52 ============================= |
| 53 |
| 54 `Typedefs <group___typedefs.html>`_ |
| 55 =================================== |
| 56 |
| 57 `Macros <globals_defs.html>`_ |
| 58 ============================= |
| 59 |
| 60 Files |
| 61 ===== |
| 62 %(files)s |
| 63 """ |
| 64 |
| 65 C_INTERFACE_WILDCARDS = ['struct_p_p_p__*', 'struct_p_p_b__*'] |
| 66 |
| 67 C_STRUCT_WILDCARDS = ['struct_p_p__*', 'union_p_p__*'] |
| 68 |
| 69 CPP_FILE_CONTENTS = """.. _pepper_%(channel)s_cpp_index: |
| 70 |
| 71 .. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py. |
| 72 |
| 73 ############################################ |
| 74 Pepper C++ API Reference (%(channel_title)s) |
| 75 ############################################ |
| 76 |
| 77 `Classes <inherits.html>`_ |
| 78 ========================== |
| 79 %(classes)s |
| 80 |
| 81 Files |
| 82 ===== |
| 83 %(files)s |
| 84 """ |
| 85 |
| 86 CPP_CLASSES_WILDCARDS = ['classpp_1_1*.html'] |
| 87 CPP_CLASSES_EXCLUDES = ['*-members*'] |
| 88 |
| 89 FILE_WILDCARDS = ['*_8h.html'] |
| 90 |
| 91 |
| 92 def GetName(filename): |
| 93 filename = os.path.splitext(filename)[0] |
| 94 out = '' |
| 95 if filename.startswith('struct_p_p_b__'): |
| 96 mangle = filename[7:] # skip "struct_" |
| 97 elif filename.startswith('struct_p_p_p__'): |
| 98 mangle = filename[7:] # skip "struct_" |
| 99 elif filename.startswith('struct_p_p__'): |
| 100 mangle = filename[7:] # skip "struct_" |
| 101 elif filename.startswith('union_p_p__'): |
| 102 mangle = filename[6:] # skip "union_" |
| 103 elif filename.startswith('classpp_1_1_'): |
| 104 mangle = filename[12:] |
| 105 elif filename.startswith('classpp_1_1ext_1_1_'): |
| 106 out = 'Ext::' # maybe 'ext::' ? |
| 107 mangle = filename[19:] |
| 108 elif filename.startswith('classpp_1_1internal_1_1_'): |
| 109 out = 'Internal::' # maybe 'internal::' |
| 110 mangle = filename[24:] |
| 111 elif filename.startswith('structpp_1_1internal_1_1_'): |
| 112 out = 'Internal::' |
| 113 mangle = filename[25:] |
| 114 elif filename.endswith('_8h'): |
| 115 return filename[:-3].replace('__', '_') + '.h' |
| 116 else: |
| 117 print 'No match: ' + filename |
| 118 cap = True |
| 119 for c in mangle: |
| 120 if c == '_': |
| 121 if cap: |
| 122 # If cap is True, we've already read one underscore. The second means |
| 123 # that we should insert a literal underscore. |
| 124 cap = False |
| 125 else: |
| 126 cap = True |
| 127 continue |
| 128 if cap: |
| 129 c = c.upper() |
| 130 cap = False |
| 131 out += c |
| 132 return out |
| 133 |
| 134 |
| 135 def GetPath(filepath): |
| 136 if os.path.exists(filepath): |
| 137 return filepath |
| 138 raise OSError('Couldnt find: ' + filepath) |
| 139 |
| 140 |
| 141 def MakeReSTListFromFiles(path, matches, excludes=None): |
| 142 dir_files = os.listdir(path) |
| 143 good_files = [] |
| 144 for match in matches: |
| 145 good_files.extend(fnmatch.filter(dir_files, match)) |
| 146 |
| 147 if excludes: |
| 148 for exclude in excludes: |
| 149 good_files = [filename for filename in good_files |
| 150 if not fnmatch.fnmatch(filename, exclude)] |
| 151 |
| 152 good_files.sort() |
| 153 return '\n'.join(' * `%s <%s>`_\n' % (GetName(f), f) for f in good_files) |
| 154 |
| 155 |
| 156 def MakeTitleCase(s): |
| 157 return s[0].upper() + s[1:] |
| 158 |
| 159 |
| 160 def GenerateRootIndex(channel, out_filename): |
| 161 channel_title = MakeTitleCase(channel) |
| 162 |
| 163 # Use StringIO so we don't write out a partial file on error. |
| 164 output = cStringIO.StringIO() |
| 165 output.write(ROOT_FILE_CONTENTS % vars()) |
| 166 |
| 167 with open(out_filename, 'w') as f: |
| 168 f.write(output.getvalue()) |
| 169 |
| 170 |
| 171 def GenerateCIndex(root_dir, channel, out_filename): |
| 172 interfaces = MakeReSTListFromFiles(root_dir, C_INTERFACE_WILDCARDS) |
| 173 structures = MakeReSTListFromFiles(root_dir, C_STRUCT_WILDCARDS) |
| 174 files = MakeReSTListFromFiles(root_dir, FILE_WILDCARDS) |
| 175 channel_title = MakeTitleCase(channel) |
| 176 |
| 177 # Use StringIO so we don't write out a partial file on error. |
| 178 output = cStringIO.StringIO() |
| 179 output.write(C_FILE_CONTENTS % vars()) |
| 180 |
| 181 with open(out_filename, 'w') as f: |
| 182 f.write(output.getvalue()) |
| 183 |
| 184 |
| 185 def GenerateCppIndex(root_dir, channel, out_filename): |
| 186 classes = MakeReSTListFromFiles(root_dir, CPP_CLASSES_WILDCARDS, |
| 187 CPP_CLASSES_EXCLUDES) |
| 188 files = MakeReSTListFromFiles(root_dir, FILE_WILDCARDS) |
| 189 channel_title = MakeTitleCase(channel) |
| 190 |
| 191 # Use StringIO so we don't write out a partial file on error. |
| 192 output = cStringIO.StringIO() |
| 193 output.write(CPP_FILE_CONTENTS % vars()) |
| 194 |
| 195 with open(out_filename, 'w') as f: |
| 196 f.write(output.getvalue()) |
| 197 |
| 198 |
| 199 def main(argv): |
| 200 usage = 'Usage: %prog [options] <--root|--c|--cpp> directory' |
| 201 parser = optparse.OptionParser(usage=usage) |
| 202 parser.add_option('--channel', help='pepper channel (stable, beta, dev)') |
| 203 parser.add_option('--root', help='Generate root API index', |
| 204 action='store_true', default=False) |
| 205 parser.add_option('--c', help='Generate C API index', action='store_true', |
| 206 default=False) |
| 207 parser.add_option('--cpp', help='Generate C++ API index', action='store_true', |
| 208 default=False) |
| 209 parser.add_option('-o', '--output', help='output file.') |
| 210 options, files = parser.parse_args(argv) |
| 211 |
| 212 if len(files) != 1: |
| 213 parser.error('Expected one directory') |
| 214 |
| 215 if not options.output: |
| 216 parser.error('Need output file') |
| 217 |
| 218 if options.channel not in VALID_CHANNELS: |
| 219 parser.error('Expected channel to be one of %s' % ', '.join(VALID_CHANNELS)) |
| 220 |
| 221 if sum((options.c, options.cpp, options.root)) != 1: |
| 222 parser.error('Exactly one of --c/--cpp/--root flags is required.') |
| 223 |
| 224 root_dir = files[0] |
| 225 |
| 226 if options.c: |
| 227 GenerateCIndex(root_dir, options.channel, options.output) |
| 228 elif options.cpp: |
| 229 GenerateCppIndex(root_dir, options.channel, options.output) |
| 230 elif options.root: |
| 231 GenerateRootIndex(options.channel, options.output) |
| 232 else: |
| 233 assert(False) |
| 234 return 0 |
| 235 |
| 236 |
| 237 if __name__ == '__main__': |
| 238 try: |
| 239 rtn = main(sys.argv[1:]) |
| 240 except KeyboardInterrupt: |
| 241 sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__)) |
| 242 rtn = 1 |
| 243 sys.exit(rtn) |
OLD | NEW |