list_api.py 2.1 KB

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