Skip to content

Commit ede39f7

Browse files
authored
Merge pull request #189 from jaraco/distutils-deprecated
Remove usage of deprecated distutils.
2 parents 2929621 + c08c4a8 commit ede39f7

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

pytest-virtualenv/pytest_virtualenv.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
"""
33
import os
44
import sys
5-
from distutils import sysconfig
65

6+
import importlib_metadata as metadata
77
from pytest import yield_fixture
8-
from pkg_resources import working_set
98
try:
109
from path import Path
1110
except ImportError:
@@ -178,8 +177,12 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None):
178177
False: Runs 'python setup.py develop'
179178
None (default): installs the egg if available in dist/, otherwise develops it
180179
"""
181-
installed = [p for p in working_set if p.project_name == pkg_name]
182-
if not installed or installed[0].location.endswith('.egg'):
180+
def location(dist):
181+
return dist.locate_file('')
182+
183+
installed = [
184+
dist for dist in metadata.distributions() if dist.name == pkg_name]
185+
if not installed or location(installed[0]).endswith('.egg'):
183186
if sys.platform == 'win32':
184187
# In virtualenv on windows "Scripts" folder is used instead of "bin".
185188
installer = str(self.virtualenv / 'Scripts' / installer + '.exe')
@@ -191,16 +194,17 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None):
191194
# This is to circumvent #! line length limits :(
192195
cmd = '%s %s %s' % (self.python, installer, pkg_name)
193196
else:
194-
pkg = installed[0]
197+
dist = installed[0]
195198
d = {'python': self.python,
196199
'easy_install': self.easy_install,
197-
'src_dir': pkg.location,
198-
'name': pkg.project_name,
199-
'version': pkg.version,
200-
'pyversion': sysconfig.get_python_version(),
200+
'src_dir': location(dist),
201+
'name': dist.name,
202+
'version': dist.version,
203+
'pyversion': '{sys.version_info[0]}.{sys.version_info[1]}'
204+
.format(**globals()),
201205
}
202206

203-
d['egg_file'] = Path(pkg.location) / 'dist' / ('%(name)s-%(version)s-py%(pyversion)s.egg' % d)
207+
d['egg_file'] = Path(location(dist)) / 'dist' / ('%(name)s-%(version)s-py%(pyversion)s.egg' % d)
204208
if build_egg and not d['egg_file'].isfile():
205209
self.run('cd %(src_dir)s; %(python)s setup.py -q bdist_egg' % d, capture=True)
206210

@@ -222,8 +226,8 @@ def installed_packages(self, package_type=None):
222226
raise ValueError('invalid package_type parameter (%s)' % str(package_type))
223227

224228
res = {}
225-
code = "from pkg_resources import working_set\n"\
226-
"for i in working_set: print(i.project_name + ' ' + i.version + ' ' + i.location)"
229+
code = "import importlib_metadata as metadata\n"\
230+
"for i in metadata.distributions(): print(i.name + ' ' + i.version + ' ' + i.locate_file(''))"
227231
lines = self.run([self.python, "-c", code], capture=True).split('\n')
228232
for line in [i.strip() for i in lines if i.strip()]:
229233
name, version, location = line.split()

pytest-virtualenv/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'pytest-shutil',
2727
'pytest',
2828
'virtualenv',
29+
'importlib-metadata',
2930
]
3031

3132
tests_require = [

0 commit comments

Comments
 (0)