Skip to content

Commit af72b63

Browse files
committed
Refactor: Move get_platform_info to mssql_python/platform_utils.py
- Single source of truth for platform detection - Clean imports in setup.py (no sys.path hacks) - build_ddbc re-exports for backwards compatibility
1 parent 4b2a5cf commit af72b63

4 files changed

Lines changed: 74 additions & 60 deletions

File tree

build_ddbc/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
python -m build # Compile + create wheel (automatic)
1313
"""
1414

15-
from .compiler import compile_ddbc, get_platform_info
15+
from .compiler import compile_ddbc
16+
from mssql_python.platform_utils import get_platform_info
1617

1718
__all__ = ["compile_ddbc", "get_platform_info"]
1819
__version__ = "1.2.0"

build_ddbc/compiler.py

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,16 @@
11
"""
22
Core compiler logic for ddbc_bindings.
33
4-
This module contains the platform detection and build script execution logic.
4+
This module contains the build script execution logic.
5+
Platform detection is provided by mssql_python.platform_utils.
56
"""
67

7-
import glob
8-
import os
9-
import platform
108
import sys
119
import subprocess
1210
from pathlib import Path
13-
from typing import Tuple, Optional
11+
from typing import Optional
1412

15-
16-
def get_platform_info() -> Tuple[str, str]:
17-
"""
18-
Get platform-specific architecture and platform tag information.
19-
20-
Returns:
21-
Tuple of (architecture, platform_tag)
22-
23-
Raises:
24-
OSError: If the platform or architecture is not supported
25-
"""
26-
if sys.platform.startswith("win"):
27-
arch = os.environ.get("ARCHITECTURE", "x64")
28-
if isinstance(arch, str):
29-
arch = arch.strip("\"'")
30-
31-
if arch in ["x86", "win32"]:
32-
return "x86", "win32"
33-
elif arch == "arm64":
34-
return "arm64", "win_arm64"
35-
else:
36-
return "x64", "win_amd64"
37-
38-
elif sys.platform.startswith("darwin"):
39-
return "universal2", "macosx_15_0_universal2"
40-
41-
elif sys.platform.startswith("linux"):
42-
target_arch = os.environ.get("targetArch", platform.machine())
43-
libc_name, _ = platform.libc_ver()
44-
45-
if not libc_name:
46-
# Fallback: check for musl linker (Alpine Linux)
47-
# platform.libc_ver() returns empty string on Alpine
48-
is_musl = bool(glob.glob("/lib/ld-musl*"))
49-
if not is_musl:
50-
print(
51-
"[build_ddbc] Warning: libc detection failed; defaulting to glibc.",
52-
file=sys.stderr,
53-
)
54-
else:
55-
is_musl = "musl" in libc_name.lower()
56-
57-
if target_arch == "x86_64":
58-
return "x86_64", "musllinux_1_2_x86_64" if is_musl else "manylinux_2_28_x86_64"
59-
elif target_arch in ["aarch64", "arm64"]:
60-
return "aarch64", "musllinux_1_2_aarch64" if is_musl else "manylinux_2_28_aarch64"
61-
else:
62-
raise OSError(
63-
f"Unsupported architecture '{target_arch}' for Linux; "
64-
"expected 'x86_64' or 'aarch64'."
65-
)
66-
67-
raise OSError(f"Unsupported platform: {sys.platform}")
13+
from mssql_python.platform_utils import get_platform_info
6814

6915

7016
def find_pybind_dir() -> Path:

mssql_python/platform_utils.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Platform detection utilities for mssql-python.
3+
4+
This module provides platform and architecture detection used by both
5+
the build system (setup.py, build_ddbc) and runtime code.
6+
"""
7+
8+
import glob
9+
import os
10+
import platform
11+
import sys
12+
from typing import Tuple
13+
14+
15+
def get_platform_info() -> Tuple[str, str]:
16+
"""
17+
Get platform-specific architecture and platform tag information.
18+
19+
Returns:
20+
Tuple of (architecture, platform_tag) where platform_tag is a
21+
PEP 425 compatible wheel platform tag.
22+
23+
Raises:
24+
OSError: If the platform or architecture is not supported
25+
"""
26+
if sys.platform.startswith("win"):
27+
arch = os.environ.get("ARCHITECTURE", "x64")
28+
if isinstance(arch, str):
29+
arch = arch.strip("\"'")
30+
31+
if arch in ["x86", "win32"]:
32+
return "x86", "win32"
33+
elif arch == "arm64":
34+
return "arm64", "win_arm64"
35+
else:
36+
return "x64", "win_amd64"
37+
38+
elif sys.platform.startswith("darwin"):
39+
return "universal2", "macosx_15_0_universal2"
40+
41+
elif sys.platform.startswith("linux"):
42+
target_arch = os.environ.get("targetArch", platform.machine())
43+
libc_name, _ = platform.libc_ver()
44+
45+
if not libc_name:
46+
# Fallback: check for musl linker (Alpine Linux)
47+
# platform.libc_ver() returns empty string on Alpine
48+
is_musl = bool(glob.glob("/lib/ld-musl*"))
49+
if not is_musl:
50+
print(
51+
"[mssql_python] Warning: libc detection failed; defaulting to glibc.",
52+
file=sys.stderr,
53+
)
54+
else:
55+
is_musl = "musl" in libc_name.lower()
56+
57+
if target_arch == "x86_64":
58+
return "x86_64", "musllinux_1_2_x86_64" if is_musl else "manylinux_2_28_x86_64"
59+
elif target_arch in ["aarch64", "arm64"]:
60+
return "aarch64", "musllinux_1_2_aarch64" if is_musl else "manylinux_2_28_aarch64"
61+
else:
62+
raise OSError(
63+
f"Unsupported architecture '{target_arch}' for Linux; "
64+
"expected 'x86_64' or 'aarch64'."
65+
)
66+
67+
raise OSError(f"Unsupported platform: {sys.platform}")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from setuptools.dist import Distribution
2121
from wheel.bdist_wheel import bdist_wheel
2222

23-
from build_ddbc.compiler import get_platform_info
23+
from mssql_python.platform_utils import get_platform_info
2424

2525

2626
# =============================================================================

0 commit comments

Comments
 (0)