Skip to content

Commit 0141139

Browse files
committed
fix: Apply PEP 503 package name normalization before verifying packages
This resolves issues where packages, like ruamel.yaml, may appear under different names across the set of expected and installed package sets (e.g. `ruamel-yaml` and `ruamel.yaml`) despite both names referring to the same package per PEP 503.
1 parent ca2b965 commit 0141139

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

eng/tox/verify_installed_packages.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import logging
12+
import re
1213
from os import path
1314

1415
# import common_task module
@@ -17,6 +18,19 @@
1718
sys.path.append(common_task_path)
1819
from common_tasks import get_installed_packages
1920

21+
22+
def normalize_package_name(package_name: str) -> str:
23+
"""Apply PEP 503 normalization rules to package names
24+
25+
.. see-also::
26+
27+
https://peps.python.org/pep-0503/#normalized-names
28+
https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
29+
"""
30+
31+
return re.sub(r"[-_.]+", "-", package_name).lower()
32+
33+
2034
def verify_packages(package_file_path):
2135
# this method verifies packages installed on machine is matching the expected package version
2236
# packages.txt file expects to have list of packages and version in format <package-name>==<version>
@@ -38,11 +52,11 @@ def verify_packages(package_file_path):
3852
for p in get_installed_packages():
3953
if "==" in p:
4054
[package, version] = p.split("==")
41-
installed[package.upper().replace("_","-")] = version
55+
installed[normalize_package_name(package)] = version
4256
expected = {}
4357
for p in packages:
4458
[package, version] = p.split("==")
45-
expected[package.upper().replace("_","-")] = version
59+
expected[normalize_package_name(package)] = version
4660

4761
missing_packages = [pkg for pkg in expected.keys() if installed.get(pkg) != expected.get(pkg)]
4862

0 commit comments

Comments
 (0)