|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Runs compliance checks on ADK source files. |
| 17 | +
|
| 18 | +This script is used as a pre-commit hook and in CI to enforce coding standards. |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import re |
| 24 | +import sys |
| 25 | + |
| 26 | +# Legacy files that are temporarily excluded from the mTLS check. |
| 27 | +# Do not add new files to this list. All new code must support mTLS. |
| 28 | +_EXCLUDED_FROM_MTLS = { |
| 29 | + 'contributing/samples/environment_and_skills/e2b_environment/agent.py', |
| 30 | + 'contributing/samples/integrations/bigquery_mcp/agent.py', |
| 31 | + 'contributing/samples/integrations/bigtable/agent.py', |
| 32 | + 'contributing/samples/integrations/data_agent/agent.py', |
| 33 | + 'contributing/samples/integrations/gcp_auth/agent.py', |
| 34 | + 'contributing/samples/integrations/gcs/agent.py', |
| 35 | + 'contributing/samples/integrations/gcs_admin/agent.py', |
| 36 | + 'contributing/samples/integrations/integration_connector_euc_agent/agent.py', |
| 37 | + 'contributing/samples/integrations/oauth_calendar_agent/agent.py', |
| 38 | + 'contributing/samples/integrations/spanner/agent.py', |
| 39 | + 'contributing/samples/integrations/spanner_admin/agent.py', |
| 40 | + 'contributing/samples/integrations/spanner_rag_agent/agent.py', |
| 41 | + 'contributing/samples/mcp/mcp_service_account_agent/agent.py', |
| 42 | + 'contributing/samples/models/interactions_api/main.py', |
| 43 | + 'contributing/samples/multimodal/static_non_text_content/agent.py', |
| 44 | + 'src/google/adk/auth/auth_credential.py', |
| 45 | + 'src/google/adk/integrations/api_registry/api_registry.py', |
| 46 | + 'src/google/adk/integrations/bigquery/bigquery_credentials.py', |
| 47 | + 'src/google/adk/integrations/bigquery/data_insights_tool.py', |
| 48 | + 'src/google/adk/integrations/bigquery/metadata_tool.py', |
| 49 | + 'src/google/adk/integrations/gcs/gcs_credentials.py', |
| 50 | + 'src/google/adk/plugins/bigquery_agent_analytics_plugin.py', |
| 51 | + 'src/google/adk/tools/_google_credentials.py', |
| 52 | + 'src/google/adk/tools/apihub_tool/clients/apihub_client.py', |
| 53 | + 'src/google/adk/tools/application_integration_tool/application_integration_toolset.py', |
| 54 | + 'src/google/adk/tools/application_integration_tool/clients/connections_client.py', |
| 55 | + 'src/google/adk/tools/application_integration_tool/clients/integration_client.py', |
| 56 | + 'src/google/adk/tools/bigtable/bigtable_credentials.py', |
| 57 | + 'src/google/adk/tools/data_agent/credentials.py', |
| 58 | + 'src/google/adk/tools/data_agent/data_agent_tool.py', |
| 59 | + 'src/google/adk/tools/google_api_tool/google_api_toolset.py', |
| 60 | + 'src/google/adk/tools/google_api_tool/googleapi_to_openapi_converter.py', |
| 61 | + 'src/google/adk/tools/mcp_tool/mcp_session_manager.py', |
| 62 | + 'src/google/adk/tools/openapi_tool/auth/auth_helpers.py', |
| 63 | + 'src/google/adk/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.py', |
| 64 | + 'src/google/adk/tools/pubsub/pubsub_credentials.py', |
| 65 | + 'src/google/adk/tools/spanner/spanner_credentials.py', |
| 66 | + 'tests/unittests/auth/test_credential_manager.py', |
| 67 | + 'tests/unittests/cli/utils/test_gcp_utils.py', |
| 68 | + 'tests/unittests/flows/llm_flows/test_functions_request_euc.py', |
| 69 | + 'tests/unittests/integrations/api_registry/test_api_registry.py', |
| 70 | + 'tests/unittests/integrations/bigquery/test_bigquery_credentials.py', |
| 71 | + 'tests/unittests/tools/apihub_tool/clients/test_apihub_client.py', |
| 72 | + 'tests/unittests/tools/application_integration_tool/clients/test_connections_client.py', |
| 73 | + 'tests/unittests/tools/application_integration_tool/clients/test_integration_client.py', |
| 74 | + 'tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py', |
| 75 | + 'tests/unittests/tools/data_agent/test_data_agent_tool.py', |
| 76 | + 'tests/unittests/tools/google_api_tool/test_docs_batchupdate.py', |
| 77 | + 'tests/unittests/tools/google_api_tool/test_google_api_toolset.py', |
| 78 | + 'tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py', |
| 79 | + 'tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py', |
| 80 | + 'tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py', |
| 81 | + 'tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py', |
| 82 | + 'tests/unittests/tools/spanner/test_spanner_credentials.py', |
| 83 | + 'tests/unittests/tools/test_base_google_credentials_manager.py', |
| 84 | + 'tests/unittests/tools/test_google_tool.py', |
| 85 | + 'tests/unittests/workflow/utils/test_workflow_hitl_utils.py', |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +def check_logger(content: str) -> bool: |
| 90 | + # Forbidden: 'logger = logging.getLogger(__name__)' |
| 91 | + pattern = re.compile(r'logger\s*=\s*logging\.getLogger\(__name__\)') |
| 92 | + return not pattern.search(content) |
| 93 | + |
| 94 | + |
| 95 | +def check_future_annotations(content: str, filename: str) -> bool: |
| 96 | + # Exclude: __init__.py, version.py, tests/, contributing/samples/ |
| 97 | + if ( |
| 98 | + filename.endswith('__init__.py') |
| 99 | + or filename.endswith('version.py') |
| 100 | + or 'tests/' in filename |
| 101 | + or 'contributing/samples/' in filename |
| 102 | + ): |
| 103 | + return True |
| 104 | + return 'from __future__ import annotations' in content |
| 105 | + |
| 106 | + |
| 107 | +def check_cli_import(content: str, filename: str) -> bool: |
| 108 | + # Exclude: cli/, apihub_toolset.py, tests/, contributing/samples/ |
| 109 | + if ( |
| 110 | + 'cli/' in filename |
| 111 | + or filename.endswith('apihub_toolset.py') |
| 112 | + or 'tests/' in filename |
| 113 | + or 'contributing/samples/' in filename |
| 114 | + ): |
| 115 | + return True |
| 116 | + # Pattern: ^from.*\bcli\b.*import.*$ (multiline) |
| 117 | + pattern = re.compile(r'^from.*\bcli\b.*import.*$', re.MULTILINE) |
| 118 | + return not pattern.search(content) |
| 119 | + |
| 120 | + |
| 121 | +def check_mtls(content: str, filename: str) -> bool: |
| 122 | + if filename in _EXCLUDED_FROM_MTLS: |
| 123 | + return True |
| 124 | + # Pattern for googleapis: https?://[a-zA-Z0-9.-]+\.googleapis\.com |
| 125 | + endpoint_pattern = re.compile(r'https?://[a-zA-Z0-9.-]+\.googleapis\.com') |
| 126 | + if endpoint_pattern.search(content): |
| 127 | + return '.mtls.googleapis.com' in content |
| 128 | + return True |
| 129 | + |
| 130 | + |
| 131 | +def main() -> None: |
| 132 | + parser = argparse.ArgumentParser(description=__doc__) |
| 133 | + parser.add_argument('files', nargs='*', help='Files to check') |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + failed = False |
| 137 | + for f in args.files: |
| 138 | + # Skip directories if they are passed accidentally |
| 139 | + if not os.path.isfile(f): |
| 140 | + continue |
| 141 | + try: |
| 142 | + with open(f, 'r', encoding='utf-8') as file: |
| 143 | + content = file.read() |
| 144 | + except Exception as e: # pylint: disable=broad-except |
| 145 | + print(f"Error reading {f}: {e}") |
| 146 | + continue |
| 147 | + |
| 148 | + # Run checks |
| 149 | + if not check_logger(content): |
| 150 | + print( |
| 151 | + f"❌ {f}: Found forbidden use of 'logger = logging.getLogger(__name__)'. " |
| 152 | + "Please use 'logger = logging.getLogger(\"google_adk.\" + __name__)' instead." |
| 153 | + ) |
| 154 | + failed = True |
| 155 | + |
| 156 | + if not check_future_annotations(content, f): |
| 157 | + print(f"❌ {f}: Missing 'from __future__ import annotations'.") |
| 158 | + failed = True |
| 159 | + |
| 160 | + if not check_cli_import(content, f): |
| 161 | + print( |
| 162 | + f"❌ {f}: Do not import from the cli package outside of the cli package." |
| 163 | + ) |
| 164 | + failed = True |
| 165 | + |
| 166 | + if not check_mtls(content, f): |
| 167 | + print( |
| 168 | + f"❌ {f}: Found hardcoded googleapis.com endpoints without mTLS support." |
| 169 | + ) |
| 170 | + failed = True |
| 171 | + |
| 172 | + if failed: |
| 173 | + sys.exit(1) |
| 174 | + sys.exit(0) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + main() |
0 commit comments