1
- import distutils
2
1
import os
3
2
import re
4
3
import ssl
5
4
import sys
5
+ import sysconfig
6
6
import textwrap
7
7
from os .path import curdir , join , pardir
8
8
from pathlib import Path
@@ -1145,6 +1145,39 @@ def main(): pass
1145
1145
assert "--no-warn-script-location" not in result .stderr , str (result )
1146
1146
1147
1147
1148
+ def _change_root (new_root : str , pathname : str ) -> str :
1149
+ """
1150
+ Adapted from distutils.
1151
+
1152
+ Return 'pathname' with 'new_root' prepended. If 'pathname' is
1153
+ relative, this is equivalent to "os.path.join(new_root,pathname)".
1154
+ Otherwise, it requires making 'pathname' relative and then joining the
1155
+ two, which is tricky on DOS/Windows and Mac OS.
1156
+ """
1157
+ try :
1158
+ from distutils .util import change_root
1159
+ except ImportError :
1160
+ pass
1161
+ else :
1162
+ return change_root (new_root , pathname )
1163
+
1164
+ if os .name == "posix" :
1165
+ if not os .path .isabs (pathname ):
1166
+ return os .path .join (new_root , pathname )
1167
+ else :
1168
+ return os .path .join (new_root , pathname [1 :])
1169
+
1170
+ elif os .name == "nt" :
1171
+ drive , path = os .path .splitdrive (pathname )
1172
+ if path [0 ] == "\\ " :
1173
+ path = path [1 :]
1174
+ return os .path .join (new_root , path )
1175
+
1176
+ else :
1177
+ # distutils raise DistutilsPlatformError here
1178
+ raise RuntimeError (f"nothing known about platform '{ os .name } '" )
1179
+
1180
+
1148
1181
@pytest .mark .usefixtures ("with_wheel" )
1149
1182
def test_install_package_with_root (script : PipTestEnvironment , data : TestData ) -> None :
1150
1183
"""
@@ -1163,10 +1196,8 @@ def test_install_package_with_root(script: PipTestEnvironment, data: TestData) -
1163
1196
normal_install_path = os .fspath (
1164
1197
script .base_path / script .site_packages / "simple-1.0.dist-info"
1165
1198
)
1166
- # use distutils to change the root exactly how the --root option does it
1167
- from distutils .util import change_root
1168
1199
1169
- root_path = change_root (os .path .join (script .scratch , "root" ), normal_install_path )
1200
+ root_path = _change_root (os .path .join (script .scratch , "root" ), normal_install_path )
1170
1201
result .did_create (root_path )
1171
1202
1172
1203
# Should show find-links location in output
@@ -1195,7 +1226,7 @@ def test_install_package_with_prefix(
1195
1226
1196
1227
rel_prefix_path = script .scratch / "prefix"
1197
1228
install_path = join (
1198
- distutils . sysconfig .get_python_lib ( prefix = rel_prefix_path ),
1229
+ sysconfig .get_path ( "purelib" , vars = { "base" : rel_prefix_path } ),
1199
1230
# we still test for egg-info because no-binary implies setup.py install
1200
1231
f"simple-1.0-py{ pyversion } .egg-info" ,
1201
1232
)
@@ -1217,7 +1248,7 @@ def _test_install_editable_with_prefix(
1217
1248
"prefix" , "lib" , f"python{ pyversion } " , "site-packages"
1218
1249
)
1219
1250
else :
1220
- site_packages = distutils . sysconfig .get_python_lib ( prefix = " prefix" )
1251
+ site_packages = sysconfig .get_path ( "purelib" , vars = { "base" : " prefix"} )
1221
1252
1222
1253
# make sure target path is in PYTHONPATH
1223
1254
pythonpath = script .scratch_path / site_packages
0 commit comments