• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python2.7
2
3# Copyright 2016 gRPC authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import collections
18import fnmatch
19import os
20import re
21import sys
22import yaml
23
24_RE_API = r'(?:GPRAPI|GRPCAPI|CENSUSAPI)([^;]*);'
25
26
27def list_c_apis(filenames):
28    for filename in filenames:
29        with open(filename, 'r') as f:
30            text = f.read()
31        for m in re.finditer(_RE_API, text):
32            api_declaration = re.sub('[ \r\n\t]+', ' ', m.group(1))
33            type_and_name, args_and_close = api_declaration.split('(', 1)
34            args = args_and_close[:args_and_close.rfind(')')].strip()
35            last_space = type_and_name.rfind(' ')
36            last_star = type_and_name.rfind('*')
37            type_end = max(last_space, last_star)
38            return_type = type_and_name[0:type_end + 1].strip()
39            name = type_and_name[type_end + 1:].strip()
40            yield {
41                'return_type': return_type,
42                'name': name,
43                'arguments': args,
44                'header': filename
45            }
46
47
48def headers_under(directory):
49    for root, dirnames, filenames in os.walk(directory):
50        for filename in fnmatch.filter(filenames, '*.h'):
51            yield os.path.join(root, filename)
52
53
54def mako_plugin(dictionary):
55    apis = []
56    headers = []
57
58    for lib in dictionary['libs']:
59        if lib['name'] in ['grpc', 'gpr']:
60            headers.extend(lib['public_headers'])
61
62    apis.extend(list_c_apis(sorted(set(headers))))
63    dictionary['c_apis'] = apis
64
65
66if __name__ == '__main__':
67    print yaml.dump([api for api in list_c_apis(headers_under('include/grpc'))])
68