15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#!/usr/bin/env python
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright (c) 2014 The Chromium Authors. All rights reserved.
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# found in the LICENSE file.
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import cStringIO
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import fnmatch
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import optparse
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import os
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import re
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import sys
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)VALID_CHANNELS = ('stable', 'beta', 'dev')
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ROOT_FILE_CONTENTS = """.. _pepper_%(channel)s_index:
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles):orphan:
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles).. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)########################################
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Pepper API Reference (%(channel_title)s)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)########################################
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)This page lists the API for Pepper %(version)s. Apps that use this API can
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)run in Chrome %(version)s or higher.
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles):ref:`Pepper C API Reference <pepper_%(channel)s_c_index>`
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)===========================================================
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles):ref:`Pepper C++ API Reference <pepper_%(channel)s_cpp_index>`
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)===============================================================
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)C_FILE_CONTENTS = """.. _pepper_%(channel)s_c_index:
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles).. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)##########################################
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Pepper C API Reference (%(channel_title)s)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)##########################################
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)This page lists the C API for Pepper %(version)s. Apps that use this API can
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)run in Chrome %(version)s or higher.
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Interfaces <group___interfaces.html>`_
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=======================================
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)%(interfaces)s
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Structures <group___structs.html>`_
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)====================================
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)%(structures)s
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Functions <group___functions.html>`_
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=====================================
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Enums <group___enums.html>`_
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=============================
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Typedefs <group___typedefs.html>`_
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)===================================
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Macros <globals_defs.html>`_
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=============================
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Files
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=====
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)%(files)s
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)C_INTERFACE_WILDCARDS =  ['struct_p_p_p__*', 'struct_p_p_b__*']
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)C_STRUCT_WILDCARDS =  ['struct_p_p__*', 'union_p_p__*']
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CPP_FILE_CONTENTS = """.. _pepper_%(channel)s_cpp_index:
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles).. DO NOT EDIT! This document is auto-generated by doxygen/rst_index.py.
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)############################################
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Pepper C++ API Reference (%(channel_title)s)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)############################################
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)This page lists the C++ API for Pepper %(version)s. Apps that use this API can
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)run in Chrome %(version)s or higher.
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)`Classes <inherits.html>`_
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)==========================
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)%(classes)s
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)Files
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)=====
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)%(files)s
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CPP_CLASSES_WILDCARDS = ['classpp_1_1*.html']
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CPP_CLASSES_EXCLUDES = ['*-members*']
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FILE_WILDCARDS = ['*_8h.html']
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetName(filename):
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  filename = os.path.splitext(filename)[0]
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  out = ''
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if filename.startswith('struct_p_p_b__'):
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[7:]  # skip "struct_"
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('struct_p_p_p__'):
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[7:]  # skip "struct_"
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('struct_p_p__'):
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[7:]  # skip "struct_"
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('union_p_p__'):
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[6:]  # skip "union_"
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('classpp_1_1_'):
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[12:]
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('classpp_1_1ext_1_1_'):
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    out = 'Ext::'      # maybe 'ext::' ?
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[19:]
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('classpp_1_1internal_1_1_'):
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    out = 'Internal::' # maybe 'internal::'
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[24:]
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.startswith('structpp_1_1internal_1_1_'):
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    out = 'Internal::'
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mangle = filename[25:]
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif filename.endswith('_8h'):
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return filename[:-3].replace('__', '_') + '.h'
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  else:
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    print 'No match: ' + filename
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cap = True
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for c in mangle:
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if c == '_':
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if cap:
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        # If cap is True, we've already read one underscore. The second means
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        # that we should insert a literal underscore.
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        cap = False
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      else:
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        cap = True
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        continue
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if cap:
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      c = c.upper()
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      cap = False
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    out += c
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Strip trailing version number (e.g. PPB_Audio_1_1 -> PPB_Audio)
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return re.sub(r'_\d_\d$', '', out)
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GetPath(filepath):
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if os.path.exists(filepath):
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return filepath
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  raise OSError('Couldnt find: ' + filepath)
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def MakeReSTListFromFiles(path, matches, excludes=None):
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dir_files = os.listdir(path)
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  good_files = []
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for match in matches:
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    good_files.extend(fnmatch.filter(dir_files, match))
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if excludes:
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for exclude in excludes:
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      good_files = [filename for filename in good_files
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    if not fnmatch.fnmatch(filename, exclude)]
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  good_files.sort()
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return '\n'.join('  * `%s <%s>`_\n' % (GetName(f), f) for f in good_files)
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def MakeTitleCase(s):
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return s[0].upper() + s[1:]
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GenerateRootIndex(channel, version, out_filename):
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  channel_title = MakeTitleCase(channel)
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Use StringIO so we don't write out a partial file on error.
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output = cStringIO.StringIO()
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output.write(ROOT_FILE_CONTENTS % vars())
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  with open(out_filename, 'w') as f:
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    f.write(output.getvalue())
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GenerateCIndex(root_dir, channel, version, out_filename):
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  interfaces = MakeReSTListFromFiles(root_dir, C_INTERFACE_WILDCARDS)
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  structures = MakeReSTListFromFiles(root_dir, C_STRUCT_WILDCARDS)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  files = MakeReSTListFromFiles(root_dir, FILE_WILDCARDS)
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  channel_title = MakeTitleCase(channel)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Use StringIO so we don't write out a partial file on error.
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output = cStringIO.StringIO()
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output.write(C_FILE_CONTENTS % vars())
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  with open(out_filename, 'w') as f:
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    f.write(output.getvalue())
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def GenerateCppIndex(root_dir, channel, version, out_filename):
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  classes = MakeReSTListFromFiles(root_dir, CPP_CLASSES_WILDCARDS,
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  CPP_CLASSES_EXCLUDES)
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  files = MakeReSTListFromFiles(root_dir, FILE_WILDCARDS)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  channel_title = MakeTitleCase(channel)
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Use StringIO so we don't write out a partial file on error.
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output = cStringIO.StringIO()
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  output.write(CPP_FILE_CONTENTS % vars())
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  with open(out_filename, 'w') as f:
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    f.write(output.getvalue())
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def main(argv):
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  usage = 'Usage: %prog [options] <--root|--c|--cpp> directory'
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser = optparse.OptionParser(usage=usage)
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('--channel', help='pepper channel (stable, beta, dev)')
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('--version', help='pepper version (e.g. 32, 33, 34, etc.)')
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('--root', help='Generate root API index',
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    action='store_true', default=False)
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('--c', help='Generate C API index', action='store_true',
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    default=False)
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('--cpp', help='Generate C++ API index', action='store_true',
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    default=False)
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('-o', '--output', help='output file.')
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  options, files = parser.parse_args(argv)
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if len(files) != 1:
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    parser.error('Expected one directory')
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if not options.output:
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    parser.error('Need output file')
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if options.channel not in VALID_CHANNELS:
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    parser.error('Expected channel to be one of %s' % ', '.join(VALID_CHANNELS))
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if sum((options.c, options.cpp, options.root)) != 1:
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    parser.error('Exactly one of --c/--cpp/--root flags is required.')
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  root_dir = files[0]
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if options.c:
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GenerateCIndex(root_dir, options.channel, options.version, options.output)
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif options.cpp:
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GenerateCppIndex(root_dir, options.channel, options.version, options.output)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  elif options.root:
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GenerateRootIndex(options.channel, options.version, options.output)
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  else:
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    assert(False)
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return 0
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)if __name__ == '__main__':
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  try:
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    rtn = main(sys.argv[1:])
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  except KeyboardInterrupt:
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__))
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    rtn = 1
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  sys.exit(rtn)
257