Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .generator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ RUN chmod a+rx ./cli.py
COPY .generator/parse_googleapis_content.py .
RUN chmod a+rx ./parse_googleapis_content.py

ENV ENABLE_PERF_LOGS=1
ENTRYPOINT ["python3.14", "./cli.py"]
44 changes: 44 additions & 0 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import subprocess
import sys
import tempfile
import time
import yaml
from datetime import date, datetime
from functools import lru_cache
Expand All @@ -31,6 +32,40 @@
import build.util
import parse_googleapis_content

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

import functools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per PEP 8, imports should be grouped at the top of the file. Please move import functools to the import block at the beginning of the file.


PERF_LOGGING_ENABLED = os.environ.get("ENABLE_PERF_LOGS") == "1"

if PERF_LOGGING_ENABLED:
perf_logger = logging.getLogger("performance_metrics")
perf_logger.setLevel(logging.INFO)
perf_handler = logging.FileHandler("performance_metrics.log", mode='w')
perf_formatter = logging.Formatter('%(asctime)s | %(message)s', datefmt='%H:%M:%S')
perf_handler.setFormatter(perf_formatter)
perf_logger.addHandler(perf_handler)
perf_logger.propagate = False

def track_time(func):
"""
Decorator. Usage: @track_time
If logging is OFF, it returns the original function (Zero Overhead).
If logging is ON, it wraps the function to measure execution time.
"""
if not PERF_LOGGING_ENABLED:
return func

@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
try:
return func(*args, **kwargs)
finally:
duration = time.perf_counter() - start_time
perf_logger.info(f"{func.__name__:<30} | {duration:.4f} seconds")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line appears to be an unnecessary blank line with trailing whitespace. Please remove it to improve code cleanliness.

return wrapper

try:
import synthtool
Expand Down Expand Up @@ -320,6 +355,7 @@ def _get_library_id(request_data: Dict) -> str:
return library_id


@track_time
def _run_post_processor(output: str, library_id: str, is_mono_repo: bool):
"""Runs the synthtool post-processor on the output directory.

Expand Down Expand Up @@ -389,6 +425,7 @@ def _add_header_to_files(directory: str) -> None:
f.writelines(lines)


@track_time
def _copy_files_needed_for_post_processing(
output: str, input: str, library_id: str, is_mono_repo: bool
):
Expand Down Expand Up @@ -435,6 +472,7 @@ def _copy_files_needed_for_post_processing(
)


@track_time
def _clean_up_files_after_post_processing(
output: str, library_id: str, is_mono_repo: bool
):
Expand Down Expand Up @@ -581,6 +619,7 @@ def _get_repo_name_from_repo_metadata(base: str, library_id: str, is_mono_repo:
return repo_name


@track_time
def _generate_repo_metadata_file(
output: str, library_id: str, source: str, apis: List[Dict], is_mono_repo: bool
):
Expand Down Expand Up @@ -622,6 +661,7 @@ def _generate_repo_metadata_file(
_write_json_file(output_repo_metadata, metadata_content)


@track_time
def _copy_readme_to_docs(output: str, library_id: str, is_mono_repo: bool):
"""Copies the README.rst file for a generated library to docs/README.rst.

Expand Down Expand Up @@ -663,6 +703,7 @@ def _copy_readme_to_docs(output: str, library_id: str, is_mono_repo: bool):
f.write(content)


@track_time
def handle_generate(
librarian: str = LIBRARIAN_DIR,
source: str = SOURCE_DIR,
Expand Down Expand Up @@ -711,6 +752,7 @@ def handle_generate(
_run_post_processor(output, library_id, is_mono_repo)
_copy_readme_to_docs(output, library_id, is_mono_repo)
_clean_up_files_after_post_processing(output, library_id, is_mono_repo)

except Exception as e:
raise ValueError("Generation failed.") from e
logger.info("'generate' command executed.")
Expand Down Expand Up @@ -924,6 +966,7 @@ def _stage_gapic_library(tmp_dir: str, staging_dir: str) -> None:
shutil.copytree(tmp_dir, staging_dir, dirs_exist_ok=True)


@track_time
def _generate_api(
api_path: str,
library_id: str,
Expand Down Expand Up @@ -1744,6 +1787,7 @@ def handle_release_stage(
output=args.output,
input=args.input,
)

elif args.command == "build":
args.func(librarian=args.librarian, repo=args.repo)
elif args.command == "release-stage":
Expand Down
32 changes: 32 additions & 0 deletions .librarian/generate-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"id": "google-cloud-discoveryengine",
"version": "0.4.0",
"apis": [
{
"path": "google/cloud/discoveryengine/v1",
"service_config": "discoveryengine_v1.yaml"
},
{
"path": "google/cloud/discoveryengine/v1beta",
"service_config": "discoveryengine_v1beta.yaml"
},
{
"path": "google/cloud/discoveryengine/v1alpha",
"service_config": "discoveryengine_v1alpha.yaml"
}
],
"source_roots": [
"packages/google-cloud-discoveryengine"
],
"preserve_regex": [
"packages/google-cloud-discoveryengine/CHANGELOG.md",
"docs/CHANGELOG.md",
"samples/README.txt",
"samples/snippets/README.rst",
"tests/system"
],
"remove_regex": [
"packages/google-cloud-discoveryengine/"
],
"tag_format": "{id}-v{version}"
}
Loading