generate.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2020 The 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. """Generates grpc-prefixed packages using template renderer.
  15. To use this script, please use 3.7+ interpreter. This script is work-directory
  16. agnostic. A quick executable command:
  17. python3 tools/distrib/python/grpc_prefixed/generate.py
  18. """
  19. import dataclasses
  20. import datetime
  21. import logging
  22. import os
  23. import shutil
  24. import subprocess
  25. import sys
  26. import jinja2
  27. WORK_PATH = os.path.realpath(os.path.dirname(__file__))
  28. LICENSE = os.path.join(WORK_PATH, '../../../../LICENSE')
  29. BUILD_PATH = os.path.join(WORK_PATH, 'build')
  30. DIST_PATH = os.path.join(WORK_PATH, 'dist')
  31. env = jinja2.Environment(
  32. loader=jinja2.FileSystemLoader(os.path.join(WORK_PATH, 'templates')))
  33. LOGGER = logging.getLogger(__name__)
  34. POPEN_TIMEOUT_S = datetime.timedelta(minutes=1).total_seconds()
  35. @dataclasses.dataclass
  36. class PackageMeta:
  37. """Meta-info of a PyPI package."""
  38. name: str
  39. name_long: str
  40. destination_package: str
  41. version: str = '1.0.0'
  42. def clean() -> None:
  43. try:
  44. shutil.rmtree(BUILD_PATH)
  45. except FileNotFoundError:
  46. pass
  47. try:
  48. shutil.rmtree(DIST_PATH)
  49. except FileNotFoundError:
  50. pass
  51. def generate_package(meta: PackageMeta) -> None:
  52. # Makes package directory
  53. package_path = os.path.join(BUILD_PATH, meta.name)
  54. os.makedirs(package_path, exist_ok=True)
  55. # Copy license
  56. shutil.copyfile(LICENSE, os.path.join(package_path, 'LICENSE'))
  57. # Generates source code
  58. for template_name in env.list_templates():
  59. template = env.get_template(template_name)
  60. with open(
  61. os.path.join(package_path,
  62. template_name.replace('.template', '')), 'w') as f:
  63. f.write(template.render(dataclasses.asdict(meta)))
  64. # Creates wheel
  65. job = subprocess.Popen([
  66. sys.executable,
  67. os.path.join(package_path, 'setup.py'), 'sdist', '--dist-dir', DIST_PATH
  68. ],
  69. cwd=package_path,
  70. stdout=subprocess.PIPE,
  71. stderr=subprocess.STDOUT)
  72. outs, _ = job.communicate(timeout=POPEN_TIMEOUT_S)
  73. # Logs result
  74. if job.returncode != 0:
  75. LOGGER.error('Wheel creation failed with %d', job.returncode)
  76. LOGGER.error(outs)
  77. else:
  78. LOGGER.info('Package <%s> generated', meta.name)
  79. def main():
  80. clean()
  81. generate_package(
  82. PackageMeta(name='grpc',
  83. name_long='gRPC Python',
  84. destination_package='grpcio'))
  85. generate_package(
  86. PackageMeta(name='grpc-status',
  87. name_long='gRPC Rich Error Status',
  88. destination_package='grpcio-status'))
  89. generate_package(
  90. PackageMeta(name='grpc-channelz',
  91. name_long='gRPC Channel Tracing',
  92. destination_package='grpcio-channelz'))
  93. generate_package(
  94. PackageMeta(name='grpc-tools',
  95. name_long='ProtoBuf Code Generator',
  96. destination_package='grpcio-tools'))
  97. generate_package(
  98. PackageMeta(name='grpc-reflection',
  99. name_long='gRPC Reflection',
  100. destination_package='grpcio-reflection'))
  101. generate_package(
  102. PackageMeta(name='grpc-testing',
  103. name_long='gRPC Testing Utility',
  104. destination_package='grpcio-testing'))
  105. generate_package(
  106. PackageMeta(name='grpc-health-checking',
  107. name_long='gRPC Health Checking',
  108. destination_package='grpcio-health-checking'))
  109. generate_package(
  110. PackageMeta(name='grpc-csds',
  111. name_long='gRPC Client Status Discovery Service',
  112. destination_package='grpcio-csds'))
  113. generate_package(
  114. PackageMeta(name='grpc-admin',
  115. name_long='gRPC Admin Interface',
  116. destination_package='grpcio-admin'))
  117. if __name__ == "__main__":
  118. logging.basicConfig(level=logging.INFO)
  119. main()