Skip to content

Commit e36c53a

Browse files
authored
chore(librarian): Migrate off bazel for librarian generate (#14367)
Fixes googleapis/librarian#1929
1 parent dff9e32 commit e36c53a

File tree

4 files changed

+346
-353
lines changed

4 files changed

+346
-353
lines changed

.generator/Dockerfile

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,15 @@
1616
# This stage installs all build dependencies and compiles all Python versions.
1717
FROM marketplace.gcr.io/google/ubuntu2404 AS builder
1818

19-
# TODO(https://github.com/googleapis/librarian/issues/901): Install the necssary dependencies and build tools.
2019
RUN apt-get update && \
2120
apt-get install -y --no-install-recommends \
2221
# Essential for compiling C code
2322
build-essential \
24-
# For downloading secure files
23+
# For downloading and extracting secure files
2524
git \
2625
wget \
2726
ca-certificates \
28-
# For running bazelisk commands
29-
openjdk-17-jdk \
30-
zip \
31-
unzip \
32-
# To avoid bazel error
33-
# "python interpreter `python3` not found in PATH"
34-
python3-dev \
27+
unzip \
3528
# --- Critical libraries for a complete Python build ---
3629
libssl-dev \
3730
zlib1g-dev \
@@ -43,9 +36,6 @@ RUN apt-get update && \
4336
&& apt-get clean && \
4437
rm -rf /var/lib/apt/lists/*
4538

46-
# Set up environment variables for tool versions to make updates easier.
47-
ENV BAZELISK_VERSION=v1.26.0
48-
4939
# Install multiple Python versions from source. `make altinstall` is used to
5040
# prevent replacing the system's default python binary.
5141
# TODO(http://github.com/googleapis/gapic-generator-python/issues/2435): Remove `3.10.18` when the linked issue is resolved.
@@ -68,77 +58,48 @@ RUN wget --no-check-certificate -O /tmp/get-pip.py 'https://bootstrap.pypa.io/ge
6858
done && \
6959
rm /tmp/get-pip.py
7060

71-
# Install Bazelisk
72-
RUN wget https://github.com/bazelbuild/bazelisk/releases/download/${BAZELISK_VERSION}/bazelisk-linux-amd64 -O /usr/local/bin/bazelisk && \
73-
chmod +x /usr/local/bin/bazelisk
74-
75-
# Set the working directory for build-related tasks.
76-
WORKDIR /app
77-
78-
# Create the group and user, but only if they don't already exist.
79-
ARG UID=1000
80-
ARG GID=1000
81-
82-
RUN if ! getent group $GID > /dev/null; then \
83-
groupadd -g $GID myuser; \
84-
fi && \
85-
if ! getent passwd $UID > /dev/null; then \
86-
useradd -u $UID -g $GID -ms /bin/bash myuser; \
87-
fi
88-
89-
# Set ownership of the app directory now, before we copy files into it.
90-
RUN mkdir -p /app && chown $UID:$GID /app
91-
92-
# We'll point both to the /bazel_cache directory which will be mounted as a volume.
93-
ENV BAZELISK_HOME="/bazel_cache/bazelisk"
94-
ENV BAZEL_HOME="/bazel_cache/bazel"
95-
96-
# Ensure the cache directories within the non-root user's context exist and are writable.
97-
# This is crucial as Bazel creates subdirectories under BAZEL_HOME.
98-
RUN mkdir -p ${BAZEL_HOME}/_bazel_ubuntu/cache/repos \
99-
${BAZEL_HOME}/_bazel_ubuntu/output_base \
100-
${BAZELISK_HOME} && \
101-
chown -R $UID:$GID ${BAZEL_HOME} ${BAZELISK_HOME}
102-
10361
RUN /usr/local/bin/python3.9 -m venv bazel_env
10462
RUN . bazel_env/bin/activate
10563

106-
RUN git clone https://github.com/googleapis/googleapis.git \
107-
&& cd googleapis \
108-
&& bazelisk --output_base=/bazel_cache/_bazel_ubuntu/output_base build --disk_cache=/bazel_cache/_bazel_ubuntu/cache/repos --incompatible_strict_action_env //google/cloud/language/v1:language-v1-py
64+
# Download/extract protoc
65+
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v25.3/protoc-25.3-linux-x86_64.zip
66+
RUN unzip protoc-25.3-linux-x86_64.zip -d protoc
10967

110-
# TODO(https://github.com/googleapis/librarian/issues/904): Install protoc for gencode.
68+
# Download/extract pandoc
69+
# Pandoc is required by gapic-generator-python for parsing documentation
70+
RUN wget https://github.com/jgm/pandoc/releases/download/3.7.0.2/pandoc-3.7.0.2-linux-amd64.tar.gz
71+
RUN tar -xvf pandoc-3.7.0.2-linux-amd64.tar.gz
72+
73+
# Download synthtool
74+
RUN git clone --depth 1 https://github.com/googleapis/synthtool.git synthtool
11175

11276
# --- Final Stage ---
11377
# This stage creates the lightweight final image, copying only the
11478
# necessary artifacts from the builder stage.
11579
FROM marketplace.gcr.io/google/ubuntu2404
11680

81+
# Tell synthtool to pull templates from this docker image instead of from
82+
# the live repo.
83+
ENV SYNTHTOOL_TEMPLATES="/synthtool/synthtool/gcp/templates"
84+
11785
# Install only the essential runtime libraries for Python.
11886
# These are the non "-dev" versions of the libraries used in the builder.
11987
RUN apt-get update && \
12088
apt-get install -y --no-install-recommends \
121-
ca-certificates \
122-
git \
123-
libssl3 \
124-
zlib1g \
125-
libbz2-1.0 \
126-
libffi8 \
89+
# This is needed to avoid the following error:
90+
# `ImportError: libsqlite3.so.0: cannot open shared object file: No such file or directory`.
91+
# `libsqlite3-0` is used by the `coverage` PyPI package which is used when testing libraries
12792
libsqlite3-0 \
128-
libreadline8 \
129-
# For running bazelisk commands
130-
openjdk-17-jdk \
131-
# To avoid bazel error
132-
# "python interpreter `python3` not found in PATH"
133-
python3-dev \
134-
# To avoid bazel error
135-
# "Cannot find gcc or CC; either correct your path or set the CC environment variable"
136-
build-essential \
137-
# To avoid bazel error
138-
# unzip command not found
139-
unzip \
140-
&& apt-get clean && \
141-
rm -rf /var/lib/apt/lists/*
93+
&& apt-get clean autoclean \
94+
&& apt-get autoremove -y \
95+
&& rm -rf /var/lib/apt/lists/* \
96+
&& rm -f /var/cache/apt/archives/*.deb
97+
98+
COPY --from=builder protoc/bin /usr/local/bin
99+
COPY --from=builder protoc/include /usr/local/include
100+
101+
COPY --from=builder pandoc-3.7.0.2/bin /usr/local/bin
102+
COPY --from=builder synthtool /synthtool
142103

143104
# Copy all Python interpreters, their pip executables, and their standard libraries from the builder.
144105
COPY --from=builder /usr/local/bin/python3.9 /usr/local/bin/
@@ -151,26 +112,16 @@ COPY --from=builder /usr/local/lib/python3.10 /usr/local/lib/python3.10
151112
COPY --from=builder /usr/local/bin/python3.13 /usr/local/bin/
152113
COPY --from=builder /usr/local/lib/python3.13 /usr/local/lib/python3.13
153114

154-
# Copy the bazelisk executable from the builder.
155-
COPY --from=builder /usr/local/bin/bazelisk /usr/local/bin/
156-
157-
# Copy bazel cache from the builder.
158-
COPY --from=builder /bazel_cache /bazel_cache
159-
RUN chmod -R 777 /bazel_cache
160-
161115
# Set the working directory in the container.
162116
WORKDIR /app
163117

164-
# Create a virtual env and set the Path to fix the missing nox error
165-
# when running the post processor changes.
166-
RUN /usr/local/bin/python3.9 -m venv bazel_env
167-
RUN . bazel_env/bin/activate
168-
169-
ENV PATH=/app/bazel_env/bin:$PATH
170-
171-
RUN git clone --depth 1 https://github.com/googleapis/synthtool.git /tmp/synthtool && \
172-
bazel_env/bin/python3.9 -m pip install /tmp/synthtool nox && \
173-
rm -rf /tmp/synthtool
118+
# Install dependencies of the CLI such as click.
119+
# Install gapic-generator which is used to generate libraries.
120+
# Install nox which is used for running client library tests.
121+
# Install starlark-pyo3 which is used to parse BUILD.bazel files.
122+
COPY .generator/requirements.in .
123+
RUN python3.9 -m pip install -r requirements.in
124+
RUN python3.9 -m pip install /synthtool
174125

175126
# Install build which is used to get the metadata of package config files.
176127
COPY .generator/requirements.in .
@@ -180,4 +131,8 @@ RUN python3.9 -m pip install -r requirements.in
180131
COPY .generator/cli.py .
181132
RUN chmod a+rx ./cli.py
182133

134+
# Copy the script used to parse BUILD.bazel files.
135+
COPY .generator/parse_googleapis_content.py .
136+
RUN chmod a+rx ./parse_googleapis_content.py
137+
183138
ENTRYPOINT ["python3.9", "./cli.py"]

0 commit comments

Comments
 (0)