-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate config change functions to
common
module (#3311)
In this PR: - Migrate config change functions to `common` module. - Library generation will accept library names directly.
- Loading branch information
1 parent
ee34704
commit 4d4c798
Showing
27 changed files
with
335 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# 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 os | ||
|
||
import click as click | ||
|
||
from common.model.generation_config import from_yaml | ||
from common.utils.generation_config_comparator import compare_config | ||
|
||
|
||
@click.group(invoke_without_command=False) | ||
@click.pass_context | ||
@click.version_option(message="%(version)s") | ||
def main(ctx): | ||
pass | ||
|
||
|
||
@main.command() | ||
@click.option( | ||
"--baseline-generation-config-path", | ||
required=True, | ||
type=str, | ||
help=""" | ||
Absolute or relative path to a generation_config.yaml. | ||
This config file is used for computing changed library list. | ||
""", | ||
) | ||
@click.option( | ||
"--current-generation-config-path", | ||
required=True, | ||
type=str, | ||
help=""" | ||
Absolute or relative path to a generation_config.yaml that contains the | ||
metadata about library generation. | ||
""", | ||
) | ||
def create( | ||
baseline_generation_config_path: str, | ||
current_generation_config_path: str, | ||
) -> None: | ||
""" | ||
Compares baseline generation config with current generation config and | ||
generates changed library names (a comma separated string) based on current | ||
generation config. | ||
""" | ||
baseline_generation_config_path = os.path.abspath(baseline_generation_config_path) | ||
if not os.path.isfile(baseline_generation_config_path): | ||
raise FileNotFoundError( | ||
f"{baseline_generation_config_path} does not exist. " | ||
"A valid generation config has to be passed in as " | ||
"baseline-generation-config-path." | ||
) | ||
current_generation_config_path = os.path.abspath(current_generation_config_path) | ||
if not os.path.isfile(current_generation_config_path): | ||
raise FileNotFoundError( | ||
f"{current_generation_config_path} does not exist. " | ||
"A valid generation config has to be passed in as " | ||
"current-generation-config-path." | ||
) | ||
config_change = compare_config( | ||
baseline_config=from_yaml(baseline_generation_config_path), | ||
current_config=from_yaml(current_generation_config_path), | ||
) | ||
changed_libraries = config_change.get_changed_libraries() | ||
if changed_libraries is None: | ||
print("No changed library.") | ||
return | ||
click.echo(",".join(config_change.get_changed_libraries())) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
black==24.8.0 | ||
GitPython==3.1.43 | ||
parameterized==0.9.0 | ||
PyYAML==6.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
71 changes: 71 additions & 0 deletions
71
hermetic_build/common/tests/cli/config_change_unit_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# 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 os | ||
from click.testing import CliRunner | ||
import unittest | ||
|
||
from common.cli.get_changed_libraries import create | ||
|
||
script_dir = os.path.dirname(os.path.realpath(__file__)) | ||
test_resource_dir = os.path.join(script_dir, "..", "resources", "cli") | ||
|
||
|
||
class GetChangedLibrariesTest(unittest.TestCase): | ||
def test_entry_point_without_baseline_config_raise_system_exception(self): | ||
os.chdir(script_dir) | ||
runner = CliRunner() | ||
# noinspection PyTypeChecker | ||
result = runner.invoke(create) | ||
self.assertEqual(2, result.exit_code) | ||
self.assertEqual(SystemExit, result.exc_info[0]) | ||
|
||
def test_entry_point_without_current_config_raise_system_exception(self): | ||
os.chdir(script_dir) | ||
runner = CliRunner() | ||
# noinspection PyTypeChecker | ||
result = runner.invoke( | ||
create, ["--baseline-generation-config-path=/invalid/path/file"] | ||
) | ||
self.assertEqual(2, result.exit_code) | ||
self.assertEqual(SystemExit, result.exc_info[0]) | ||
|
||
def test_entry_point_with_invalid_baseline_config_raise_file_exception(self): | ||
os.chdir(script_dir) | ||
runner = CliRunner() | ||
# noinspection PyTypeChecker | ||
result = runner.invoke( | ||
create, | ||
[ | ||
"--baseline-generation-config-path=/invalid/path/file", | ||
"--current-generation-config-path=/invalid/path/file", | ||
], | ||
) | ||
self.assertEqual(1, result.exit_code) | ||
self.assertEqual(FileNotFoundError, result.exc_info[0]) | ||
self.assertRegex(result.exception.args[0], "baseline-generation-config-path") | ||
|
||
def test_entry_point_with_invalid_current_config_raise_file_exception(self): | ||
os.chdir(script_dir) | ||
runner = CliRunner() | ||
# noinspection PyTypeChecker | ||
result = runner.invoke( | ||
create, | ||
[ | ||
f"--baseline-generation-config-path={test_resource_dir}/empty_config.yaml", | ||
"--current-generation-config-path=/invalid/path/file", | ||
], | ||
) | ||
self.assertEqual(1, result.exit_code) | ||
self.assertEqual(FileNotFoundError, result.exc_info[0]) | ||
self.assertRegex(result.exception.args[0], "current-generation-config-path") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
hermetic_build/common/tests/utils/proto_path_utils_unit_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python3 | ||
# 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 | ||
# | ||
# http://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 os | ||
import unittest | ||
from pathlib import Path | ||
from common.utils.proto_path_utils import find_versioned_proto_path | ||
|
||
script_dir = os.path.dirname(os.path.realpath(__file__)) | ||
resources_dir = os.path.join(script_dir, "..", "resources") | ||
test_config_dir = Path(os.path.join(resources_dir, "test-config")).resolve() | ||
|
||
|
||
class ProtoPathsUtilsTest(unittest.TestCase): | ||
def test_find_versioned_proto_path_nested_version_success(self): | ||
proto_path = "google/cloud/aiplatform/v1/schema/predict/params/image_classification.proto" | ||
expected = "google/cloud/aiplatform/v1" | ||
self.assertEqual(expected, find_versioned_proto_path(proto_path)) | ||
|
||
def test_find_versioned_proto_path_success(self): | ||
proto_path = "google/cloud/asset/v1p2beta1/assets.proto" | ||
expected = "google/cloud/asset/v1p2beta1" | ||
self.assertEqual(expected, find_versioned_proto_path(proto_path)) | ||
|
||
def test_find_versioned_proto_without_version_return_itself(self): | ||
proto_path = "google/type/color.proto" | ||
expected = "google/type/color.proto" | ||
self.assertEqual(expected, find_versioned_proto_path(proto_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python3 | ||
# 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 | ||
# | ||
# http://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 re | ||
|
||
|
||
def find_versioned_proto_path(proto_path: str) -> str: | ||
""" | ||
Returns a versioned proto_path from a given proto_path; or proto_path itself | ||
if it doesn't contain a versioned proto_path. | ||
:param proto_path: a proto file path | ||
:return: the versioned proto_path | ||
""" | ||
version_regex = re.compile(r"^v[1-9].*") | ||
directories = proto_path.split("/") | ||
for directory in directories: | ||
result = version_regex.search(directory) | ||
if result: | ||
version = result[0] | ||
idx = proto_path.find(version) | ||
return proto_path[:idx] + version | ||
return proto_path |
Oops, something went wrong.