From c0af3abd4ce565dcc4e4e533ccfe78d574616d03 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Mon, 14 Jun 2021 00:24:56 +0000 Subject: [PATCH 01/19] chore(python): autogenerate docs/index.rst --- .../templates/python_library/docs/index.rst | 38 +++++++++++++++ synthtool/languages/python.py | 33 ++++++++++++- tests/test_python_library.py | 46 +++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 synthtool/gcp/templates/python_library/docs/index.rst diff --git a/synthtool/gcp/templates/python_library/docs/index.rst b/synthtool/gcp/templates/python_library/docs/index.rst new file mode 100644 index 000000000..5eb0dec7a --- /dev/null +++ b/synthtool/gcp/templates/python_library/docs/index.rst @@ -0,0 +1,38 @@ +.. include:: README.rst + +.. include:: multiprocessing.rst +{% if default_version %} +{% if versions|length > 1 %} +This package includes clients for multiple versions of {{ metadata['repo']['name_pretty'] }}. +By default, you will get version ``{{ default_version }}``. +{% endif %} +{% endif %} +{% for version in versions %} +API Reference +------------- +.. toctree:: + :maxdepth: 2 + + {{ version }}/services + {{ version }}/types +{% endfor %} +{%- if migration_guide_version %} +Migration Guide +--------------- + +See the guide below for instructions on migrating to the {{ migration_guide_version }} release of this library. + +.. toctree:: + :maxdepth: 2 + + UPGRADING +{% endif %} +Changelog +--------- + +For a list of all ``{{ metadata['repo']['distribution_name'] }}`` releases: + +.. toctree:: + :maxdepth: 2 + + changelog diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index ff4c467e2..c43e6f4c8 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import re import sys from pathlib import Path -from typing import Any, Dict +from typing import Any, Dict, List, Optional import yaml @@ -93,6 +94,36 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict: return sample_metadata +def detect_versions( + path: str = "./src", default_version: Optional[str] = None +) -> List[str]: + """ + Detects the versions a library has, based on distinct folders + within path. This is based on the fact that our GAPIC libraries are + structured as follows: + + src/v1 + src/v1beta + src/v1alpha + + With folder names mapping directly to versions. + + Returns: a list of the subdirectories; for the example above: + ['v1', 'v1alpha', 'v1beta'] + If specified, the default_version is guaranteed to be listed last. + Otherwise, the list is sorted alphabetically. + """ + versions = [] + if os.path.isdir(path): + for directory in os.listdir(path): + if os.path.isdir(os.path.join(path, directory)): + versions.append(directory) + versions.sort() + if default_version is not None: + versions = [v for v in versions if v != default_version] + [default_version] + return versions + + def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None: """ Find all samples projects and render templates. diff --git a/tests/test_python_library.py b/tests/test_python_library.py index 04fdb9a12..b3f462c60 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -16,9 +16,11 @@ from pathlib import Path import pytest +import tempfile from synthtool import gcp from synthtool.sources import templates +from synthtool.languages import python from . import util @@ -126,3 +128,47 @@ def test_split_system_tests(): with open(templated_files / ".kokoro/presubmit/system-3.8.cfg", "r") as f: contents = f.read() assert "system-3.8" in contents + + +def test_detect_versions_src(): + temp_dir = Path(tempfile.mkdtemp()) + src_dir = temp_dir / "src" + for v in ("v1", "v2", "v3"): + os.makedirs(src_dir / v) + + with util.chdir(temp_dir): + versions = python.detect_versions() + assert ["v1", "v2", "v3"] == versions + + +def test_detect_versions_staging(): + temp_dir = Path(tempfile.mkdtemp()) + staging_dir = temp_dir / "owl-bot-staging" + for v in ("v1", "v2", "v3"): + os.makedirs(staging_dir / v) + + versions = python.detect_versions(staging_dir) + assert ["v1", "v2", "v3"] == versions + + +def test_detect_versions_dir_not_found(): + temp_dir = Path(tempfile.mkdtemp()) + + versions = python.detect_versions(temp_dir / "does-not-exist") + assert [] == versions + + +def test_detect_versions_with_default(): + temp_dir = Path(tempfile.mkdtemp()) + src_dir = temp_dir / "src" + vs = ("v1", "v2", "v3") + for v in vs: + os.makedirs(src_dir / v) + + with util.chdir(temp_dir): + versions = python.detect_versions(default_version="v1") + assert ["v2", "v3", "v1"] == versions + versions = python.detect_versions(default_version="v2") + assert ["v1", "v3", "v2"] == versions + versions = python.detect_versions(default_version="v3") + assert ["v1", "v2", "v3"] == versions From bdee7a88e47056e692be62de7b1f22ea2aaedc68 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 29 Sep 2021 15:53:26 +0000 Subject: [PATCH 02/19] set default path to owl-bot-staging --- synthtool/languages/python.py | 8 ++++---- tests/test_python_library.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index c43e6f4c8..8cba5ba4b 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -95,16 +95,16 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict: def detect_versions( - path: str = "./src", default_version: Optional[str] = None + path: str = "./owl-bot-staging", default_version: Optional[str] = None ) -> List[str]: """ Detects the versions a library has, based on distinct folders within path. This is based on the fact that our GAPIC libraries are structured as follows: - src/v1 - src/v1beta - src/v1alpha + owl-bot-staging/v1 + owl-bot-staging/v1beta + owl-bot-staging/v1alpha With folder names mapping directly to versions. diff --git a/tests/test_python_library.py b/tests/test_python_library.py index b3f462c60..4bd12dfa7 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -137,18 +137,19 @@ def test_detect_versions_src(): os.makedirs(src_dir / v) with util.chdir(temp_dir): - versions = python.detect_versions() + versions = python.detect_versions(src_dir) assert ["v1", "v2", "v3"] == versions -def test_detect_versions_staging(): +def test_detect_versions_default_path(): temp_dir = Path(tempfile.mkdtemp()) staging_dir = temp_dir / "owl-bot-staging" for v in ("v1", "v2", "v3"): os.makedirs(staging_dir / v) - versions = python.detect_versions(staging_dir) - assert ["v1", "v2", "v3"] == versions + with util.chdir(temp_dir): + versions = python.detect_versions() + assert ["v1", "v2", "v3"] == versions def test_detect_versions_dir_not_found(): @@ -160,10 +161,9 @@ def test_detect_versions_dir_not_found(): def test_detect_versions_with_default(): temp_dir = Path(tempfile.mkdtemp()) - src_dir = temp_dir / "src" - vs = ("v1", "v2", "v3") - for v in vs: - os.makedirs(src_dir / v) + staging_dir = temp_dir / "owl-bot-staging" + for v in ("v1", "v2", "v3"): + os.makedirs(staging_dir / v) with util.chdir(temp_dir): versions = python.detect_versions(default_version="v1") From fb23307ccc88cc41f1dde056aeea6fd1632748fe Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 29 Sep 2021 16:18:07 +0000 Subject: [PATCH 03/19] optimize code using glob --- synthtool/languages/python.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index 77c19afbd..78819ce39 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import glob import os import re import sys @@ -113,12 +114,7 @@ def detect_versions( If specified, the default_version is guaranteed to be listed last. Otherwise, the list is sorted alphabetically. """ - versions = [] - if os.path.isdir(path): - for directory in os.listdir(path): - if os.path.isdir(os.path.join(path, directory)): - versions.append(directory) - versions.sort() + versions = sorted([p.name for p in Path(path).glob("**/*v[1-9]*")]) if default_version is not None: versions = [v for v in versions if v != default_version] + [default_version] return versions From 89e2bc43056196d5c537d8ac5298c269c0c52eb6 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 29 Sep 2021 16:23:06 +0000 Subject: [PATCH 04/19] move the default_version to the front of the list --- synthtool/languages/python.py | 4 ++-- tests/test_python_library.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index 78819ce39..7840ae313 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -111,12 +111,12 @@ def detect_versions( Returns: a list of the subdirectories; for the example above: ['v1', 'v1alpha', 'v1beta'] - If specified, the default_version is guaranteed to be listed last. + If specified, the default_version is guaranteed to be listed first. Otherwise, the list is sorted alphabetically. """ versions = sorted([p.name for p in Path(path).glob("**/*v[1-9]*")]) if default_version is not None: - versions = [v for v in versions if v != default_version] + [default_version] + versions = [default_version] + [v for v in versions if v != default_version] return versions diff --git a/tests/test_python_library.py b/tests/test_python_library.py index 660ed253c..bc6cc28aa 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -167,8 +167,8 @@ def test_detect_versions_with_default(): with util.chdir(temp_dir): versions = python.detect_versions(default_version="v1") - assert ["v2", "v3", "v1"] == versions + assert ["v1", "v2", "v3"] == versions versions = python.detect_versions(default_version="v2") - assert ["v1", "v3", "v2"] == versions + assert ["v2", "v1", "v3"] == versions versions = python.detect_versions(default_version="v3") - assert ["v1", "v2", "v3"] == versions + assert ["v3", "v1", "v2"] == versions From 9a6f82ffac0dc752a664fcaf52f23d84dac8fc8f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 29 Sep 2021 16:55:21 +0000 Subject: [PATCH 05/19] Drop redundant default_version argument --- .../templates/python_library/docs/index.rst | 4 +-- synthtool/languages/python.py | 12 +++---- tests/test_python_library.py | 34 +++++++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/synthtool/gcp/templates/python_library/docs/index.rst b/synthtool/gcp/templates/python_library/docs/index.rst index 5eb0dec7a..03b24c37f 100644 --- a/synthtool/gcp/templates/python_library/docs/index.rst +++ b/synthtool/gcp/templates/python_library/docs/index.rst @@ -1,11 +1,9 @@ .. include:: README.rst .. include:: multiprocessing.rst -{% if default_version %} {% if versions|length > 1 %} This package includes clients for multiple versions of {{ metadata['repo']['name_pretty'] }}. -By default, you will get version ``{{ default_version }}``. -{% endif %} +By default, you will get version ``{{ versions | first }}``. {% endif %} {% for version in versions %} API Reference diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index 7840ae313..db179e3ae 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -96,25 +96,25 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict: def detect_versions( - path: str = "./owl-bot-staging", default_version: Optional[str] = None + path: str = "./google/cloud", default_version: Optional[str] = None ) -> List[str]: """ Detects the versions a library has, based on distinct folders within path. This is based on the fact that our GAPIC libraries are structured as follows: - owl-bot-staging/v1 - owl-bot-staging/v1beta - owl-bot-staging/v1alpha + google/cloud/*_v1 + google/cloud/*_v1beta + google/cloud/*_v1alpha With folder names mapping directly to versions. Returns: a list of the subdirectories; for the example above: - ['v1', 'v1alpha', 'v1beta'] + ['*_v1', '*_v1alpha', '*_v1beta'] If specified, the default_version is guaranteed to be listed first. Otherwise, the list is sorted alphabetically. """ - versions = sorted([p.name for p in Path(path).glob("**/*v[1-9]*")]) + versions = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) if default_version is not None: versions = [default_version] + [v for v in versions if v != default_version] return versions diff --git a/tests/test_python_library.py b/tests/test_python_library.py index bc6cc28aa..22757be6c 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -130,26 +130,26 @@ def test_split_system_tests(): assert "system-3.8" in contents -def test_detect_versions_src(): +def test_detect_versions_non_default_path(): temp_dir = Path(tempfile.mkdtemp()) src_dir = temp_dir / "src" - for v in ("v1", "v2", "v3"): + for v in ("api_v1", "api_v2", "api_v3"): os.makedirs(src_dir / v) with util.chdir(temp_dir): versions = python.detect_versions(src_dir) - assert ["v1", "v2", "v3"] == versions + assert ["api_v1", "api_v2", "api_v3"] == versions def test_detect_versions_default_path(): temp_dir = Path(tempfile.mkdtemp()) - staging_dir = temp_dir / "owl-bot-staging" - for v in ("v1", "v2", "v3"): - os.makedirs(staging_dir / v) + default_dir = temp_dir / "google/cloud" + for v in ("api_v1", "api_v2", "api_v3"): + os.makedirs(default_dir / v) with util.chdir(temp_dir): versions = python.detect_versions() - assert ["v1", "v2", "v3"] == versions + assert ["api_v1", "api_v2", "api_v3"] == versions def test_detect_versions_dir_not_found(): @@ -159,16 +159,16 @@ def test_detect_versions_dir_not_found(): assert [] == versions -def test_detect_versions_with_default(): +def test_detect_versions_with_default_version(): temp_dir = Path(tempfile.mkdtemp()) - staging_dir = temp_dir / "owl-bot-staging" - for v in ("v1", "v2", "v3"): - os.makedirs(staging_dir / v) + default_dir = temp_dir / "google/cloud" + for v in ("api_v1", "api_v2", "api_v3"): + os.makedirs(default_dir / v) with util.chdir(temp_dir): - versions = python.detect_versions(default_version="v1") - assert ["v1", "v2", "v3"] == versions - versions = python.detect_versions(default_version="v2") - assert ["v2", "v1", "v3"] == versions - versions = python.detect_versions(default_version="v3") - assert ["v3", "v1", "v2"] == versions + versions = python.detect_versions(default_version="api_v1") + assert ["api_v1", "api_v2", "api_v3"] == versions + versions = python.detect_versions(default_version="api_v2") + assert ["api_v2", "api_v1", "api_v3"] == versions + versions = python.detect_versions(default_version="api_v3") + assert ["api_v3", "api_v1", "api_v2"] == versions From 52541cc4cab8ce65d64555485b57cecbfb24d957 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 29 Sep 2021 17:11:18 +0000 Subject: [PATCH 06/19] remove unused imports --- synthtool/languages/python.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index db179e3ae..c51553bb8 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import glob -import os import re import sys from pathlib import Path From 3fe0d5bb634bf06f8fe2646a0d181853cadf9369 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 5 Oct 2021 14:55:16 +0000 Subject: [PATCH 07/19] read default_version from .repo-metadata.json --- synthtool/languages/python.py | 16 ++++++++++------ tests/test_python_library.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index c51553bb8..e4d39f403 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import re import sys from pathlib import Path @@ -93,9 +94,7 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict: return sample_metadata -def detect_versions( - path: str = "./google/cloud", default_version: Optional[str] = None -) -> List[str]: +def detect_versions(path: str = "./google/cloud") -> List[str]: """ Detects the versions a library has, based on distinct folders within path. This is based on the fact that our GAPIC libraries are @@ -109,11 +108,16 @@ def detect_versions( Returns: a list of the subdirectories; for the example above: ['*_v1', '*_v1alpha', '*_v1beta'] - If specified, the default_version is guaranteed to be listed first. - Otherwise, the list is sorted alphabetically. + The default_version specified in .repo-metadata.json is + guaranteed to be listed first. Otherwise, the list is + sorted alphabetically. """ + + default_version = json.load(open(".repo-metadata.json", "rt")).get( + "default_version" + ) versions = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) - if default_version is not None: + if versions: versions = [default_version] + [v for v in versions if v != default_version] return versions diff --git a/tests/test_python_library.py b/tests/test_python_library.py index 22757be6c..37f69249f 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import os from pathlib import Path @@ -136,6 +137,11 @@ def test_detect_versions_non_default_path(): for v in ("api_v1", "api_v2", "api_v3"): os.makedirs(src_dir / v) + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + with util.chdir(temp_dir): versions = python.detect_versions(src_dir) assert ["api_v1", "api_v2", "api_v3"] == versions @@ -147,6 +153,11 @@ def test_detect_versions_default_path(): for v in ("api_v1", "api_v2", "api_v3"): os.makedirs(default_dir / v) + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + with util.chdir(temp_dir): versions = python.detect_versions() assert ["api_v1", "api_v2", "api_v3"] == versions @@ -155,6 +166,10 @@ def test_detect_versions_default_path(): def test_detect_versions_dir_not_found(): temp_dir = Path(tempfile.mkdtemp()) + test_json = {"default_version": "api_v1"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions(temp_dir / "does-not-exist") assert [] == versions @@ -166,9 +181,23 @@ def test_detect_versions_with_default_version(): os.makedirs(default_dir / v) with util.chdir(temp_dir): - versions = python.detect_versions(default_version="api_v1") + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions() assert ["api_v1", "api_v2", "api_v3"] == versions - versions = python.detect_versions(default_version="api_v2") + + # Set default_version to "api_v2" + test_json = {"default_version": "api_v2"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions() assert ["api_v2", "api_v1", "api_v3"] == versions - versions = python.detect_versions(default_version="api_v3") + + # Set default_version to "api_v3" + test_json = {"default_version": "api_v3"} + with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions() assert ["api_v3", "api_v1", "api_v2"] == versions From 83bd511adabe3d941d9edc39e93e25b1a2f8c23f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 5 Oct 2021 15:20:35 +0000 Subject: [PATCH 08/19] exclude docs/index.rst if default_version is not specified --- synthtool/gcp/common.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 1f54ad3f4..50dcf8fd7 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -237,6 +237,18 @@ def py_library(self, **kwargs) -> Path: if "samples" not in kwargs: self.excludes += ["samples/AUTHORING_GUIDE.md", "samples/CONTRIBUTING.md"] + # Get the value of `default_version` from `.repo-metadata.json` + try: + default_version = json.load(open(".repo-metadata.json", "rt")).get( + "default_version" + ) + except FileNotFoundError: + default_version = "" + + # Exclude `docs/index.rst` if `default_version` is not specified. + if not default_version: + self.excludes += ["docs/index.rst"] + ret = self._generic_library("python_library", **kwargs) # If split_system_tests is set to True, we disable the system From da5be6cb088f237d387dcfb187313ca3aa6326cf Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 5 Oct 2021 15:24:14 +0000 Subject: [PATCH 09/19] check for versions kwarg --- synthtool/gcp/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 50dcf8fd7..a4108e5bc 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -245,8 +245,9 @@ def py_library(self, **kwargs) -> Path: except FileNotFoundError: default_version = "" - # Exclude `docs/index.rst` if `default_version` is not specified. - if not default_version: + # Exclude `docs/index.rst` if `default_version` is not specified + # or `versions` is not given. + if not default_version or "versions" not in kwargs: self.excludes += ["docs/index.rst"] ret = self._generic_library("python_library", **kwargs) From cf8a2bc38b32c0bfcede8fcfaef651c9b39c44e4 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 6 Oct 2021 15:15:26 +0000 Subject: [PATCH 10/19] only generate docs/index.rst if default_version is specified --- synthtool/gcp/common.py | 23 +++++++++++------------ synthtool/languages/python.py | 28 +++++++++++++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index a4108e5bc..9ea9827c7 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -209,7 +209,15 @@ def py_library(self, **kwargs) -> Path: # kwargs["metadata"] is required to load values from .repo-metadata.json if "metadata" not in kwargs: kwargs["metadata"] = {} - # rename variable to accomodate existing synth.py files + + # load common repo meta information (metadata that's not language specific). + self._load_generic_metadata(kwargs["metadata"]) + + # initialize default_version if it doesn't exist in kwargs["metadata"]['repo'] + if "default_version" not in kwargs["metadata"]['repo']: + kwargs["metadata"]['repo']["default_version"] = "" + + # rename variable to accommodate existing owlbot.py files if "system_test_dependencies" in kwargs: kwargs["system_test_local_dependencies"] = kwargs[ "system_test_dependencies" @@ -237,17 +245,8 @@ def py_library(self, **kwargs) -> Path: if "samples" not in kwargs: self.excludes += ["samples/AUTHORING_GUIDE.md", "samples/CONTRIBUTING.md"] - # Get the value of `default_version` from `.repo-metadata.json` - try: - default_version = json.load(open(".repo-metadata.json", "rt")).get( - "default_version" - ) - except FileNotFoundError: - default_version = "" - - # Exclude `docs/index.rst` if `default_version` is not specified - # or `versions` is not given. - if not default_version or "versions" not in kwargs: + # Don't add `docs/index.rst` if `versions` is not provided or `default_version` is empty + if "versions" not in kwargs or not kwargs["metadata"]["repo"]["default_version"]: self.excludes += ["docs/index.rst"] ret = self._generic_library("python_library", **kwargs) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index e4d39f403..f1d571635 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -108,17 +108,27 @@ def detect_versions(path: str = "./google/cloud") -> List[str]: Returns: a list of the subdirectories; for the example above: ['*_v1', '*_v1alpha', '*_v1beta'] - The default_version specified in .repo-metadata.json is - guaranteed to be listed first. Otherwise, the list is - sorted alphabetically. + The subdirectory with the same suffix as the default_version + specified in .repo-metadata.json will be first in the list. The + remaining elements will be sorted alphabetically. """ - default_version = json.load(open(".repo-metadata.json", "rt")).get( - "default_version" - ) - versions = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) - if versions: - versions = [default_version] + [v for v in versions if v != default_version] + versions = [] + + # Get the default_version from .repo-metadata.json + default_version = json.load(open(".repo-metadata.json", "rt")).get("default_version") + + # Sort the sub directories alphabetically + sub_dirs = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) + + if default_version and sub_dirs: + # The subdirectory with the same suffix as the default_version + # specified in .repo-metadata.json will be the default client. + default_client = next(iter([d for d in sub_dirs if d.endswith(default_version)]), None) + if default_client: + # The default_client will be first in the list. + # The remaining elements will be sorted alphabetically. + versions = [default_client] + [d for d in sub_dirs if not d.endswith(default_version)] return versions From 55e74f8562b075656e02389519bf8c2d415c3db4 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 6 Oct 2021 15:44:36 +0000 Subject: [PATCH 11/19] lint --- synthtool/gcp/common.py | 9 ++++++--- synthtool/languages/python.py | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 9ea9827c7..abbce4ee8 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -214,8 +214,8 @@ def py_library(self, **kwargs) -> Path: self._load_generic_metadata(kwargs["metadata"]) # initialize default_version if it doesn't exist in kwargs["metadata"]['repo'] - if "default_version" not in kwargs["metadata"]['repo']: - kwargs["metadata"]['repo']["default_version"] = "" + if "default_version" not in kwargs["metadata"]["repo"]: + kwargs["metadata"]["repo"]["default_version"] = "" # rename variable to accommodate existing owlbot.py files if "system_test_dependencies" in kwargs: @@ -246,7 +246,10 @@ def py_library(self, **kwargs) -> Path: self.excludes += ["samples/AUTHORING_GUIDE.md", "samples/CONTRIBUTING.md"] # Don't add `docs/index.rst` if `versions` is not provided or `default_version` is empty - if "versions" not in kwargs or not kwargs["metadata"]["repo"]["default_version"]: + if ( + "versions" not in kwargs + or not kwargs["metadata"]["repo"]["default_version"] + ): self.excludes += ["docs/index.rst"] ret = self._generic_library("python_library", **kwargs) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index f1d571635..19b8965d7 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -116,7 +116,9 @@ def detect_versions(path: str = "./google/cloud") -> List[str]: versions = [] # Get the default_version from .repo-metadata.json - default_version = json.load(open(".repo-metadata.json", "rt")).get("default_version") + default_version = json.load(open(".repo-metadata.json", "rt")).get( + "default_version" + ) # Sort the sub directories alphabetically sub_dirs = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) @@ -124,11 +126,15 @@ def detect_versions(path: str = "./google/cloud") -> List[str]: if default_version and sub_dirs: # The subdirectory with the same suffix as the default_version # specified in .repo-metadata.json will be the default client. - default_client = next(iter([d for d in sub_dirs if d.endswith(default_version)]), None) + default_client = next( + iter([d for d in sub_dirs if d.endswith(default_version)]), None + ) if default_client: # The default_client will be first in the list. # The remaining elements will be sorted alphabetically. - versions = [default_client] + [d for d in sub_dirs if not d.endswith(default_version)] + versions = [default_client] + [ + d for d in sub_dirs if not d.endswith(default_version) + ] return versions From 483c6d0112c1230cb1c5cce8d79dd52e8de1cc36 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 6 Oct 2021 16:36:52 +0000 Subject: [PATCH 12/19] fix build --- tests/test_python_library.py | 40 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/tests/test_python_library.py b/tests/test_python_library.py index 37f69249f..9e924861b 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -137,12 +137,12 @@ def test_detect_versions_non_default_path(): for v in ("api_v1", "api_v2", "api_v3"): os.makedirs(src_dir / v) - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - with util.chdir(temp_dir): + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions(src_dir) assert ["api_v1", "api_v2", "api_v3"] == versions @@ -153,12 +153,13 @@ def test_detect_versions_default_path(): for v in ("api_v1", "api_v2", "api_v3"): os.makedirs(default_dir / v) - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - with util.chdir(temp_dir): + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions() assert ["api_v1", "api_v2", "api_v3"] == versions @@ -166,12 +167,13 @@ def test_detect_versions_default_path(): def test_detect_versions_dir_not_found(): temp_dir = Path(tempfile.mkdtemp()) - test_json = {"default_version": "api_v1"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - - versions = python.detect_versions(temp_dir / "does-not-exist") - assert [] == versions + with util.chdir(temp_dir): + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = python.detect_versions(temp_dir / "does-not-exist") + assert [] == versions def test_detect_versions_with_default_version(): @@ -183,21 +185,21 @@ def test_detect_versions_with_default_version(): with util.chdir(temp_dir): # Set default_version to "api_v1" test_json = {"default_version": "api_v1"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + with open(".repo-metadata.json", "w") as metadata: json.dump(test_json, metadata) versions = python.detect_versions() assert ["api_v1", "api_v2", "api_v3"] == versions # Set default_version to "api_v2" test_json = {"default_version": "api_v2"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + with open(".repo-metadata.json", "w") as metadata: json.dump(test_json, metadata) versions = python.detect_versions() assert ["api_v2", "api_v1", "api_v3"] == versions # Set default_version to "api_v3" test_json = {"default_version": "api_v3"} - with open(f"{temp_dir}/.repo-metadata.json", "w") as metadata: + with open(".repo-metadata.json", "w") as metadata: json.dump(test_json, metadata) versions = python.detect_versions() assert ["api_v3", "api_v1", "api_v2"] == versions From 52de68acebb3931fedb10b89becfa2c143f38d82 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 6 Oct 2021 17:05:40 +0000 Subject: [PATCH 13/19] fix build --- synthtool/languages/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index 19b8965d7..ce9ace8af 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -16,7 +16,7 @@ import re import sys from pathlib import Path -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List import yaml From 8af8e95fee0f786be46a5223ca0b55d28390239c Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 13 Oct 2021 23:59:03 +0000 Subject: [PATCH 14/19] consolidate the duplicated detect_versions function --- synthtool/gcp/common.py | 65 +++++++++++++++++++++++++++++ synthtool/languages/node.py | 30 -------------- synthtool/languages/python.py | 44 -------------------- tests/test_common.py | 76 +++++++++++++++++++++++++++++++++- tests/test_node.py | 44 -------------------- tests/test_python_library.py | 77 ----------------------------------- 6 files changed, 140 insertions(+), 196 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index a5d3d4966..1e74797bb 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -338,6 +338,71 @@ def _load_generic_metadata(self, metadata: Dict): metadata["repo"] = _load_repo_metadata() +def detect_versions( + path: str = "./src", + default_version: Optional[str] = None, + default_first: Optional[bool] = None, +) -> List[str]: + """ + Detects the versions a library has, based on distinct folders + within path. This is based on the fact that our GAPIC libraries are + structured as follows: + + src/v1 + src/v1beta + src/v1alpha + + With folder names mapping directly to versions. + + Returns: a list of the sorted subdirectories; for the example above: + ['v1', 'v1alpha', 'v1beta'] + If the `default_version` argument is not provided, the `default_version` + will be read from `.repo-metadata.json`, if it exists. + If `default_version` is available, the default_version is moved to + at the front or the end of the sorted list depending on the value of `default_first`. + The default_version will be first in the list when `default_first` is `True`. + """ + + versions = [] + + if not default_version: + try: + # Get the default_version from .repo-metadata.json + default_version = json.load(open(".repo-metadata.json", "rt")).get( + "default_version" + ) + except FileNotFoundError: + pass + + # Sort the sub directories alphabetically + sub_dirs = sorted([p.name for p in Path(path).glob("*v[1-9]*")]) + + if sub_dirs: + # if default_version is not specified, return the sorted directories. + if not default_version: + versions = sub_dirs + # if default_version is specified, the default client should be first in the + # list only when `default_first` is `True`. + else: + # The subdirectory with the same suffix as the default_version + # will be the default client. + default_client = next( + iter([d for d in sub_dirs if d.endswith(default_version)]), None + ) + + # start with all the versions except for the default client + versions = [d for d in sub_dirs if not d.endswith(default_version)] + + if default_client: + # The default_client will be first in the list. + # The remaining elements will be sorted alphabetically. + if default_first: + versions = [default_client] + versions + else: + versions += [default_client] + return versions + + def decamelize(value: str): """ parser to convert fooBar.js to Foo Bar. """ if not value: diff --git a/synthtool/languages/node.py b/synthtool/languages/node.py index 0c5a058d0..a6e81269e 100644 --- a/synthtool/languages/node.py +++ b/synthtool/languages/node.py @@ -214,36 +214,6 @@ def compile_protos(hide_output=False): shell.run(["npx", "compileProtos", "src"], hide_output=hide_output) -def detect_versions( - path: str = "./src", default_version: Optional[str] = None -) -> List[str]: - """ - Detects the versions a library has, based on distinct folders - within path. This is based on the fact that our GAPIC libraries are - structured as follows: - - src/v1 - src/v1beta - src/v1alpha - - With folder names mapping directly to versions. - - Returns: a list of the subdirectories; for the example above: - ['v1', 'v1alpha', 'v1beta'] - If specified, the default_version is guaranteed to be listed last. - Otherwise, the list is sorted alphabetically. - """ - versions = [] - if os.path.isdir(path): - for directory in os.listdir(path): - if os.path.isdir(os.path.join(path, directory)): - versions.append(directory) - versions.sort() - if default_version is not None: - versions = [v for v in versions if v != default_version] + [default_version] - return versions - - def compile_protos_hermetic(hide_output=False): """ Compiles protos into .json, .js, and .d.ts files using diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index ce9ace8af..6db28f49c 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -94,50 +94,6 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict: return sample_metadata -def detect_versions(path: str = "./google/cloud") -> List[str]: - """ - Detects the versions a library has, based on distinct folders - within path. This is based on the fact that our GAPIC libraries are - structured as follows: - - google/cloud/*_v1 - google/cloud/*_v1beta - google/cloud/*_v1alpha - - With folder names mapping directly to versions. - - Returns: a list of the subdirectories; for the example above: - ['*_v1', '*_v1alpha', '*_v1beta'] - The subdirectory with the same suffix as the default_version - specified in .repo-metadata.json will be first in the list. The - remaining elements will be sorted alphabetically. - """ - - versions = [] - - # Get the default_version from .repo-metadata.json - default_version = json.load(open(".repo-metadata.json", "rt")).get( - "default_version" - ) - - # Sort the sub directories alphabetically - sub_dirs = sorted([p.name for p in Path(path).glob("**/*_v[1-9]*")]) - - if default_version and sub_dirs: - # The subdirectory with the same suffix as the default_version - # specified in .repo-metadata.json will be the default client. - default_client = next( - iter([d for d in sub_dirs if d.endswith(default_version)]), None - ) - if default_client: - # The default_client will be first in the list. - # The remaining elements will be sorted alphabetically. - versions = [default_client] + [ - d for d in sub_dirs if not d.endswith(default_version) - ] - return versions - - def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None: """ Find all samples projects and render templates. diff --git a/tests/test_common.py b/tests/test_common.py index 446ec7c30..7bff5b5ff 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import os import tempfile from pathlib import Path @@ -20,7 +21,7 @@ from pytest import raises import synthtool as s -from synthtool.gcp.common import _get_default_branch_name, decamelize +from synthtool.gcp.common import _get_default_branch_name, decamelize, detect_versions from . import util @@ -159,3 +160,76 @@ def test_py_samples_multiple_override_content(): with open("README.md") as f: result = f.read() assert "Last Example" in result + + +def test_detect_versions_src(): + temp_dir = Path(tempfile.mkdtemp()) + src_dir = temp_dir / "src" + for v in ("v1", "v2", "v3"): + os.makedirs(src_dir / v) + + with util.chdir(temp_dir): + versions = detect_versions() + assert ["v1", "v2", "v3"] == versions + + +def test_detect_versions_staging(): + temp_dir = Path(tempfile.mkdtemp()) + staging_dir = temp_dir / "owl-bot-staging" + for v in ("v1", "v2", "v3"): + os.makedirs(staging_dir / v) + + versions = detect_versions(staging_dir) + assert ["v1", "v2", "v3"] == versions + + +def test_detect_versions_dir_not_found(): + temp_dir = Path(tempfile.mkdtemp()) + + versions = detect_versions(temp_dir / "does-not-exist") + assert [] == versions + + +def test_detect_versions_with_default_version(): + temp_dir = Path(tempfile.mkdtemp()) + src_dir = temp_dir / "src" + vs = ("v1", "v2", "v3") + for v in vs: + os.makedirs(src_dir / v) + + with util.chdir(temp_dir): + versions = detect_versions(default_version="v1") + assert ["v2", "v3", "v1"] == versions + versions = detect_versions(default_version="v2") + assert ["v1", "v3", "v2"] == versions + versions = detect_versions(default_version="v3") + assert ["v1", "v2", "v3"] == versions + + +def test_detect_versions_with_default_version_from_metadata(): + temp_dir = Path(tempfile.mkdtemp()) + default_dir = temp_dir / "src" + for v in ("api_v1", "api_v2", "api_v3"): + os.makedirs(default_dir / v) + + with util.chdir(temp_dir): + # Set default_version to "api_v1" + test_json = {"default_version": "api_v1"} + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = detect_versions(default_first=True) + assert ["api_v1", "api_v2", "api_v3"] == versions + + # Set default_version to "api_v2" + test_json = {"default_version": "api_v2"} + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = detect_versions(default_first=True) + assert ["api_v2", "api_v1", "api_v3"] == versions + + # Set default_version to "api_v3" + test_json = {"default_version": "api_v3"} + with open(".repo-metadata.json", "w") as metadata: + json.dump(test_json, metadata) + versions = detect_versions(default_first=True) + assert ["api_v3", "api_v1", "api_v2"] == versions diff --git a/tests/test_node.py b/tests/test_node.py index 8e4c2c387..a87a18026 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -272,47 +272,3 @@ def patch(library: Path): assert "import * as v2" in staging_text assert "export * as v2" not in staging_text assert "export * as v2" in text - - -def test_detect_versions_src(): - temp_dir = Path(tempfile.mkdtemp()) - src_dir = temp_dir / "src" - for v in ("v1", "v2", "v3"): - os.makedirs(src_dir / v) - - with util.chdir(temp_dir): - versions = node.detect_versions() - assert ["v1", "v2", "v3"] == versions - - -def test_detect_versions_staging(): - temp_dir = Path(tempfile.mkdtemp()) - staging_dir = temp_dir / "owl-bot-staging" - for v in ("v1", "v2", "v3"): - os.makedirs(staging_dir / v) - - versions = node.detect_versions(staging_dir) - assert ["v1", "v2", "v3"] == versions - - -def test_detect_versions_dir_not_found(): - temp_dir = Path(tempfile.mkdtemp()) - - versions = node.detect_versions(temp_dir / "does-not-exist") - assert [] == versions - - -def test_detect_versions_with_default(): - temp_dir = Path(tempfile.mkdtemp()) - src_dir = temp_dir / "src" - vs = ("v1", "v2", "v3") - for v in vs: - os.makedirs(src_dir / v) - - with util.chdir(temp_dir): - versions = node.detect_versions(default_version="v1") - assert ["v2", "v3", "v1"] == versions - versions = node.detect_versions(default_version="v2") - assert ["v1", "v3", "v2"] == versions - versions = node.detect_versions(default_version="v3") - assert ["v1", "v2", "v3"] == versions diff --git a/tests/test_python_library.py b/tests/test_python_library.py index 9e924861b..1fe8ff9c4 100644 --- a/tests/test_python_library.py +++ b/tests/test_python_library.py @@ -12,16 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import os from pathlib import Path import pytest -import tempfile from synthtool import gcp from synthtool.sources import templates -from synthtool.languages import python from . import util @@ -129,77 +126,3 @@ def test_split_system_tests(): with open(templated_files / ".kokoro/presubmit/system-3.8.cfg", "r") as f: contents = f.read() assert "system-3.8" in contents - - -def test_detect_versions_non_default_path(): - temp_dir = Path(tempfile.mkdtemp()) - src_dir = temp_dir / "src" - for v in ("api_v1", "api_v2", "api_v3"): - os.makedirs(src_dir / v) - - with util.chdir(temp_dir): - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - - versions = python.detect_versions(src_dir) - assert ["api_v1", "api_v2", "api_v3"] == versions - - -def test_detect_versions_default_path(): - temp_dir = Path(tempfile.mkdtemp()) - default_dir = temp_dir / "google/cloud" - for v in ("api_v1", "api_v2", "api_v3"): - os.makedirs(default_dir / v) - - with util.chdir(temp_dir): - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - - versions = python.detect_versions() - assert ["api_v1", "api_v2", "api_v3"] == versions - - -def test_detect_versions_dir_not_found(): - temp_dir = Path(tempfile.mkdtemp()) - - with util.chdir(temp_dir): - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - versions = python.detect_versions(temp_dir / "does-not-exist") - assert [] == versions - - -def test_detect_versions_with_default_version(): - temp_dir = Path(tempfile.mkdtemp()) - default_dir = temp_dir / "google/cloud" - for v in ("api_v1", "api_v2", "api_v3"): - os.makedirs(default_dir / v) - - with util.chdir(temp_dir): - # Set default_version to "api_v1" - test_json = {"default_version": "api_v1"} - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - versions = python.detect_versions() - assert ["api_v1", "api_v2", "api_v3"] == versions - - # Set default_version to "api_v2" - test_json = {"default_version": "api_v2"} - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - versions = python.detect_versions() - assert ["api_v2", "api_v1", "api_v3"] == versions - - # Set default_version to "api_v3" - test_json = {"default_version": "api_v3"} - with open(".repo-metadata.json", "w") as metadata: - json.dump(test_json, metadata) - versions = python.detect_versions() - assert ["api_v3", "api_v1", "api_v2"] == versions From d66ea79aca933c2f6e32a807c33f728b59b599ae Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 14 Oct 2021 00:00:16 +0000 Subject: [PATCH 15/19] remove unused import --- synthtool/languages/python.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synthtool/languages/python.py b/synthtool/languages/python.py index 6db28f49c..266aeb64d 100644 --- a/synthtool/languages/python.py +++ b/synthtool/languages/python.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import re import sys from pathlib import Path From b8c20e0d4c520b563812bc352da1aea6a98b382f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 14 Oct 2021 00:09:34 +0000 Subject: [PATCH 16/19] remove unused imports --- tests/test_node.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_node.py b/tests/test_node.py index a87a18026..a44dbd136 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -13,9 +13,7 @@ # limitations under the License. import filecmp -import os import pathlib -import tempfile from pathlib import Path from unittest import TestCase from unittest.mock import patch From fc607c099cfe32b49bdf4d20cd775d716105332f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 14 Oct 2021 00:14:26 +0000 Subject: [PATCH 17/19] update comments --- synthtool/gcp/common.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 1e74797bb..91bb28f01 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -358,31 +358,29 @@ def detect_versions( ['v1', 'v1alpha', 'v1beta'] If the `default_version` argument is not provided, the `default_version` will be read from `.repo-metadata.json`, if it exists. - If `default_version` is available, the default_version is moved to + If `default_version` is available, the `default_version` is moved to at the front or the end of the sorted list depending on the value of `default_first`. - The default_version will be first in the list when `default_first` is `True`. + The `default_version` will be first in the list when `default_first` is `True`. """ versions = [] if not default_version: try: - # Get the default_version from .repo-metadata.json + # Get the `default_version` from ``.repo-metadata.json`. default_version = json.load(open(".repo-metadata.json", "rt")).get( "default_version" ) except FileNotFoundError: pass - # Sort the sub directories alphabetically + # Sort the sub directories alphabetically. sub_dirs = sorted([p.name for p in Path(path).glob("*v[1-9]*")]) if sub_dirs: - # if default_version is not specified, return the sorted directories. + # if `default_version` is not specified, return the sorted directories. if not default_version: versions = sub_dirs - # if default_version is specified, the default client should be first in the - # list only when `default_first` is `True`. else: # The subdirectory with the same suffix as the default_version # will be the default client. @@ -394,8 +392,8 @@ def detect_versions( versions = [d for d in sub_dirs if not d.endswith(default_version)] if default_client: - # The default_client will be first in the list. - # The remaining elements will be sorted alphabetically. + # If `default_first` is true, the default_client will be first + # in the list. if default_first: versions = [default_client] + versions else: From b992238ac7dd7d9152e8dbef831442ffec4a2dcd Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 14 Oct 2021 00:16:24 +0000 Subject: [PATCH 18/19] remove unused import --- synthtool/languages/node.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synthtool/languages/node.py b/synthtool/languages/node.py index a6e81269e..315a4ccc5 100644 --- a/synthtool/languages/node.py +++ b/synthtool/languages/node.py @@ -15,7 +15,6 @@ import json from jinja2 import FileSystemLoader, Environment from pathlib import Path -import os import re from synthtool import _tracked_paths, gcp, shell, transforms from synthtool.gcp import samples, snippets From c79fc51ddf3cb3c4c0eab725bc0ac2a5dfb54f4b Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 14 Oct 2021 00:28:37 +0000 Subject: [PATCH 19/19] run black --- synthtool/gcp/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 91bb28f01..b996fcc52 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -392,7 +392,7 @@ def detect_versions( versions = [d for d in sub_dirs if not d.endswith(default_version)] if default_client: - # If `default_first` is true, the default_client will be first + # If `default_first` is true, the default_client will be first # in the list. if default_first: versions = [default_client] + versions