1# Copyright 2021 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 15from pathlib import Path 16import os 17import shutil 18import tempfile 19 20from synthtool.log import logger 21from synthtool.sources import git 22 23 24class Pregenerated: 25 """A synthtool component that copies pregenerated bazel code.""" 26 27 def __init__(self): 28 local_clone = os.environ.get("SYNTHTOOL_GOOGLEAPIS_GEN") 29 if local_clone: 30 self._googleapis_gen = Path(local_clone).expanduser() 31 logger.debug(f"Using local googleapis-gen at {self._googleapis_gen}") 32 else: 33 logger.debug("Cloning googleapis-gen.") 34 self._googleapis_gen = git.clone( 35 git.make_repo_clone_url("googleapis/googleapis-gen") 36 ) 37 38 def generate(self, path: str) -> Path: 39 # shutil.copytree(dirs_exist_ok=True) does not exist until python 3.8 40 tempdir = Path(tempfile.mkdtemp()) / "code" 41 42 # make a copy of the code at the provided path because autosynth 43 # may not reset the source git repository 44 shutil.copytree(self._googleapis_gen / path, tempdir) 45 return tempdir 46