1# Copyright 2018 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# https://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 15import importlib.util 16import os 17import sys 18from typing import List, Sequence 19 20import click 21import pkg_resources 22from synthtool.log import logger 23import synthtool.metadata 24from synthtool import preconfig 25 26try: 27 VERSION = pkg_resources.get_distribution("gcp-synthtool").version 28except pkg_resources.DistributionNotFound: 29 VERSION = "0.0.0+dev" 30 31 32_extra_args: List[str] = [] 33 34 35def extra_args() -> List[str]: 36 """Return any additional arguments specified to synthtool.""" 37 # Return a copy so these don't get modified. 38 # A little trickery. If __name__ isn't __main__, import __main__ and return 39 # its extra_args(). This ensures that both `from __main__ import extra_args()` 40 # and `from synthtool.__main__ import extra_args()` works as expected. This 41 # is needed because *technically* Python can have two copies of this module 42 # in sys.modules when it's executed as main. Weird, I know. 43 if __name__ != "__main__": # pragma: no cover 44 try: 45 import __main__ 46 47 return __main__.extra_args() 48 except AttributeError: 49 # __main__ didn't have an extra_args() attribute, so this means 50 # synthtool is not the main module. Just return what's in this 51 # module. 52 pass 53 54 return list(_extra_args) 55 56 57@click.command() 58@click.version_option(message="%(version)s", version=VERSION) 59@click.argument("synthfile", default="synth.py") 60@click.option( 61 "--metadata", 62 default="synth.metadata", 63 help="Path to metadata file that will be read and overwritten.", 64) 65@click.argument("extra_args", nargs=-1) 66def main(synthfile: str, metadata: str, extra_args: Sequence[str]): 67 f"""Synthesizes source code according to the instructions in synthfile arg. 68 69 Optional environment variables: 70 SYNTHTOOL_ARTMAN_VERSION: The version of artman to use. 71 SYNTHTOOL_GOOGLEAPIS: Path to local clone of https://github.com/googleapis/googleapis 72 SYNTHTOOL_GENERATOR: Path to local gapic-generator directory to use for generation. 73 By default, the latest version of gapic-generator will be used. 74 AUTOSYNTH_USE_SSH: Access github repos via ssh instead of https. 75 {preconfig.PRECONFIG_ENVIRONMENT_VARIABLE}: Path to a json file. 76 77 78 {preconfig.PRECONFIG_HELP} 79 """ 80 _extra_args.extend(extra_args) 81 82 synth_file = os.path.abspath(synthfile) 83 84 if os.path.lexists(synth_file): 85 logger.debug(f"Executing {synth_file}.") 86 # https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly 87 spec = importlib.util.spec_from_file_location("synth", synth_file) 88 synth_module = importlib.util.module_from_spec(spec) 89 90 if spec.loader is None: 91 raise ImportError("Could not import synth.py") 92 93 with synthtool.metadata.MetadataTrackerAndWriter(metadata): 94 spec.loader.exec_module(synth_module) # type: ignore 95 96 else: 97 logger.exception(f"{synth_file} not found.") 98 sys.exit(1) 99 100 101if __name__ == "__main__": 102 main() 103