Skip to content

Commit 62e8876

Browse files
authored
[engsys] fix: Apply PEP 503 name normalization during package verification in latestdependencies (#44597)
* Revert "Fix-Verify-Installed-Packages (#44587)" This reverts commit e4b5384. * 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 e4b5384 commit 62e8876

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

eng/tox/verify_installed_packages.py

Lines changed: 17 additions & 3 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,18 +52,18 @@ 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.lower().replace("_","-")] = version
55+
installed[normalize_package_name(package)] = version
4256
expected = {}
4357
for p in packages:
4458
[package, version] = p.split("==")
45-
expected[package.lower().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

4963
if missing_packages:
5064
logging.error("Version is incorrect for following package[s]")
5165
for package in missing_packages:
52-
logging.error("%s, Expected[%s], Installed[%s]", package, expected[package], installed.get(package, "NOT FOUND"))
66+
logging.error("%s, Expected[%s], Installed[%s]", package, expected[package], installed[package])
5367
sys.exit(1)
5468
else:
5569
logging.info("Verified package version")

0 commit comments

Comments
 (0)