list_protos.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Buildgen .proto files list plugin.
  15. This parses the list of targets from the yaml build file, and creates
  16. a list called "protos" that contains all of the proto file names.
  17. """
  18. import re
  19. def mako_plugin(dictionary):
  20. """The exported plugin code for list_protos.
  21. Some projects generators may want to get the full list of unique .proto files
  22. that are being included in a project. This code extracts all files referenced
  23. in any library or target that ends in .proto, and builds and exports that as
  24. a list called "protos".
  25. """
  26. libs = dictionary.get('libs', [])
  27. targets = dictionary.get('targets', [])
  28. proto_re = re.compile('(.*)\\.proto')
  29. protos = set()
  30. for lib in libs:
  31. for src in lib.get('src', []):
  32. m = proto_re.match(src)
  33. if m:
  34. protos.add(m.group(1))
  35. for tgt in targets:
  36. for src in tgt.get('src', []):
  37. m = proto_re.match(src)
  38. if m:
  39. protos.add(m.group(1))
  40. protos = sorted(protos)
  41. dictionary['protos'] = protos