Skip to content

Commit

Permalink
chore: remove pb2 file generation during post processing step (#266)
Browse files Browse the repository at this point in the history
* chore: remove pb2 file generation during post processing


---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
parthea and gcf-owl-bot[bot] authored Dec 17, 2024
1 parent 1c0667f commit 0b9e40e
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
59 changes: 0 additions & 59 deletions packages/googleapis-common-protos/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

BLACK_VERSION = "black==22.3.0"

# `grpcio-tools` 1.59.0 or newer is required for protobuf 5.x compatibility.
GRPCIO_TOOLS_VERSION = "grpcio-tools==1.59.0"

CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
UNIT_TEST_PYTHON_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

Expand Down Expand Up @@ -261,59 +258,3 @@ def tests_local(session, protobuf_implementation):
"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": protobuf_implementation,
},
)


@nox.session(python="3.8")
def generate_protos(session):
"""Generates the protos using protoc.
This session but be last to avoid overwriting the protos used in CI runs.
Some notes on the `google` directory:
1. The `_pb2.py` files are produced by protoc.
2. The .proto files are non-functional but are left in the repository
to make it easier to understand diffs.
3. The `google` directory also has `__init__.py` files to create proper modules.
If a new subdirectory is added, you will need to create more `__init__.py`
files.
"""
# longrunning operations directory is non-standard for backwards compatibility
# see comments in directory for details
# Temporarily rename the operations_pb2.py to keep it from getting overwritten
os.replace(
"google/longrunning/operations_pb2.py",
"google/longrunning/operations_pb2-COPY.py",
)

session.install(GRPCIO_TOOLS_VERSION)
protos = [str(p) for p in (Path(".").glob("google/**/*.proto"))]
session.run(
"python", "-m", "grpc_tools.protoc", "--proto_path=.", "--python_out=.", *protos
)

# Some files contain service definitions for which `_pb2_grpc.py` files must be generated.
service_protos = ["google/longrunning/operations.proto"]
session.run(
"python",
"-m",
"grpc_tools.protoc",
"--proto_path=.",
"--grpc_python_out=.",
*service_protos,
)

# More LRO non-standard fixes: rename the file and fix the import statement
operations_grpc_py = Path("google/longrunning/operations_pb2_grpc.py")
file_contents = operations_grpc_py.read_text()
file_contents = file_contents.replace("operations_pb2", "operations_proto_pb2")
operations_grpc_py.write_text(file_contents)

# Clean up LRO directory
os.replace(
"google/longrunning/operations_pb2.py",
"google/longrunning/operations_proto_pb2.py",
)
os.replace(
"google/longrunning/operations_pb2-COPY.py",
"google/longrunning/operations_pb2.py",
)
65 changes: 0 additions & 65 deletions packages/googleapis-common-protos/owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,9 @@
# 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 subprocess
import shutil


import synthtool as s
import synthtool.gcp as gcp
from synthtool.languages import python
from synthtool.sources import git

GOOGLEAPIS_REPO = "googleapis/googleapis"

# ----------------------------------------------------------------------------
# Get gapic metadata proto from googleapis
# ----------------------------------------------------------------------------

# Clean up googleapis
shutil.rmtree('googleapis', ignore_errors=True)

# Clone googleapis
googleapis_url = git.make_repo_clone_url(GOOGLEAPIS_REPO)
subprocess.run(["git", "clone", googleapis_url])

# This is required in order for s.copy() to work
s._tracked_paths.add("googleapis")

# Gapic metadata proto needed by gapic-generator-python
# Desired import is "from google.gapic.metadata import gapic_metadata_pb2"
s.copy("googleapis/gapic", "google/gapic", excludes=["lang/", "packaging/", "**/BUILD.bazel"],)

s.copy("googleapis/google/api/*.proto", "google/api")
s.copy("googleapis/google/cloud/extended_operations.proto", "google/cloud")
s.copy("googleapis/google/cloud/location/locations.proto", "google/cloud/location")
s.copy("googleapis/google/logging/type/*.proto", "google/logging/type")
s.copy("googleapis/google/longrunning/*.proto", "google/longrunning")
s.copy("googleapis/google/rpc/*.proto", "google/rpc")
s.copy("googleapis/google/rpc/context/*.proto", "google/rpc/context")
s.copy("googleapis/google/type/*.proto", "google/type")

# Clean up googleapis
shutil.rmtree('googleapis')

# ----------------------------------------------------------------------------
# Add templated files
Expand All @@ -66,34 +28,7 @@
s.move(templated_files / "renovate.json")
s.move(templated_files / ".github", excludes=["workflows"])

# Generate _pb2.py files and format them
s.shell.run(["nox", "-s", "generate_protos"], hide_output=False)

s.shell.run(["nox", "-s", "blacken"], hide_output=False)

# Add license headers
python.fix_pb2_headers()

LICENSE = """
# Copyright 2020 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."""

PB2_GRPC_HEADER = r"""(\# Generated by the gRPC Python protocol compiler plugin\. DO NOT EDIT!$)
(.*?$)"""

s.replace(
"**/*_pb2_grpc.py",
PB2_GRPC_HEADER,
fr"{LICENSE}\n\n\g<1>\n\n\g<2>", # add line breaks to avoid stacking replacements
)

0 comments on commit 0b9e40e

Please sign in to comment.