Skip to content

Commit 586d458

Browse files
committed
default to mono repo
1 parent 77d44f7 commit 586d458

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

.generator/cli.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import tempfile
2626
import yaml
2727
from datetime import date, datetime
28+
from functools import lru_cache
2829
from pathlib import Path
2930
from typing import Dict, List
3031
import build.util
@@ -493,18 +494,49 @@ def _create_repo_metadata_from_service_config(
493494
"api_shortname": api_shortname,
494495
}
495496

496-
def _get_repo_metadata_file_path(output: str, library_id: str, is_mono_repo: bool):
497+
498+
def _get_repo_metadata_file_path(base: str, library_id: str, is_mono_repo: bool):
499+
"""Constructs the full path to the .repo-metadata.json file.
500+
501+
Args:
502+
base (str): The base directory where the library is located.
503+
library_id (str): The ID of the library.
504+
is_mono_repo (bool): True if the current repository is a mono-repo.
505+
506+
Returns:
507+
str: The absolute path to the .repo-metadata.json file.
508+
"""
497509
path_to_library = f"packages/{library_id}" if is_mono_repo else "."
498-
return f"{output}/{path_to_library}/.repo-metadata.json"
510+
return f"{base}/{path_to_library}/.repo-metadata.json"
511+
512+
513+
@lru_cache(maxsize=None)
514+
def _get_repo_name_from_repo_metadata(base: str, library_id: str, is_mono_repo: bool):
515+
"""Retrieves the repository name from the .repo-metadata.json file.
516+
517+
This function is cached to avoid redundant file I/O.
499518
500-
def _get_repo_name_from_repo_metadata(output: str, library_id: str, is_mono_repo: bool):
501-
file_path = _get_repo_metadata_file_path(output, library_id, is_mono_repo)
519+
Args:
520+
base (str): The base directory where the library is located.
521+
library_id (str): The ID of the library.
522+
is_mono_repo (bool): True if the current repository is a mono-repo.
523+
524+
Returns:
525+
str: The name of the repository (e.g., 'googleapis/google-cloud-python').
526+
527+
Raises:
528+
ValueError: If the '.repo-metadata.json' file is missing the 'repo' field.
529+
"""
530+
if is_mono_repo:
531+
return "google-cloud-python"
532+
file_path = _get_repo_metadata_file_path(base, library_id, is_mono_repo)
502533
repo_metadata = _read_json_file(file_path)
503534
repo_name = repo_metadata.get("repo")
504535
if not repo_name:
505536
raise ValueError("`.repo-metadata.json` file is missing required 'repo' field.")
506537
return repo_name
507538

539+
508540
def _generate_repo_metadata_file(
509541
output: str, library_id: str, source: str, apis: List[Dict], is_mono_repo: bool
510542
):
@@ -518,7 +550,9 @@ def _generate_repo_metadata_file(
518550
is_mono_repo(bool): True if the current repository is a mono-repo.
519551
"""
520552
path_to_library = f"packages/{library_id}" if is_mono_repo else "."
521-
output_repo_metadata = _get_repo_metadata_file_path(output, library_id, is_mono_repo)
553+
output_repo_metadata = _get_repo_metadata_file_path(
554+
output, library_id, is_mono_repo
555+
)
522556

523557
# TODO(https://github.com/googleapis/librarian/issues/2334)): If `.repo-metadata.json`
524558
# already exists in the `output` dir, then this means that it has been successfully copied
@@ -1053,7 +1087,11 @@ def _verify_library_namespace(library_id: str, repo: str, is_mono_repo: bool):
10531087
# Exclude proto paths which are not intended to be used for code generation.
10541088
# Generally any protos under the `samples` or `tests` directories or in a
10551089
# directory called `proto` are not used for code generation.
1056-
if proto_path.startswith("tests") or proto_path.startswith("samples") or proto_path.endswith("proto"):
1090+
if (
1091+
proto_path.startswith("tests")
1092+
or proto_path.startswith("samples")
1093+
or proto_path.endswith("proto")
1094+
):
10571095
continue
10581096
relevant_dirs.add(proto_file.parent)
10591097

@@ -1344,7 +1382,7 @@ def _process_changelog(
13441382
version: str,
13451383
previous_version: str,
13461384
library_id: str,
1347-
repo_name: str
1385+
repo_name: str,
13481386
):
13491387
"""This function searches the given content for the anchor pattern
13501388
`[1]: https://pypi.org/project/{library_id}/#history`
@@ -1372,7 +1410,10 @@ def _process_changelog(
13721410
entry_parts = []
13731411
entry_parts.append(
13741412
_create_main_version_header(
1375-
version=version, previous_version=previous_version, library_id=library_id, repo_name=repo_name
1413+
version=version,
1414+
previous_version=previous_version,
1415+
library_id=library_id,
1416+
repo_name=repo_name,
13761417
)
13771418
)
13781419

@@ -1444,7 +1485,7 @@ def _update_changelog_for_library(
14441485

14451486
changelog_src = f"{repo}/{relative_path}"
14461487
changelog_dest = f"{output}/{relative_path}"
1447-
repo_name = _get_repo_name_from_repo_metadata(output, library_id, is_mono_repo)
1488+
repo_name = _get_repo_name_from_repo_metadata(repo, library_id, is_mono_repo)
14481489
updated_content = _process_changelog(
14491490
_read_text_file(changelog_src),
14501491
library_changes,

.generator/test_cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,9 @@ def test_update_changelog_for_library_single_repo(mocker):
12651265
mock_read = mocker.patch("cli._read_text_file", return_value=mock_content)
12661266
mock_write = mocker.patch("cli._write_text_file")
12671267
mock_path_exists = mocker.patch("cli.os.path.lexists", return_value=True)
1268+
mocker.patch(
1269+
"cli._get_repo_name_from_repo_metadata", return_value="google-cloud-python"
1270+
)
12681271
_update_changelog_for_library(
12691272
"repo",
12701273
"output",
@@ -1300,15 +1303,20 @@ def test_process_changelog_success():
13001303
library_id = "google-cloud-language"
13011304

13021305
result = _process_changelog(
1303-
mock_content, _MOCK_LIBRARY_CHANGES, version, previous_version, library_id
1306+
mock_content,
1307+
_MOCK_LIBRARY_CHANGES,
1308+
version,
1309+
previous_version,
1310+
library_id,
1311+
"google-cloud-python",
13041312
)
13051313
assert result == expected_result
13061314

13071315

13081316
def test_process_changelog_failure():
13091317
"""Tests that value error is raised if the changelog anchor string cannot be found"""
13101318
with pytest.raises(ValueError):
1311-
_process_changelog("", [], "", "", "")
1319+
_process_changelog("", [], "", "", "", "google-cloud-python")
13121320

13131321

13141322
def test_update_changelog_for_library_failure(mocker):
@@ -1351,7 +1359,9 @@ def test_create_main_version_header():
13511359
previous_version = "1.2.2"
13521360
version = "1.2.3"
13531361
library_id = "google-cloud-language"
1354-
actual_header = _create_main_version_header(version, previous_version, library_id)
1362+
actual_header = _create_main_version_header(
1363+
version, previous_version, library_id, "google-cloud-python"
1364+
)
13551365
assert actual_header == expected_header
13561366

13571367

0 commit comments

Comments
 (0)