18
18
# https://docs.python.org/3/library/platform.html#platform.architecture
19
19
bits = 8 * (8 if sys .maxsize > 2 ** 32 else 4 )
20
20
21
+ _POLICY_JSON_MAP = {
22
+ Libc .GLIBC : _HERE / "manylinux-policy.json" ,
23
+ Libc .MUSL : _HERE / "musllinux-policy.json" ,
24
+ }
25
+
26
+
27
+ class WheelPolicies :
28
+ def __init__ (self ) -> None :
29
+ libc_variant = get_libc ()
30
+ policies_path = _POLICY_JSON_MAP [libc_variant ]
31
+ policies = json .loads (policies_path .read_text ())
32
+ self ._policies = []
33
+ self ._musl_policy = _get_musl_policy ()
34
+ self ._arch_name = get_arch_name ()
35
+ self ._libc_variant = get_libc ()
36
+
37
+ _validate_pep600_compliance (policies )
38
+ for policy in policies :
39
+ if self ._musl_policy is not None and policy ["name" ] not in {
40
+ "linux" ,
41
+ self ._musl_policy ,
42
+ }:
43
+ continue
44
+ if (
45
+ self ._arch_name in policy ["symbol_versions" ].keys ()
46
+ or policy ["name" ] == "linux"
47
+ ):
48
+ if policy ["name" ] != "linux" :
49
+ policy ["symbol_versions" ] = policy ["symbol_versions" ][
50
+ self ._arch_name
51
+ ]
52
+ policy ["name" ] = policy ["name" ] + "_" + self ._arch_name
53
+ policy ["aliases" ] = [
54
+ alias + "_" + self ._arch_name for alias in policy ["aliases" ]
55
+ ]
56
+ policy ["lib_whitelist" ] = _fixup_musl_libc_soname (
57
+ policy ["lib_whitelist" ]
58
+ )
59
+ self ._policies .append (policy )
60
+
61
+ if self ._libc_variant == Libc .MUSL :
62
+ assert len (self ._policies ) == 2 , self ._policies
63
+
64
+ @property
65
+ def policies (self ):
66
+ return self ._policies
67
+
68
+ @property
69
+ def priority_highest (self ):
70
+ return max (p ["priority" ] for p in self ._policies )
71
+
72
+ @property
73
+ def priority_lowest (self ):
74
+ return min (p ["priority" ] for p in self ._policies )
75
+
76
+ def get_policy_by_name (self , name : str ) -> dict | None :
77
+ matches = [
78
+ p for p in self ._policies if p ["name" ] == name or name in p ["aliases" ]
79
+ ]
80
+ if len (matches ) == 0 :
81
+ return None
82
+ if len (matches ) > 1 :
83
+ raise RuntimeError ("Internal error. Policies should be unique" )
84
+ return matches [0 ]
85
+
86
+ def get_policy_name (self , priority : int ) -> str | None :
87
+ matches = [p ["name" ] for p in self ._policies if p ["priority" ] == priority ]
88
+ if len (matches ) == 0 :
89
+ return None
90
+ if len (matches ) > 1 :
91
+ raise RuntimeError ("Internal error. priorities should be unique" )
92
+ return matches [0 ]
93
+
94
+ def get_priority_by_name (self , name : str ) -> int | None :
95
+ policy = self .get_policy_by_name (name )
96
+ return None if policy is None else policy ["priority" ]
97
+
21
98
22
99
def get_arch_name () -> str :
23
100
machine = _platform_module .machine ()
@@ -65,22 +142,13 @@ def _validate_pep600_compliance(policies) -> None:
65
142
symbol_versions [arch ] = symbol_versions_arch
66
143
67
144
68
- _POLICY_JSON_MAP = {
69
- Libc .GLIBC : _HERE / "manylinux-policy.json" ,
70
- Libc .MUSL : _HERE / "musllinux-policy.json" ,
71
- }
72
-
73
-
74
145
def _get_musl_policy ():
75
146
if _LIBC != Libc .MUSL :
76
147
return None
77
148
musl_version = get_musl_version (find_musl_libc ())
78
149
return f"musllinux_{ musl_version .major } _{ musl_version .minor } "
79
150
80
151
81
- _MUSL_POLICY = _get_musl_policy ()
82
-
83
-
84
152
def _fixup_musl_libc_soname (whitelist ):
85
153
if _LIBC != Libc .MUSL :
86
154
return whitelist
@@ -105,60 +173,6 @@ def _fixup_musl_libc_soname(whitelist):
105
173
return new_whitelist
106
174
107
175
108
- with _POLICY_JSON_MAP [_LIBC ].open () as f :
109
- _POLICIES = []
110
- _policies_temp = json .load (f )
111
- _validate_pep600_compliance (_policies_temp )
112
- for _p in _policies_temp :
113
- if _MUSL_POLICY is not None and _p ["name" ] not in {"linux" , _MUSL_POLICY }:
114
- continue
115
- if _ARCH_NAME in _p ["symbol_versions" ].keys () or _p ["name" ] == "linux" :
116
- if _p ["name" ] != "linux" :
117
- _p ["symbol_versions" ] = _p ["symbol_versions" ][_ARCH_NAME ]
118
- _p ["name" ] = _p ["name" ] + "_" + _ARCH_NAME
119
- _p ["aliases" ] = [alias + "_" + _ARCH_NAME for alias in _p ["aliases" ]]
120
- _p ["lib_whitelist" ] = _fixup_musl_libc_soname (_p ["lib_whitelist" ])
121
- _POLICIES .append (_p )
122
- if _LIBC == Libc .MUSL :
123
- assert len (_POLICIES ) == 2 , _POLICIES
124
-
125
- POLICY_PRIORITY_HIGHEST = max (p ["priority" ] for p in _POLICIES )
126
- POLICY_PRIORITY_LOWEST = min (p ["priority" ] for p in _POLICIES )
127
-
128
-
129
- def load_policies ():
130
- return _POLICIES
131
-
132
-
133
- def _load_policy_schema ():
134
- with open (join (dirname (abspath (__file__ )), "policy-schema.json" )) as f_ :
135
- schema = json .load (f_ )
136
- return schema
137
-
138
-
139
- def get_policy_by_name (name : str ) -> dict | None :
140
- matches = [p for p in _POLICIES if p ["name" ] == name or name in p ["aliases" ]]
141
- if len (matches ) == 0 :
142
- return None
143
- if len (matches ) > 1 :
144
- raise RuntimeError ("Internal error. Policies should be unique" )
145
- return matches [0 ]
146
-
147
-
148
- def get_policy_name (priority : int ) -> str | None :
149
- matches = [p ["name" ] for p in _POLICIES if p ["priority" ] == priority ]
150
- if len (matches ) == 0 :
151
- return None
152
- if len (matches ) > 1 :
153
- raise RuntimeError ("Internal error. priorities should be unique" )
154
- return matches [0 ]
155
-
156
-
157
- def get_priority_by_name (name : str ) -> int | None :
158
- policy = get_policy_by_name (name )
159
- return None if policy is None else policy ["priority" ]
160
-
161
-
162
176
def get_replace_platforms (name : str ) -> list [str ]:
163
177
"""Extract platform tag replacement rules from policy
164
178
@@ -185,10 +199,14 @@ def get_replace_platforms(name: str) -> list[str]:
185
199
from .external_references import lddtree_external_references # noqa
186
200
from .versioned_symbols import versioned_symbols_policy # noqa
187
201
202
+ def _load_policy_schema ():
203
+ with open (join (dirname (abspath (__file__ )), "policy-schema.json" )) as f_ :
204
+ schema = json .load (f_ )
205
+ return schema
206
+
207
+
188
208
__all__ = [
189
209
"lddtree_external_references" ,
190
210
"versioned_symbols_policy" ,
191
- "load_policies" ,
192
- "POLICY_PRIORITY_HIGHEST" ,
193
- "POLICY_PRIORITY_LOWEST" ,
211
+ "WheelPolicies" ,
194
212
]
0 commit comments