Skip to content

Commit

Permalink
Merge pull request pypa#11194 from pradyunsg/distutils-change-root
Browse files Browse the repository at this point in the history
Replace usage of `distutils.utils.change_root` with copied-over logic
  • Loading branch information
pradyunsg committed Jun 17, 2022
2 parents 6c2013c + c520ccf commit ae324d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/pip/_internal/locations/_sysconfig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import distutils.util # FIXME: For change_root.
import logging
import os
import sys
Expand All @@ -9,7 +8,7 @@
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
from pip._internal.utils.virtualenv import running_under_virtualenv

from .base import get_major_minor_version, is_osx_framework
from .base import change_root, get_major_minor_version, is_osx_framework

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -194,7 +193,7 @@ def get_scheme(
)
if root is not None:
for key in SCHEME_KEYS:
value = distutils.util.change_root(root, getattr(scheme, key))
value = change_root(root, getattr(scheme, key))
setattr(scheme, key, value)
return scheme

Expand Down
29 changes: 29 additions & 0 deletions src/pip/_internal/locations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sysconfig
import typing

from pip._internal.exceptions import InstallationError
from pip._internal.utils import appdirs
from pip._internal.utils.virtualenv import running_under_virtualenv

Expand All @@ -23,6 +24,34 @@ def get_major_minor_version() -> str:
return "{}.{}".format(*sys.version_info)


def change_root(new_root: str, pathname: str) -> str:
"""Return 'pathname' with 'new_root' prepended.
If 'pathname' is relative, this is equivalent to os.path.join(new_root, pathname).
Otherwise, it requires making 'pathname' relative and then joining the
two, which is tricky on DOS/Windows and Mac OS.
This is borrowed from Python's standard library's distutils module.
"""
if os.name == "posix":
if not os.path.isabs(pathname):
return os.path.join(new_root, pathname)
else:
return os.path.join(new_root, pathname[1:])

elif os.name == "nt":
(drive, path) = os.path.splitdrive(pathname)
if path[0] == "\\":
path = path[1:]
return os.path.join(new_root, path)

else:
raise InstallationError(
f"Unknown platform: {os.name}\n"
"Can not change root path prefix on unknown platform."
)


def get_src_prefix() -> str:
if running_under_virtualenv():
src_prefix = os.path.join(sys.prefix, "src")
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/operations/install/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import logging
import os
from distutils.util import change_root
from typing import List, Optional, Sequence

from pip._internal.build_env import BuildEnvironment
from pip._internal.exceptions import InstallationError, LegacyInstallFailure
from pip._internal.locations.base import change_root
from pip._internal.models.scheme import Scheme
from pip._internal.utils.misc import ensure_dir
from pip._internal.utils.setuptools_build import make_setuptools_install_args
Expand Down

0 comments on commit ae324d1

Please sign in to comment.