Skip to content

Commit 55369cf

Browse files
authored
chore(librarian): Add Python 3.13 to Python librarian generator image (#14895)
This PR is a partial revert of #14826. The reason for the revert is that some split repositories don't support Python 3.14 yet. See follow up issue googleapis/librarian#2945 to remove 3.13 from `.generator/Dockerfile`. The work to add Python 3.14 support in all split repositories is tracked in b/375664027 .
1 parent 4f86993 commit 55369cf

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

.generator/Dockerfile

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,27 @@ RUN apt-get update && \
4040
&& apt-get clean && \
4141
rm -rf /var/lib/apt/lists/*
4242

43-
ENV PYTHON_VERSION=3.14
44-
45-
# The full Python version, including the minor version, is needed for download/install
46-
ENV PYTHON_VERSION_WITH_MINOR=3.14.0
47-
4843
# `make altinstall` is used to prevent replacing the system's default python binary.
49-
RUN wget https://www.python.org/ftp/python/${PYTHON_VERSION_WITH_MINOR}/Python-${PYTHON_VERSION_WITH_MINOR}.tgz && \
44+
# The full Python version, including the minor version, is needed for download/install
45+
# TODO(https://github.com/googleapis/librarian/issues/2945): Remove `3.13` when the linked issue is resolved.
46+
RUN for PYTHON_VERSION_WITH_MINOR in 3.13.9 3.14.0; do \
47+
wget https://www.python.org/ftp/python/${PYTHON_VERSION_WITH_MINOR}/Python-${PYTHON_VERSION_WITH_MINOR}.tgz && \
5048
tar -xvf Python-${PYTHON_VERSION_WITH_MINOR}.tgz && \
5149
cd Python-${PYTHON_VERSION_WITH_MINOR} && \
5250
./configure --enable-optimizations --prefix=/usr/local && \
5351
make -j$(nproc) && \
5452
make altinstall && \
5553
cd / && \
56-
rm -rf Python-${PYTHON_VERSION_WITH_MINOR}*
54+
rm -rf Python-${PYTHON_VERSION_WITH_MINOR}* \
55+
; done
5756

5857
# Install pip for each python version
59-
# TODO(http://github.com/googleapis/gapic-generator-python/issues/2435): Remove `3.10` when the linked issue is resolved.
60-
RUN wget --no-check-certificate -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' && \
58+
# TODO(https://github.com/googleapis/librarian/issues/2945): Remove `3.13` when the linked issue is resolved.
59+
RUN for PYTHON_VERSION in 3.13 3.14; do \
60+
wget --no-check-certificate -O /tmp/get-pip.py 'https://bootstrap.pypa.io/get-pip.py' && \
6161
python${PYTHON_VERSION} /tmp/get-pip.py && \
62-
rm /tmp/get-pip.py
62+
rm /tmp/get-pip.py \
63+
; done
6364

6465
# Download/extract protoc
6566
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v25.3/protoc-25.3-linux-x86_64.zip
@@ -87,7 +88,7 @@ FROM marketplace.gcr.io/google/ubuntu2404
8788
# the live repo.
8889
ENV SYNTHTOOL_TEMPLATES="/synthtool/synthtool/gcp/templates"
8990

90-
ENV PYTHON_VERSION=3.14
91+
ENV PYTHON_VERSION_DEFAULT=3.14
9192

9293
# Install only the essential runtime libraries for Python.
9394
# These are the non "-dev" versions of the libraries used in the builder.
@@ -108,8 +109,12 @@ COPY --from=builder protoc/include /usr/local/include
108109
COPY --from=builder pandoc-binary/bin /usr/local/bin
109110
COPY --from=builder synthtool /synthtool
110111

111-
COPY --from=builder /usr/local/bin/python${PYTHON_VERSION} /usr/local/bin/
112-
COPY --from=builder /usr/local/lib/python${PYTHON_VERSION} /usr/local/lib/python${PYTHON_VERSION}
112+
COPY --from=builder /usr/local/bin/python${PYTHON_VERSION_DEFAULT} /usr/local/bin/
113+
COPY --from=builder /usr/local/lib/python${PYTHON_VERSION_DEFAULT} /usr/local/lib/python${PYTHON_VERSION_DEFAULT}
114+
115+
# TODO(https://github.com/googleapis/librarian/issues/2945): Remove `3.13` when the linked issue is resolved.
116+
COPY --from=builder /usr/local/bin/python3.13 /usr/local/bin
117+
COPY --from=builder /usr/local/lib/python3.13 /usr/local/lib/python3.13
113118

114119
# Set the working directory in the container.
115120
WORKDIR /app
@@ -119,12 +124,12 @@ WORKDIR /app
119124
# Install nox which is used for running client library tests.
120125
# Install starlark-pyo3 which is used to parse BUILD.bazel files.
121126
COPY .generator/requirements.in .
122-
RUN python${PYTHON_VERSION} -m pip install -r requirements.in
123-
RUN python${PYTHON_VERSION} -m pip install /synthtool
127+
RUN python${PYTHON_VERSION_DEFAULT} -m pip install -r requirements.in
128+
RUN python${PYTHON_VERSION_DEFAULT} -m pip install /synthtool
124129

125130
# Install build which is used to get the metadata of package config files.
126131
COPY .generator/requirements.in .
127-
RUN python${PYTHON_VERSION} -m pip install -r requirements.in
132+
RUN python${PYTHON_VERSION_DEFAULT} -m pip install -r requirements.in
128133

129134
# Copy the CLI script into the container.
130135
COPY .generator/cli.py .

.generator/cli.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,18 @@ def _run_nox_sessions(library_id: str, repo: str, is_mono_repo: bool):
942942
the config.yaml.
943943
is_mono_repo(bool): True if the current repository is a mono-repo.
944944
"""
945+
path_to_library = f"{repo}/packages/{library_id}" if is_mono_repo else repo
946+
_python_314_supported = Path(
947+
f"{path_to_library}/testing/constraints-3.14.txt"
948+
).exists()
949+
950+
if _python_314_supported:
951+
session_runtime = "3.14"
952+
else:
953+
session_runtime = "3.13"
954+
945955
sessions = [
946-
"unit-3.14(protobuf_implementation='upb')",
956+
f"unit-{session_runtime}(protobuf_implementation='upb')",
947957
]
948958
current_session = None
949959
try:

.generator/test_cli.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,25 +763,38 @@ def test_run_individual_session_failure(mocker):
763763
_run_individual_session("lint", "another-library", "repo", True)
764764

765765

766-
@pytest.mark.parametrize("is_mono_repo", [False, True])
766+
@pytest.mark.parametrize(
767+
"is_mono_repo,py314_constraints_file_exists, nox_session_python_runtime",
768+
[
769+
(False, True, "3.14"),
770+
(True, True, "3.14"),
771+
(True, False, "3.13"),
772+
(False, False, "3.13"),
773+
],
774+
)
767775
def test_run_nox_sessions_success(
768-
mocker, mock_generate_request_data_for_nox, is_mono_repo
776+
mocker,
777+
mock_generate_request_data_for_nox,
778+
is_mono_repo,
779+
py314_constraints_file_exists,
780+
nox_session_python_runtime,
769781
):
770782
"""Tests that _run_nox_sessions successfully runs all specified sessions."""
771783
mocker.patch("cli._read_json_file", return_value=mock_generate_request_data_for_nox)
772784
mocker.patch("cli._get_library_id", return_value="mock-library")
773785
mock_run_individual_session = mocker.patch("cli._run_individual_session")
786+
mocker.patch("pathlib.Path.exists", return_value=py314_constraints_file_exists)
774787

775788
sessions_to_run = [
776-
"unit-3.14(protobuf_implementation='upb')",
789+
f"unit-{nox_session_python_runtime}(protobuf_implementation='upb')",
777790
]
778791
_run_nox_sessions("mock-library", "repo", is_mono_repo)
779792

780793
assert mock_run_individual_session.call_count == len(sessions_to_run)
781794
mock_run_individual_session.assert_has_calls(
782795
[
783796
mocker.call(
784-
"unit-3.14(protobuf_implementation='upb')",
797+
f"unit-{nox_session_python_runtime}(protobuf_implementation='upb')",
785798
"mock-library",
786799
"repo",
787800
is_mono_repo,

0 commit comments

Comments
 (0)