|
| 1 | +"""Check whether the local Python environment can load the GaussDB driver.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import importlib |
| 7 | +import os |
| 8 | +import sys |
| 9 | +from urllib.parse import urlsplit |
| 10 | +from urllib.parse import urlunsplit |
| 11 | + |
| 12 | + |
| 13 | +def _mask_url(url: str) -> str: |
| 14 | + parts = urlsplit(url) |
| 15 | + if not parts.password: |
| 16 | + return url |
| 17 | + username = parts.username or "" |
| 18 | + host = parts.hostname or "" |
| 19 | + port = f":{parts.port}" if parts.port else "" |
| 20 | + netloc = f"{username}:***@{host}{port}" |
| 21 | + return urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment)) |
| 22 | + |
| 23 | + |
| 24 | +def _check_import(module_name: str) -> object | None: |
| 25 | + try: |
| 26 | + module = importlib.import_module(module_name) |
| 27 | + except Exception as exc: |
| 28 | + print(f"[FAIL] import {module_name}: {type(exc).__name__}: {exc}") |
| 29 | + return None |
| 30 | + |
| 31 | + version = getattr(module, "__version__", "unknown") |
| 32 | + print(f"[ OK ] import {module_name}: {version}") |
| 33 | + return module |
| 34 | + |
| 35 | + |
| 36 | +def main() -> int: |
| 37 | + parser = argparse.ArgumentParser( |
| 38 | + description="Check GaussDB Python and SQLAlchemy driver availability." |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "--url", |
| 42 | + default=os.environ.get("GAUSSDB_TEST_URL"), |
| 43 | + help="Optional SQLAlchemy URL used for a live select 1 check.", |
| 44 | + ) |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + print(f"Python: {sys.version.split()[0]}") |
| 48 | + print(f"Executable: {sys.executable}") |
| 49 | + print(f"PATH: {os.environ.get('PATH', '')}") |
| 50 | + |
| 51 | + gaussdb = _check_import("gaussdb") |
| 52 | + sqlalchemy = _check_import("sqlalchemy") |
| 53 | + dialect = _check_import("gaussdb_sqlalchemy") |
| 54 | + |
| 55 | + if not all((gaussdb, sqlalchemy, dialect)): |
| 56 | + print() |
| 57 | + print("请确认已安装 gaussdb、SQLAlchemy 和本项目 wheel。") |
| 58 | + print("Windows 上还需要将 GaussDB 客户端 bin 目录加入 PATH。") |
| 59 | + return 1 |
| 60 | + |
| 61 | + if not args.url: |
| 62 | + print("[SKIP] 未提供 --url 或 GAUSSDB_TEST_URL,跳过真实连接检查。") |
| 63 | + return 0 |
| 64 | + |
| 65 | + from sqlalchemy import create_engine |
| 66 | + from sqlalchemy import text |
| 67 | + |
| 68 | + print(f"Connecting: {_mask_url(args.url)}") |
| 69 | + try: |
| 70 | + engine = create_engine(args.url, pool_pre_ping=True) |
| 71 | + with engine.connect() as conn: |
| 72 | + value = conn.execute(text("select 1")).scalar_one() |
| 73 | + encoding = conn.execute(text("show client_encoding")).scalar_one() |
| 74 | + except Exception as exc: |
| 75 | + print(f"[FAIL] live connection: {type(exc).__name__}: {exc}") |
| 76 | + return 2 |
| 77 | + |
| 78 | + print(f"[ OK ] live connection: select 1 -> {value}") |
| 79 | + print(f"[ OK ] client_encoding: {encoding}") |
| 80 | + return 0 |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + raise SystemExit(main()) |
0 commit comments