Skip to content

Commit 0a00272

Browse files
committed
fix: circular dependency
move Platform definition to libc.py and also rename it Libc
1 parent f4c195f commit 0a00272

File tree

6 files changed

+47
-42
lines changed

6 files changed

+47
-42
lines changed

auditwheel/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ class AuditwheelException(Exception):
22
pass
33

44

5-
class InvalidPlatform(AuditwheelException):
5+
class InvalidLibc(AuditwheelException):
66
pass

auditwheel/lddtree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing import List, Dict, Optional, Any, Tuple
2222

2323
from elftools.elf.elffile import ELFFile
24-
from .policy import get_policy_platform, Platform
24+
from .libc import get_libc, Libc
2525

2626

2727
log = logging.getLogger(__name__)
@@ -198,8 +198,8 @@ def load_ld_paths(root: str = '/', prefix: str = '') -> Dict[str, List[str]]:
198198
# on a per-ELF basis so it can get turned into the right thing.
199199
ldpaths['env'] = parse_ld_paths(env_ldpath, path='')
200200

201-
policy_platform = get_policy_platform()
202-
if policy_platform == Platform.MUSLLINUX:
201+
libc = get_libc()
202+
if libc == Libc.MUSL:
203203
# from https://git.musl-libc.org/cgit/musl/tree/ldso
204204
# /dynlink.c?id=3f701faace7addc75d16dea8a6cd769fa5b3f260#n1063
205205
ld_musl = list(Path(root + prefix + '/etc').glob("ld-musl-*.path"))

auditwheel/libc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
from enum import IntEnum
3+
4+
from .error import InvalidLibc
5+
from .musllinux import find_musl_libc
6+
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class Libc(IntEnum):
12+
GLIBC = 1,
13+
MUSL = 2,
14+
15+
16+
def get_libc() -> Libc:
17+
try:
18+
find_musl_libc()
19+
logger.debug("Detected musl libc")
20+
return Libc.MUSL
21+
except InvalidLibc:
22+
logger.debug("Falling back to GNU libc")
23+
return Libc.GLIBC

auditwheel/musllinux.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
from typing import NamedTuple
66

7-
from auditwheel.error import InvalidPlatform
7+
from auditwheel.error import InvalidLibc
88

99
LOG = logging.getLogger(__name__)
1010

@@ -20,15 +20,15 @@ def find_musl_libc() -> pathlib.Path:
2020
ldd = subprocess.check_output(["ldd", "/bin/ls"], errors='strict')
2121
except (subprocess.CalledProcessError, FileNotFoundError):
2222
LOG.error("Failed to determine libc version", exc_info=True)
23-
raise InvalidPlatform
23+
raise InvalidLibc
2424

2525
match = re.search(
2626
r"libc\.musl-(?P<platform>\w+)\.so.1 " # TODO drop the platform
2727
r"=> (?P<path>[/\-\w.]+)",
2828
ldd)
2929

3030
if not match:
31-
raise InvalidPlatform
31+
raise InvalidLibc
3232

3333
return pathlib.Path(match.group("path"))
3434

@@ -43,7 +43,7 @@ def get_musl_version(ld_path: pathlib.Path) -> MuslVersion:
4343
).stderr
4444
except FileNotFoundError:
4545
LOG.error("Failed to determine musl version", exc_info=True)
46-
raise InvalidPlatform
46+
raise InvalidLibc
4747

4848
match = re.search(
4949
r"Version "
@@ -52,7 +52,7 @@ def get_musl_version(ld_path: pathlib.Path) -> MuslVersion:
5252
r"(?P<patch>\d+)",
5353
ld)
5454
if not match:
55-
raise InvalidPlatform
55+
raise InvalidLibc
5656

5757
return MuslVersion(
5858
int(match.group("major")),

auditwheel/policy/__init__.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22
import json
33
import platform as _platform_module
44
from collections import defaultdict
5-
from enum import IntEnum
65
from pathlib import Path
76
from typing import Dict, List, Optional, Set
87
from os.path import join, dirname, abspath
98
import logging
109

11-
from ..error import InvalidPlatform
10+
from ..libc import get_libc, Libc
1211
from ..musllinux import find_musl_libc, get_musl_version
1312

13+
1414
_HERE = Path(__file__).parent
1515

1616
logger = logging.getLogger(__name__)
1717

18-
19-
class Platform(IntEnum):
20-
MANYLINUX = 1,
21-
MUSLLINUX = 2
22-
23-
2418
# https://docs.python.org/3/library/platform.html#platform.architecture
2519
bits = 8 * (8 if sys.maxsize > 2 ** 32 else 4)
2620

@@ -34,19 +28,7 @@ def get_arch_name() -> str:
3428

3529

3630
_ARCH_NAME = get_arch_name()
37-
38-
39-
def get_policy_platform() -> Platform:
40-
try:
41-
find_musl_libc()
42-
logger.debug("Detected musl libc")
43-
return Platform.MUSLLINUX
44-
except InvalidPlatform:
45-
logger.debug("Falling back to GNU libc")
46-
return Platform.MANYLINUX
47-
48-
49-
_PLATFORM = get_policy_platform()
31+
_LIBC = get_libc()
5032

5133

5234
def _validate_pep600_compliance(policies) -> None:
@@ -82,13 +64,13 @@ def _validate_pep600_compliance(policies) -> None:
8264

8365

8466
_POLICY_JSON_MAP = {
85-
Platform.MANYLINUX: _HERE / 'manylinux-policy.json',
86-
Platform.MUSLLINUX: _HERE / 'musllinux-policy.json',
67+
Libc.GLIBC: _HERE / 'manylinux-policy.json',
68+
Libc.MUSL: _HERE / 'musllinux-policy.json',
8769
}
8870

8971

9072
def _get_musl_policy():
91-
if _PLATFORM != Platform.MUSLLINUX:
73+
if _LIBC != Libc.MUSL:
9274
return None
9375
musl_version = get_musl_version(find_musl_libc())
9476
return f'musllinux_{musl_version.major}_{musl_version.minor}'
@@ -98,7 +80,7 @@ def _get_musl_policy():
9880

9981

10082
def _fixup_musl_libc_soname(whitelist):
101-
if _PLATFORM != Platform.MUSLLINUX:
83+
if _LIBC != Libc.MUSL:
10284
return whitelist
10385
soname_map = {
10486
"libc.so": {
@@ -121,7 +103,7 @@ def _fixup_musl_libc_soname(whitelist):
121103
return new_whitelist
122104

123105

124-
with _POLICY_JSON_MAP[_PLATFORM].open() as f:
106+
with _POLICY_JSON_MAP[_LIBC].open() as f:
125107
_POLICIES = []
126108
_policies_temp = json.load(f)
127109
_validate_pep600_compliance(_policies_temp)
@@ -137,7 +119,7 @@ def _fixup_musl_libc_soname(whitelist):
137119
for alias in _p['aliases']]
138120
_p['lib_whitelist'] = _fixup_musl_libc_soname(_p['lib_whitelist'])
139121
_POLICIES.append(_p)
140-
if _PLATFORM == Platform.MUSLLINUX:
122+
if _LIBC == Libc.MUSL:
141123
assert len(_POLICIES) == 2, _POLICIES
142124

143125
POLICY_PRIORITY_HIGHEST = max(p['priority'] for p in _POLICIES)

tests/unit/test_musllinux.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@
44
import pytest
55

66
from auditwheel.musllinux import find_musl_libc, get_musl_version
7-
from auditwheel.error import InvalidPlatform
7+
from auditwheel.error import InvalidLibc
88

99

1010
@patch("auditwheel.musllinux.subprocess.check_output")
1111
def test_find_musllinux_no_ldd(check_output_mock):
1212
check_output_mock.side_effect = FileNotFoundError()
13-
with pytest.raises(InvalidPlatform):
13+
with pytest.raises(InvalidLibc):
1414
find_musl_libc()
1515

1616

1717
@patch("auditwheel.musllinux.subprocess.check_output")
1818
def test_find_musllinux_ldd_error(check_output_mock):
1919
check_output_mock.side_effect = subprocess.CalledProcessError(1, "ldd")
20-
with pytest.raises(InvalidPlatform):
20+
with pytest.raises(InvalidLibc):
2121
find_musl_libc()
2222

2323

2424
@patch("auditwheel.musllinux.subprocess.check_output")
2525
def test_find_musllinux_not_found(check_output_mock):
2626
check_output_mock.return_value = ""
27-
with pytest.raises(InvalidPlatform):
27+
with pytest.raises(InvalidLibc):
2828
find_musl_libc()
2929

3030

3131
def test_get_musl_version_invalid_path():
32-
with pytest.raises(InvalidPlatform):
32+
with pytest.raises(InvalidLibc):
3333
get_musl_version("/tmp/no/executable/here")
3434

3535

3636
@patch("auditwheel.musllinux.subprocess.run")
3737
def test_get_musl_version_invalid_version(run_mock):
3838
run_mock.return_value = subprocess.CompletedProcess([], 1, None, "Version 1.1")
39-
with pytest.raises(InvalidPlatform):
39+
with pytest.raises(InvalidLibc):
4040
get_musl_version("anything")
4141

4242

0 commit comments

Comments
 (0)