Skip to content

Instantly share code, notes, and snippets.

@ssbarnea
Created October 10, 2023 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssbarnea/1ee1ee3ef0c431df74b4ab090f9d161b to your computer and use it in GitHub Desktop.
Save ssbarnea/1ee1ee3ef0c431df74b4ab090f9d161b to your computer and use it in GitHub Desktop.
Script to report broken packages and uninstall and reinstall them.
#!/usr/bin/env python
from os.path import isfile
import subprocess
from email.parser import Parser
from pip._internal.metadata import get_default_environment
from pip._internal.utils.compatibility_tags import get_supported
from pip._vendor.packaging.tags import parse_tag
supported = get_supported()
packages = get_default_environment().iter_installed_distributions()
broken_packages = []
for p in packages:
wheel_file = f"{p.info_location}/WHEEL"
if isfile(wheel_file):
with open(wheel_file, "r", encoding="utf-8") as f:
for entry in list(map(parse_tag, Parser().parse(f).get_all("Tag"))):
for tag in entry:
# pylint: disable=protected-access
platform = tag._platform
if tag in supported or platform == "any":
continue
supported_str = ", ".join(str(s) for s in supported)
print(
f"{p.canonical_name} uses unsupported {platform} and supported are {supported_str}"
)
broken_packages.append(str(p.canonical_name))
for p in broken_packages:
subprocess.run(["pip", "uninstall", "-y", p], check=False)
subprocess.run(["pip", "install", p], check=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment