From 0cac03da69dc9a2fb9cc5ccfdb4161e8c259a9e1 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Tue, 16 Oct 2018 16:27:32 -0700 Subject: [PATCH 1/3] Add option to specify local googleapis --- synthtool/gcp/gapic_generator.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/synthtool/gcp/gapic_generator.py b/synthtool/gcp/gapic_generator.py index 6d596272e..426bf6317 100644 --- a/synthtool/gcp/gapic_generator.py +++ b/synthtool/gcp/gapic_generator.py @@ -25,9 +25,6 @@ class GAPICGenerator: - def __init__(self): - self._clone_googleapis() - def py_library(self, service: str, version: str, **kwargs) -> Path: """ Generates the Python Library files using artman/GAPIC @@ -58,6 +55,7 @@ def _generate_code( language, config_path=None, artman_output_name=None, + googleapis_directory=None, private=False, ): # map the language to the artman argument and subdir of genfiles @@ -75,16 +73,21 @@ def _generate_code( gapic_language_arg, gen_language = GENERATE_FLAG_LANGUAGE[language] # Determine which googleapis repo to use - if not private: - googleapis = self.googleapis + if googleapis_directory: + googleapis = Path(googleapis_directory).expanduser() + log.debug(f"Using local googleapis at: {googleapis}") else: - googleapis = self.googleapis_private - - if googleapis is None: - raise RuntimeError( - f"Unable to generate {config_path}, the googleapis repository" - "is unavailable." - ) + self._clone_googleapis() + if not private: + googleapis = self.googleapis + else: + googleapis = self.googleapis_private + + if googleapis is None: + raise RuntimeError( + f"Unable to generate {config_path}, the googleapis repository" + "is unavailable." + ) # Run the code generator. # $ artman --config path/to/artman_api.yaml generate python_gapic @@ -99,7 +102,7 @@ def _generate_code( if not (googleapis / config_path).exists(): raise FileNotFoundError( - f"Unable to find configuration yaml file: {config_path}." + f"Unable to find configuration yaml file: {(googleapis / config_path)}." ) log.debug(f"Running generator for {config_path}.") From 8fc4a2c879ed98323c9b675ffa94b979b606131d Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 18 Oct 2018 15:15:53 -0700 Subject: [PATCH 2/3] Use environment variable SYNTHTOOL_GOOGLEAPIS --- synthtool/gcp/gapic_generator.py | 53 +++++++++++++++++--------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/synthtool/gcp/gapic_generator.py b/synthtool/gcp/gapic_generator.py index 426bf6317..f4910df87 100644 --- a/synthtool/gcp/gapic_generator.py +++ b/synthtool/gcp/gapic_generator.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os from pathlib import Path import subprocess @@ -25,6 +26,9 @@ class GAPICGenerator: + def __init__(self): + self._clone_googleapis() + def py_library(self, service: str, version: str, **kwargs) -> Path: """ Generates the Python Library files using artman/GAPIC @@ -55,7 +59,6 @@ def _generate_code( language, config_path=None, artman_output_name=None, - googleapis_directory=None, private=False, ): # map the language to the artman argument and subdir of genfiles @@ -73,21 +76,16 @@ def _generate_code( gapic_language_arg, gen_language = GENERATE_FLAG_LANGUAGE[language] # Determine which googleapis repo to use - if googleapis_directory: - googleapis = Path(googleapis_directory).expanduser() - log.debug(f"Using local googleapis at: {googleapis}") + if private and not self.local_googleapis: + googleapis = self.googleapis_private else: - self._clone_googleapis() - if not private: - googleapis = self.googleapis - else: - googleapis = self.googleapis_private - - if googleapis is None: - raise RuntimeError( - f"Unable to generate {config_path}, the googleapis repository" - "is unavailable." - ) + googleapis = self.googleapis + + if googleapis is None: + raise RuntimeError( + f"Unable to generate {config_path}, the googleapis repository" + "is unavailable." + ) # Run the code generator. # $ artman --config path/to/artman_api.yaml generate python_gapic @@ -131,13 +129,18 @@ def _generate_code( return genfiles def _clone_googleapis(self): - log.debug("Cloning googleapis.") - self.googleapis = git.clone(GOOGLEAPIS_URL, depth=1) - - try: - self.googleapis_private = git.clone(GOOGLEAPIS_PRIVATE_URL, depth=1) - except subprocess.CalledProcessError: - log.warning( - "Could not clone googleapis-private, you will not be able to " - "generate private API versions!" - ) + self.local_googleapis = 'SYNTHTOOL_GOOGLEAPIS' in os.environ + if self.local_googleapis: + self.googleapis = Path(os.environ['SYNTHTOOL_GOOGLEAPIS']).expanduser() + log.debug(f"Using local googleapis at {self.googleapis}") + else: + log.debug("Cloning googleapis.") + self.googleapis = git.clone(GOOGLEAPIS_URL, depth=1) + + try: + self.googleapis_private = git.clone(GOOGLEAPIS_PRIVATE_URL, depth=1) + except subprocess.CalledProcessError: + log.warning( + "Could not clone googleapis-private, you will not be able to " + "generate private API versions!" + ) From 724ac90e16b49295c94df882dac5f46e6df935b9 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Thu, 18 Oct 2018 16:18:29 -0700 Subject: [PATCH 3/3] blacken --- synthtool/gcp/gapic_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synthtool/gcp/gapic_generator.py b/synthtool/gcp/gapic_generator.py index f4910df87..f7ad49851 100644 --- a/synthtool/gcp/gapic_generator.py +++ b/synthtool/gcp/gapic_generator.py @@ -129,9 +129,9 @@ def _generate_code( return genfiles def _clone_googleapis(self): - self.local_googleapis = 'SYNTHTOOL_GOOGLEAPIS' in os.environ + self.local_googleapis = "SYNTHTOOL_GOOGLEAPIS" in os.environ if self.local_googleapis: - self.googleapis = Path(os.environ['SYNTHTOOL_GOOGLEAPIS']).expanduser() + self.googleapis = Path(os.environ["SYNTHTOOL_GOOGLEAPIS"]).expanduser() log.debug(f"Using local googleapis at {self.googleapis}") else: log.debug("Cloning googleapis.")