_mako_renderer.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python3
  2. # Copyright 2015 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. """Simple Mako renderer.
  16. Just a wrapper around the mako rendering library.
  17. """
  18. import getopt
  19. import glob
  20. import importlib.util
  21. import os
  22. import pickle
  23. import shutil
  24. import sys
  25. from typing import List
  26. from mako import exceptions
  27. from mako.lookup import TemplateLookup
  28. from mako.runtime import Context
  29. from mako.template import Template
  30. import yaml
  31. PROJECT_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..",
  32. "..")
  33. # TODO(lidiz) find a better way for plugins to reference each other
  34. sys.path.append(os.path.join(PROJECT_ROOT, 'tools', 'buildgen', 'plugins'))
  35. def out(msg: str) -> None:
  36. print(msg, file=sys.stderr)
  37. def showhelp() -> None:
  38. out('mako-renderer.py [-o out] [-m cache] [-P preprocessed_input] [-d dict] [-d dict...]'
  39. ' [-t template] [-w preprocessed_output]')
  40. def render_template(template: Template, context: Context) -> None:
  41. """Render the mako template with given context.
  42. Prints an error template to indicate where and what in the template caused
  43. the render failure.
  44. """
  45. try:
  46. template.render_context(context)
  47. except:
  48. out(exceptions.text_error_template().render())
  49. raise
  50. def main(argv: List[str]) -> None:
  51. got_input = False
  52. module_directory = None
  53. preprocessed_output = None
  54. dictionary = {}
  55. json_dict = {}
  56. got_output = False
  57. output_name = None
  58. got_preprocessed_input = False
  59. output_merged = None
  60. try:
  61. opts, args = getopt.getopt(argv, 'hM:m:o:t:P:')
  62. except getopt.GetoptError:
  63. out('Unknown option')
  64. showhelp()
  65. sys.exit(2)
  66. for opt, arg in opts:
  67. if opt == '-h':
  68. out('Displaying showhelp')
  69. showhelp()
  70. sys.exit()
  71. elif opt == '-o':
  72. if got_output:
  73. out('Got more than one output')
  74. showhelp()
  75. sys.exit(3)
  76. got_output = True
  77. output_name = arg
  78. elif opt == '-m':
  79. if module_directory is not None:
  80. out('Got more than one cache directory')
  81. showhelp()
  82. sys.exit(4)
  83. module_directory = arg
  84. elif opt == '-M':
  85. if output_merged is not None:
  86. out('Got more than one output merged path')
  87. showhelp()
  88. sys.exit(5)
  89. output_merged = arg
  90. elif opt == '-P':
  91. assert not got_preprocessed_input
  92. assert json_dict == {}
  93. with open(arg, 'rb') as dict_file:
  94. dictionary = pickle.load(dict_file)
  95. got_preprocessed_input = True
  96. cleared_dir = False
  97. for arg in args:
  98. got_input = True
  99. with open(arg) as f:
  100. srcs = list(yaml.load_all(f.read(), Loader=yaml.FullLoader))
  101. for src in srcs:
  102. if isinstance(src, str):
  103. assert len(srcs) == 1
  104. template = Template(src,
  105. filename=arg,
  106. module_directory=module_directory,
  107. lookup=TemplateLookup(directories=['.']))
  108. with open(output_name, 'w') as output_file:
  109. render_template(template, Context(output_file,
  110. **dictionary))
  111. else:
  112. # we have optional control data: this template represents
  113. # a directory
  114. if not cleared_dir:
  115. if not os.path.exists(output_name):
  116. pass
  117. elif os.path.isfile(output_name):
  118. os.unlink(output_name)
  119. else:
  120. shutil.rmtree(output_name, ignore_errors=True)
  121. cleared_dir = True
  122. items = []
  123. if 'foreach' in src:
  124. for el in dictionary[src['foreach']]:
  125. if 'cond' in src:
  126. args = dict(dictionary)
  127. args['selected'] = el
  128. if not eval(src['cond'], {}, args):
  129. continue
  130. items.append(el)
  131. assert items
  132. else:
  133. items = [None]
  134. for item in items:
  135. args = dict(dictionary)
  136. args['selected'] = item
  137. item_output_name = os.path.join(
  138. output_name,
  139. Template(src['output_name']).render(**args))
  140. if not os.path.exists(os.path.dirname(item_output_name)):
  141. os.makedirs(os.path.dirname(item_output_name))
  142. template = Template(
  143. src['template'],
  144. filename=arg,
  145. module_directory=module_directory,
  146. lookup=TemplateLookup(directories=['.']))
  147. with open(item_output_name, 'w') as output_file:
  148. render_template(template, Context(output_file, **args))
  149. if not got_input and not preprocessed_output:
  150. out('Got nothing to do')
  151. showhelp()
  152. if __name__ == '__main__':
  153. main(sys.argv[1:])