Skip to content

Commit 4ed1a9c

Browse files
mayeutauvipy
authored andcommitted
chore: use a dataclass instead of a dict for policies
1 parent 51e4a99 commit 4ed1a9c

File tree

13 files changed

+241
-333
lines changed

13 files changed

+241
-333
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
1515
extra_checks = true
1616
strict = false
1717
strict_equality = true
18-
warn_unused_configs = true
18+
warn_redundant_casts = true
1919
warn_unreachable = false
20+
warn_unused_configs = true
2021
warn_unused_ignores = true
2122

2223
[[tool.mypy.overrides]]

src/auditwheel/elfutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ def get_undefined_symbols(path: str) -> set[str]:
151151

152152

153153
def filter_undefined_symbols(
154-
path: str, symbols: dict[str, list[str]]
154+
path: str, symbols: dict[str, frozenset[str]]
155155
) -> dict[str, list[str]]:
156156
if not symbols:
157157
return {}
158158
undef_symbols = set("*") | get_undefined_symbols(path)
159159
result = {}
160160
for lib, sym_list in symbols.items():
161-
intersection = set(sym_list) & undef_symbols
161+
intersection = sym_list & undef_symbols
162162
if intersection:
163163
result[lib] = sorted(intersection)
164164
return result

src/auditwheel/json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
def _encode_value(value: Any) -> Any:
1010
if dataclasses.is_dataclass(value) and not isinstance(value, type):
11-
return dataclasses.asdict(value)
11+
as_dict = dataclasses.asdict(value)
12+
as_dict.pop("policy", None) # don't dump full policy in logs
13+
return as_dict
1214
if isinstance(value, frozenset):
1315
return sorted(value)
1416
if isinstance(value, Enum):

src/auditwheel/main_repair.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,32 @@
1515
def configure_parser(sub_parsers) -> None: # type: ignore[no-untyped-def]
1616
wheel_policy = WheelPolicies()
1717
policies = wheel_policy.policies
18-
policy_names = [p["name"] for p in policies]
19-
policy_names += [alias for p in policies for alias in p["aliases"]]
18+
policy_names = [p.name for p in policies]
19+
policy_names += [alias for p in policies for alias in p.aliases]
2020
epilog = """PLATFORMS:
2121
These are the possible target platform tags, as specified by PEP 600.
2222
Note that old, pre-PEP 600 tags are still usable and are listed as aliases
2323
below.
2424
"""
2525
for p in policies:
26-
epilog += f"- {p['name']}"
27-
if len(p["aliases"]) > 0:
28-
epilog += f" (aliased by {', '.join(p['aliases'])})"
26+
epilog += f"- {p.name}"
27+
if len(p.aliases) > 0:
28+
epilog += f" (aliased by {', '.join(p.aliases)})"
2929
epilog += "\n"
30-
highest_policy = wheel_policy.get_policy_name(wheel_policy.priority_highest)
30+
highest_policy = wheel_policy.highest.name
3131
help = """Vendor in external shared library dependencies of a wheel.
3232
If multiple wheels are specified, an error processing one
3333
wheel will abort processing of subsequent wheels.
3434
"""
35-
p = sub_parsers.add_parser(
35+
parser = sub_parsers.add_parser(
3636
"repair",
3737
help=help,
3838
description=help,
3939
epilog=epilog,
4040
formatter_class=argparse.RawDescriptionHelpFormatter,
4141
)
42-
p.add_argument("WHEEL_FILE", help="Path to wheel file.", nargs="+")
43-
p.add_argument(
42+
parser.add_argument("WHEEL_FILE", help="Path to wheel file.", nargs="+")
43+
parser.add_argument(
4444
"--plat",
4545
action=EnvironmentDefault,
4646
metavar="PLATFORM",
@@ -51,22 +51,22 @@ def configure_parser(sub_parsers) -> None: # type: ignore[no-untyped-def]
5151
choices=policy_names,
5252
default=highest_policy,
5353
)
54-
p.add_argument(
54+
parser.add_argument(
5555
"-L",
5656
"--lib-sdir",
5757
dest="LIB_SDIR",
5858
help=('Subdirectory in packages to store copied libraries. (default: ".libs")'),
5959
default=".libs",
6060
)
61-
p.add_argument(
61+
parser.add_argument(
6262
"-w",
6363
"--wheel-dir",
6464
dest="WHEEL_DIR",
6565
type=abspath,
6666
help=('Directory to store delocated wheels (default: "wheelhouse/")'),
6767
default="wheelhouse/",
6868
)
69-
p.add_argument(
69+
parser.add_argument(
7070
"--no-update-tags",
7171
dest="UPDATE_TAGS",
7272
action="store_false",
@@ -76,14 +76,14 @@ def configure_parser(sub_parsers) -> None: # type: ignore[no-untyped-def]
7676
),
7777
default=True,
7878
)
79-
p.add_argument(
79+
parser.add_argument(
8080
"--strip",
8181
dest="STRIP",
8282
action="store_true",
8383
help="Strip symbols in the resulting wheel",
8484
default=False,
8585
)
86-
p.add_argument(
86+
parser.add_argument(
8787
"--exclude",
8888
dest="EXCLUDE",
8989
help="Exclude SONAME from grafting into the resulting wheel "
@@ -94,21 +94,21 @@ def configure_parser(sub_parsers) -> None: # type: ignore[no-untyped-def]
9494
action="append",
9595
default=[],
9696
)
97-
p.add_argument(
97+
parser.add_argument(
9898
"--only-plat",
9999
dest="ONLY_PLAT",
100100
action="store_true",
101101
help="Do not check for higher policy compatibility",
102102
default=False,
103103
)
104-
p.add_argument(
104+
parser.add_argument(
105105
"--disable-isa-ext-check",
106106
dest="DISABLE_ISA_EXT_CHECK",
107107
action="store_true",
108108
help="Do not check for extended ISA compatibility (e.g. x86_64_v2)",
109109
default=False,
110110
)
111-
p.set_defaults(func=execute)
111+
parser.set_defaults(func=execute)
112112

113113

114114
def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
@@ -137,56 +137,54 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
137137
logger.info(e.message)
138138
return 1
139139

140-
policy = wheel_policy.get_policy_by_name(args.PLAT)
141-
assert policy is not None
142-
reqd_tag = policy["priority"]
140+
requested_policy = wheel_policy.get_policy_by_name(args.PLAT)
143141

144-
if reqd_tag > wheel_policy.get_priority_by_name(wheel_abi.sym_tag):
142+
if requested_policy > wheel_abi.sym_policy:
145143
msg = (
146144
f'cannot repair "{wheel_file}" to "{args.PLAT}" ABI because of the '
147145
"presence of too-recent versioned symbols. You'll need to compile "
148146
"the wheel on an older toolchain."
149147
)
150148
parser.error(msg)
151149

152-
if reqd_tag > wheel_policy.get_priority_by_name(wheel_abi.ucs_tag):
150+
if requested_policy > wheel_abi.ucs_policy:
153151
msg = (
154152
f'cannot repair "{wheel_file}" to "{args.PLAT}" ABI because it was '
155153
"compiled against a UCS2 build of Python. You'll need to compile "
156154
"the wheel against a wide-unicode build of Python."
157155
)
158156
parser.error(msg)
159157

160-
if reqd_tag > wheel_policy.get_priority_by_name(wheel_abi.blacklist_tag):
158+
if requested_policy > wheel_abi.blacklist_policy:
161159
msg = (
162160
f'cannot repair "{wheel_file}" to "{args.PLAT}" ABI because it '
163161
"depends on black-listed symbols."
164162
)
165163
parser.error(msg)
166164

167-
if reqd_tag > wheel_policy.get_priority_by_name(wheel_abi.machine_tag):
165+
if requested_policy > wheel_abi.machine_policy:
168166
msg = (
169167
f'cannot repair "{wheel_file}" to "{args.PLAT}" ABI because it '
170168
"depends on unsupported ISA extensions."
171169
)
172170
parser.error(msg)
173171

174-
abis = [policy["name"]] + policy["aliases"]
175-
if (not args.ONLY_PLAT) and reqd_tag < wheel_policy.get_priority_by_name(
176-
wheel_abi.overall_tag
177-
):
172+
abis = [requested_policy.name, *requested_policy.aliases]
173+
if (not args.ONLY_PLAT) and requested_policy < wheel_abi.overall_policy:
178174
logger.info(
179175
(
180176
"Wheel is eligible for a higher priority tag. "
181177
"You requested %s but I have found this wheel is "
182178
"eligible for %s."
183179
),
184180
args.PLAT,
185-
wheel_abi.overall_tag,
181+
wheel_abi.overall_policy.name,
186182
)
187-
higher_policy = wheel_policy.get_policy_by_name(wheel_abi.overall_tag)
188-
assert higher_policy is not None
189-
abis = [higher_policy["name"]] + higher_policy["aliases"] + abis
183+
abis = [
184+
wheel_abi.overall_policy.name,
185+
*wheel_abi.overall_policy.aliases,
186+
*abis,
187+
]
190188

191189
patcher = Patchelf()
192190
out_wheel = repair_wheel(

src/auditwheel/main_show.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
5555
]
5656

5757
printp(
58-
f'{fn} is consistent with the following platform tag: "{winfo.overall_tag}".'
58+
f'{fn} is consistent with the following platform tag: "{winfo.overall_policy.name}".'
5959
)
6060

61-
if (
62-
wheel_policy.get_priority_by_name(winfo.pyfpe_tag)
63-
< wheel_policy.priority_highest
64-
):
61+
if winfo.pyfpe_policy < wheel_policy.highest:
6562
printp(
6663
"This wheel uses the PyFPE_jbuf function, which is not compatible with the"
6764
" manylinux1 tag. (see https://www.python.org/dev/peps/pep-0513/"
@@ -70,7 +67,7 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
7067
if args.verbose < 1:
7168
return 0
7269

73-
if wheel_policy.get_priority_by_name(winfo.ucs_tag) < wheel_policy.priority_highest:
70+
if winfo.ucs_policy < wheel_policy.highest:
7471
printp(
7572
"This wheel is compiled against a narrow unicode (UCS2) "
7673
"version of Python, which is not compatible with the "
@@ -79,10 +76,7 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
7976
if args.verbose < 1:
8077
return 0
8178

82-
if (
83-
wheel_policy.get_priority_by_name(winfo.machine_tag)
84-
< wheel_policy.priority_highest
85-
):
79+
if winfo.machine_policy < wheel_policy.highest:
8680
printp("This wheel depends on unsupported ISA extensions.")
8781
if args.verbose < 1:
8882
return 0
@@ -98,9 +92,9 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
9892
f"system-provided shared libraries: {', '.join(libs_with_versions)}"
9993
)
10094

101-
if wheel_policy.get_priority_by_name(winfo.sym_tag) < wheel_policy.priority_highest:
95+
if winfo.sym_policy < wheel_policy.highest:
10296
printp(
103-
f'This constrains the platform tag to "{winfo.sym_tag}". '
97+
f'This constrains the platform tag to "{winfo.sym_policy.name}". '
10498
"In order to achieve a more compatible tag, you would "
10599
"need to recompile a new wheel from source on a system "
106100
"with earlier versions of these libraries, such as "
@@ -109,29 +103,27 @@ def execute(args: argparse.Namespace, parser: argparse.ArgumentParser) -> int:
109103
if args.verbose < 1:
110104
return 0
111105

112-
libs = winfo.external_refs[
113-
wheel_policy.get_policy_name(wheel_policy.priority_lowest)
114-
]["libs"]
106+
libs = winfo.external_refs[wheel_policy.lowest.name].libs
115107
if len(libs) == 0:
116108
printp("The wheel requires no external shared libraries! :)")
117109
else:
118110
printp("The following external shared libraries are required by the wheel:")
119111
print(json.dumps(dict(sorted(libs.items()))))
120112

121-
for p in sorted(wheel_policy.policies, key=lambda p: p["priority"]):
122-
if p["priority"] > wheel_policy.get_priority_by_name(winfo.overall_tag):
123-
libs = winfo.external_refs[p["name"]]["libs"]
113+
for p in wheel_policy.policies:
114+
if p > winfo.overall_policy:
115+
libs = winfo.external_refs[p.name].libs
124116
if len(libs):
125117
printp(
126-
f'In order to achieve the tag platform tag "{p["name"]}" '
118+
f"In order to achieve the tag platform tag {p.name!r} "
127119
"the following shared library dependencies "
128120
"will need to be eliminated:"
129121
)
130122
printp(", ".join(sorted(libs.keys())))
131-
blacklist = winfo.external_refs[p["name"]]["blacklist"]
123+
blacklist = winfo.external_refs[p.name].blacklist
132124
if len(blacklist):
133125
printp(
134-
f'In order to achieve the tag platform tag "{p["name"]}" '
126+
f"In order to achieve the tag platform tag {p.name!r} "
135127
"the following black-listed symbol dependencies "
136128
"will need to be eliminated:"
137129
)

0 commit comments

Comments
 (0)