|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +"""Utility functions for Skills.""" |
| 16 | + |
| 17 | +import asyncio |
| 18 | +import base64 |
| 19 | +import io |
| 20 | +import os |
| 21 | +import time |
| 22 | +from typing import Any, Awaitable, Callable |
| 23 | +import zipfile |
| 24 | + |
| 25 | + |
| 26 | +def zip_directory(directory_path: str) -> bytes: |
| 27 | + """Zips a directory into memory and returns the bytes. |
| 28 | +
|
| 29 | + Args: |
| 30 | + directory_path (str): Required. The local path to the directory. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + bytes: The zipped directory content. |
| 34 | + """ |
| 35 | + if not os.path.isdir(directory_path): |
| 36 | + raise ValueError(f"Path is not a directory: {directory_path}") |
| 37 | + |
| 38 | + zip_buffer = io.BytesIO() |
| 39 | + with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file: |
| 40 | + for root, _, files in os.walk(directory_path): |
| 41 | + for file in files: |
| 42 | + file_path = os.path.join(root, file) |
| 43 | + arcname = os.path.relpath(file_path, directory_path) |
| 44 | + |
| 45 | + # Read actual file data |
| 46 | + with open(file_path, "rb") as f: |
| 47 | + file_data = f.read() |
| 48 | + |
| 49 | + # Use deterministic ZipInfo (mtime: 1980-01-01 00:00:00) |
| 50 | + zinfo = zipfile.ZipInfo(arcname, date_time=(1980, 1, 1, 0, 0, 0)) |
| 51 | + zinfo.compress_type = zipfile.ZIP_DEFLATED |
| 52 | + zinfo.external_attr = 0o644 << 16 # Constant file permissions |
| 53 | + |
| 54 | + zip_file.writestr(zinfo, file_data) |
| 55 | + return zip_buffer.getvalue() |
| 56 | + |
| 57 | + |
| 58 | +def get_zipped_filesystem_payload(directory_path: str) -> str: |
| 59 | + """Zips a directory and base64-encodes the result to a UTF-8 string. |
| 60 | +
|
| 61 | + Args: |
| 62 | + directory_path (str): Required. The local path to the directory. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + str: The base64-encoded zipped directory. |
| 66 | + """ |
| 67 | + zip_bytes = zip_directory(directory_path) |
| 68 | + return base64.b64encode(zip_bytes).decode("utf-8") |
| 69 | + |
| 70 | + |
| 71 | +def await_operation( |
| 72 | + *, |
| 73 | + operation_name: str, |
| 74 | + get_operation_fn: Callable[..., Any], |
| 75 | + poll_interval_seconds: float = 10.0, |
| 76 | +) -> Any: |
| 77 | + """Waits for a long running operation to complete. |
| 78 | +
|
| 79 | + Args: |
| 80 | + operation_name (str): Required. The name of the operation. |
| 81 | + get_operation_fn (Callable): Required. Function to get the operation |
| 82 | + status. |
| 83 | + poll_interval_seconds (float): The interval between polls in seconds. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Any: The completed operation. |
| 87 | + """ |
| 88 | + operation = get_operation_fn(operation_name=operation_name) |
| 89 | + while not operation.done: |
| 90 | + time.sleep(poll_interval_seconds) |
| 91 | + operation = get_operation_fn(operation_name=operation.name) |
| 92 | + return operation |
| 93 | + |
| 94 | + |
| 95 | +async def await_operation_async( |
| 96 | + *, |
| 97 | + operation_name: str, |
| 98 | + get_operation_fn: Callable[..., Awaitable[Any]], |
| 99 | + poll_interval_seconds: float = 10.0, |
| 100 | +) -> Any: |
| 101 | + """Waits for a long running operation to complete asynchronously. |
| 102 | +
|
| 103 | + Args: |
| 104 | + operation_name (str): Required. The name of the operation. |
| 105 | + get_operation_fn (Callable): Required. Async function to get the operation |
| 106 | + status. |
| 107 | + poll_interval_seconds (float): The interval between polls in seconds. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Any: The completed operation. |
| 111 | + """ |
| 112 | + operation = await get_operation_fn(operation_name=operation_name) |
| 113 | + while not operation.done: |
| 114 | + await asyncio.sleep(poll_interval_seconds) |
| 115 | + operation = await get_operation_fn(operation_name=operation.name) |
| 116 | + return operation |
0 commit comments