2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-22 09:58:09 +00:00
criu/scripts/uninstall_module.py
Radostin Stoyanov 7f0f07599a crit: fix compatibility with Python 3.12
Python 3.12 includes a few breaking changes, such as the removal of the
distutils module [1] and the deprecation of `setup.py install` in
favour of pip install [2]. This patch updates the installation script
for crit to reflect these changes by replacing the use of
`setup.py install` with `pip install` and `distutils` with
`setuptools`. In addition, a minimal pyproject.toml file has
been added as it is required by the new version of pip [3].

It is worth noting that with this change we are switching from the egg
packaging format to wheel [4] and add pip as a build dependency.

[1] https://www.python.org/downloads/release/python-3120a2/
[2] https://github.com/pypa/setuptools/pull/2824
[3] https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/
[4] https://packaging.python.org/en/latest/discussions/wheel-vs-egg/

Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
2023-04-15 21:17:21 -07:00

66 lines
2.2 KiB
Python
Executable File

#!/usr/bin/python3
"""
`pip uninstall` doesn't support `--prefix`.
https://github.com/pypa/pip/issues/11213
"""
import argparse
import os
import shutil
import site
import subprocess
import sys
import importlib_metadata
def add_site_dir(prefix: str):
"""
Add site directory with prefix to sys.path and update PYTHONPATH.
"""
# If prefix is used, we need to make sure that we
# do not uninstall other packages from the system paths.
sys.path = []
site.PREFIXES = [prefix]
pkgs = site.getsitepackages()
for path in pkgs:
site.addsitedir(path)
if 'dist-packages' in path:
# Ubuntu / Debian might use both dist- and site- packages.
site.addsitedir(path.replace('dist-packages', 'site-packages'))
os.environ['PYTHONPATH'] = os.pathsep.join(sys.path)
def uninstall_module(package_name: str, prefix=None):
"""
Enable support for '--prefix' with 'pip uninstall'.
"""
dist_info_path = None
if prefix:
add_site_dir(prefix)
try:
dist_info_path = str(importlib_metadata.distribution(package_name)._path)
except importlib_metadata.PackageNotFoundError:
print(f"Skipping {package_name} as it is not installed.")
sys.exit(0)
command = [sys.executable, '-m', 'pip', 'uninstall', '-y', package_name]
try:
subprocess.check_call(command, env=os.environ)
if dist_info_path and os.path.isdir(dist_info_path):
# .dist-info files are not cleaned up when the package
# has been installed with --prefix.
# https://github.com/pypa/pip/issues/5573
shutil.rmtree(dist_info_path)
if 'dist-packages' in dist_info_path:
shutil.rmtree(dist_info_path.replace('dist-packages', 'site-packages'))
except subprocess.CalledProcessError as err:
print(f'Error uninstalling package {package_name}: {err}')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('module_name', help='The name of the module to uninstall')
parser.add_argument('--prefix', help='The prefix where the module was installed')
args = parser.parse_args()
uninstall_module(args.module_name, args.prefix)