Skip to content

Commit

Permalink
feat: add generation config comparator (#2587)
Browse files Browse the repository at this point in the history
In this PR:
- Add a generation config comparator to compare the change type and
which libraries are affected by the change.
- Refactor `LibraryConfig`.
- Add unit tests (no integration tests because the comparator has not
been used).

The comparator makes assumptions that the following library level
parameters will not change:
- googleapis commit (no use case)

This is the first step to
[improve](https://docs.google.com/document/d/1JiCcG3X7lnxaJErKe0ES_JkyU7ECb40nf2Xez3gWvuo/edit?tab=t.g3vua2kd06gx#heading=h.pygigzqg78jp)
performance of hermetic code generation.
  • Loading branch information
JoeWang1127 authored Mar 21, 2024
1 parent 4c7fd88 commit a94c2f0
Show file tree
Hide file tree
Showing 8 changed files with 868 additions and 24 deletions.
68 changes: 68 additions & 0 deletions library_generation/model/library_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from hashlib import sha1

from typing import List, Optional
from library_generation.model.gapic_config import GapicConfig
Expand Down Expand Up @@ -71,3 +72,70 @@ def __init__(
self.cloud_api = cloud_api
self.requires_billing = requires_billing
self.extra_versioned_modules = extra_versioned_modules

def get_library_name(self) -> str:
"""
Return the library name of a given LibraryConfig object
:return: the library name
"""
return self.library_name if self.library_name else self.api_shortname

def __eq__(self, other):
return (
self.api_shortname == other.api_shortname
and self.api_description == other.api_description
and self.name_pretty == other.name_pretty
and self.product_documentation == other.product_documentation
and self.gapic_configs == other.gapic_configs
and self.library_type == other.library_type
and self.release_level == other.release_level
and self.api_id == other.api_id
and self.api_reference == other.api_reference
and self.codeowner_team == other.codeowner_team
and self.excluded_dependencies == other.excluded_dependencies
and self.excluded_poms == other.excluded_poms
and self.client_documentation == other.client_documentation
and self.distribution_name == other.distribution_name
and self.googleapis_commitish == other.googleapis_commitish
and self.group_id == other.group_id
and self.issue_tracker == other.issue_tracker
and self.library_name == other.library_name
and self.rest_documentation == other.rest_documentation
and self.rpc_documentation == other.rpc_documentation
and self.cloud_api == other.cloud_api
and self.requires_billing == other.requires_billing
and self.extra_versioned_modules == other.extra_versioned_modules
)

def __hash__(self):
m = sha1()
m.update(
str(
[
self.api_shortname,
self.api_description,
self.name_pretty,
self.product_documentation,
self.library_type,
self.release_level,
self.api_id,
self.api_reference,
self.codeowner_team,
self.excluded_dependencies,
self.excluded_poms,
self.client_documentation,
self.distribution_name,
self.googleapis_commitish,
self.group_id,
self.issue_tracker,
self.library_name,
self.rest_documentation,
self.rpc_documentation,
self.cloud_api,
self.requires_billing,
self.extra_versioned_modules,
]
+ [config.proto_path for config in self.gapic_configs]
).encode("utf-8")
)
return int(m.hexdigest(), 16)
10 changes: 5 additions & 5 deletions library_generation/test/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from library_generation.model.generation_config import from_yaml, GenerationConfig
from library_generation.test.compare_poms import compare_xml
from library_generation.utilities import (
get_library_name,
sh_util as shell_call,
run_process_and_print_output,
)
Expand Down Expand Up @@ -214,7 +213,7 @@ def __pull_repo_to(cls, default_dest: Path, repo: str, committish: str) -> str:
def __get_library_names_from_config(cls, config: GenerationConfig) -> List[str]:
library_names = []
for library in config.libraries:
library_names.append(f"java-{get_library_name(library)}")
library_names.append(f"java-{library.get_library_name()}")

return library_names

Expand Down Expand Up @@ -248,21 +247,22 @@ def __load_json_to_sorted_list(cls, path: str) -> List[tuple]:

@classmethod
def __recursive_diff_files(
self,
cls,
dcmp: dircmp,
diff_files: List[str],
left_only: List[str],
right_only: List[str],
dirname: str = "",
):
"""
recursively compares two subdirectories. The found differences are passed to three expected list references
Recursively compares two subdirectories. The found differences are
passed to three expected list references.
"""
append_dirname = lambda d: dirname + d
diff_files.extend(map(append_dirname, dcmp.diff_files))
left_only.extend(map(append_dirname, dcmp.left_only))
right_only.extend(map(append_dirname, dcmp.right_only))
for sub_dirname, sub_dcmp in dcmp.subdirs.items():
self.__recursive_diff_files(
cls.__recursive_diff_files(
sub_dcmp, diff_files, left_only, right_only, dirname + sub_dirname + "/"
)
Empty file.
39 changes: 39 additions & 0 deletions library_generation/test/model/library_config_unit_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest

from library_generation.model.library_config import LibraryConfig


class LibraryConfigTest(unittest.TestCase):
def test_get_library_returns_library_name(self):
library = LibraryConfig(
api_shortname="secret",
name_pretty="",
product_documentation="",
api_description="",
gapic_configs=list(),
library_name="secretmanager",
)
self.assertEqual("secretmanager", library.get_library_name())

def test_get_library_returns_api_shortname(self):
library = LibraryConfig(
api_shortname="secret",
name_pretty="",
product_documentation="",
api_description="",
gapic_configs=list(),
)
self.assertEqual("secret", library.get_library_name())
6 changes: 0 additions & 6 deletions library_generation/test/utilities_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,6 @@ def test_remove_version_from_returns_self(self):
"google/cloud/aiplatform", util.remove_version_from(proto_path)
)

def test_get_library_returns_library_name(self):
self.assertEqual("bare-metal-solution", util.get_library_name(library_1))

def test_get_library_returns_api_shortname(self):
self.assertEqual("secretmanager", util.get_library_name(library_2))

def test_generate_prerequisite_files_non_monorepo_success(self):
library_path = self.__setup_prerequisite_files(
num_libraries=1, library_type="GAPIC_COMBO"
Expand Down
Loading

0 comments on commit a94c2f0

Please sign in to comment.