|
5 | 5 | import os
|
6 | 6 | import platform
|
7 | 7 | import shlex
|
| 8 | +import shutil |
8 | 9 | import stat
|
9 | 10 | import subprocess
|
10 | 11 | import sys
|
| 12 | +import tarfile |
11 | 13 | from pathlib import Path
|
12 | 14 | from typing import Any
|
| 15 | +from urllib import request |
13 | 16 |
|
14 | 17 | HERE = Path(__file__).absolute().parent
|
15 | 18 | ROOT = HERE.parent.parent
|
@@ -92,6 +95,62 @@ def run_command(cmd: str) -> None:
|
92 | 95 | LOGGER.info("Running command %s... done.", cmd)
|
93 | 96 |
|
94 | 97 |
|
| 98 | +def setup_libmongocrypt(): |
| 99 | + target = "" |
| 100 | + if os.name == "nt": |
| 101 | + # PYTHON-2808 Ensure this machine has the CA cert for google KMS. |
| 102 | + if is_set("TEST_FLE_GCP_AUTO"): |
| 103 | + run_command('powershell.exe "Invoke-WebRequest -URI https://oauth2.googleapis.com/"') |
| 104 | + target = "windows-test" |
| 105 | + |
| 106 | + elif sys.platform == "darwin": |
| 107 | + target = "macos" |
| 108 | + |
| 109 | + else: |
| 110 | + name = "" |
| 111 | + version_id = "" |
| 112 | + arch = platform.machine() |
| 113 | + with open("etc/os-release") as fid: |
| 114 | + for line in fid: |
| 115 | + if line.startswith("NAME="): |
| 116 | + _, _, name = line.strip().split("=") |
| 117 | + if line.startswith("VERSION_ID="): |
| 118 | + _, _, version = line.strip().split("=") |
| 119 | + if name.startswith("Debian"): |
| 120 | + target = f"debian{version_id}" |
| 121 | + elif name.startswith("Red Hat"): |
| 122 | + if version_id.startswith("7"): |
| 123 | + target = "rhel-70-64-bit" |
| 124 | + elif version_id.startswith("8"): |
| 125 | + if arch == "aarch64": |
| 126 | + target = "rhel-82-arm64" |
| 127 | + else: |
| 128 | + target = "rhel-80-64-bit" |
| 129 | + |
| 130 | + if not is_set("LIBMONGOCRYPT_URL"): |
| 131 | + if not target: |
| 132 | + raise ValueError("Cannot find libmongocrypt target for current platform!") |
| 133 | + url = f"https://s3.amazonaws.com/mciuploads/libmongocrypt/{target}/master/latest/libmongocrypt.tar.gz" |
| 134 | + else: |
| 135 | + url = os.environ["LIBMONGOCRYPT_URL"] |
| 136 | + |
| 137 | + shutil.rmtree(HERE / "libmongocrypt", ignore_errors=True) |
| 138 | + |
| 139 | + LOGGER.info(f"Fetching {url}...") |
| 140 | + with request.urlopen(request.Request(url), timeout=15.0) as response: # noqa: S310 |
| 141 | + if response.status == 200: |
| 142 | + with tarfile.open("libmongocrypt.tar.gz", fileobj=response) as fid: |
| 143 | + fid.extractall(HERE / "libmongocrypt") |
| 144 | + LOGGER.info(f"Fetching {url}... done.") |
| 145 | + |
| 146 | + run_command("ls -la libmongocrypt") |
| 147 | + run_command("ls -la libmongocrypt/nocrypto") |
| 148 | + |
| 149 | + if os.name == "nt": |
| 150 | + # libmongocrypt's windows dll is not marked executable. |
| 151 | + run_command("chmod +x libmongocrypt/nocrypto/bin/mongocrypt.dll") |
| 152 | + |
| 153 | + |
95 | 154 | def handle_test_env() -> None:
|
96 | 155 | AUTH = os.environ.get("AUTH", "noauth")
|
97 | 156 | SSL = os.environ.get("SSL", "nossl")
|
@@ -214,7 +273,7 @@ def handle_test_env() -> None:
|
214 | 273 | if is_set("TEST_ENCRYPTION") or is_set("TEST_FLE_AZURE_AUTO") or is_set("TEST_FLE_GCP_AUTO"):
|
215 | 274 | # Check for libmongocrypt download.
|
216 | 275 | if not (ROOT / "libmongocrypt").exists():
|
217 |
| - run_command(f"bash {HERE.as_posix()}/setup-libmongocrypt.sh") |
| 276 | + setup_libmongocrypt() |
218 | 277 |
|
219 | 278 | # TODO: Test with 'pip install pymongocrypt'
|
220 | 279 | UV_ARGS.append("--group pymongocrypt_source")
|
|
0 commit comments