2525import tempfile
2626import yaml
2727from datetime import date , datetime
28+ from functools import lru_cache
2829from pathlib import Path
2930from typing import Dict , List
3031import 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+
508540def _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 ,
0 commit comments