Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions eng/tox/verify_installed_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import sys
import logging
import re
from os import path

# import common_task module
Expand All @@ -17,6 +18,19 @@
sys.path.append(common_task_path)
from common_tasks import get_installed_packages


def normalize_package_name(package_name: str) -> str:
"""Apply PEP 503 normalization rules to package names

.. see-also::

https://peps.python.org/pep-0503/#normalized-names
https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
"""

return re.sub(r"[-_.]+", "-", package_name).lower()


def verify_packages(package_file_path):
# this method verifies packages installed on machine is matching the expected package version
# packages.txt file expects to have list of packages and version in format <package-name>==<version>
Expand All @@ -38,18 +52,18 @@ def verify_packages(package_file_path):
for p in get_installed_packages():
if "==" in p:
[package, version] = p.split("==")
installed[package.lower().replace("_","-")] = version
installed[normalize_package_name(package)] = version
expected = {}
for p in packages:
[package, version] = p.split("==")
expected[package.lower().replace("_","-")] = version
expected[normalize_package_name(package)] = version

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

if missing_packages:
logging.error("Version is incorrect for following package[s]")
for package in missing_packages:
logging.error("%s, Expected[%s], Installed[%s]", package, expected[package], installed.get(package, "NOT FOUND"))
logging.error("%s, Expected[%s], Installed[%s]", package, expected[package], installed[package])
sys.exit(1)
else:
logging.info("Verified package version")
Expand Down
Loading