synth.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2020 Google LLC
  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. """This script is used to synthesize generated parts of this library."""
  15. import synthtool as s
  16. from synthtool.__main__ import extra_args
  17. from synthtool import log, shell
  18. from synthtool.sources import git
  19. import logging
  20. from os import path, remove
  21. from pathlib import Path
  22. import glob
  23. import json
  24. import re
  25. import sys
  26. from packaging import version
  27. logging.basicConfig(level=logging.DEBUG)
  28. VERSION_REGEX = r"([^\.]*)\.(.+)\.json$"
  29. TEMPLATE_VERSIONS = [
  30. "default",
  31. ]
  32. discovery_url = "https://github.com/googleapis/discovery-artifact-manager.git"
  33. repository = Path('.')
  34. log.debug(f"Cloning {discovery_url}.")
  35. discovery = git.clone(discovery_url)
  36. log.debug("Cleaning output directory.")
  37. shell.run("rm -rf .cache".split(), cwd=repository)
  38. log.debug("Installing dependencies.")
  39. shell.run(
  40. "python2 -m pip install -e generator/ --user".split(),
  41. cwd=repository
  42. )
  43. def generate_service(disco: str):
  44. m = re.search(VERSION_REGEX, disco)
  45. name = m.group(1)
  46. version = m.group(2)
  47. template = TEMPLATE_VERSIONS[-1] # Generate for latest version
  48. log.info(f"Generating {name} {version} ({template}).")
  49. output_dir = repository / ".cache" / name / version
  50. input_file = discovery / "discoveries" / disco
  51. command = (
  52. f"python2 -m googleapis.codegen --output_dir={output_dir}" +
  53. f" --input={input_file} --language=php --language_variant={template}" +
  54. f" --package_path=api/services"
  55. )
  56. shell.run(f"mkdir -p {output_dir}".split(), cwd=repository / "generator")
  57. shell.run(command.split(), cwd=repository, hide_output=False)
  58. s.copy(output_dir, f"src/Google/Service")
  59. def all_discoveries(skip=None, prefer=None):
  60. """Returns a map of API IDs to Discovery document filenames.
  61. Args:
  62. skip (list, optional): a list of API IDs to skip.
  63. prefer (list, optional): a list of API IDs to include.
  64. Returns:
  65. list(string): A list of Discovery document filenames.
  66. """
  67. discos = {}
  68. for file in sorted(glob.glob(str(discovery / 'discoveries/*.*.json'))):
  69. api_id = None
  70. with open(file) as api_file:
  71. api_id = json.load(api_file)['id']
  72. # If an API has already been visited, skip it.
  73. if api_id in discos:
  74. continue
  75. # Skip APIs explicitly listed in "skip" arg
  76. if skip and api_id in skip:
  77. continue
  78. discos[api_id] = path.basename(file)
  79. # Skip APIs not preferred in index.json and not listed in "prefer" arg
  80. index = {}
  81. with open(str(discovery / 'discoveries/index.json')) as file:
  82. index = json.load(file)
  83. for api in index['items']:
  84. api_id = api['id']
  85. if prefer and api_id in prefer:
  86. continue
  87. if api['preferred']:
  88. continue
  89. discos.pop(api_id, None)
  90. return discos.values()
  91. def generate_services(services):
  92. for service in services:
  93. generate_service(service)
  94. skip = ['discovery:v1', 'websecurityscanner:v1']
  95. prefer = ['admin:directory_v1', 'admin:datatransfer_v1']
  96. discoveries = all_discoveries(skip, prefer)
  97. generate_services(discoveries)