Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of deprecated distutils. #189

Merged
merged 2 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions pytest-virtualenv/pytest_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"""
import os
import sys
from distutils import sysconfig

import importlib_metadata as metadata
from pytest import yield_fixture
from pkg_resources import working_set
try:
from path import Path
except ImportError:
Expand Down Expand Up @@ -178,8 +177,12 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None):
False: Runs 'python setup.py develop'
None (default): installs the egg if available in dist/, otherwise develops it
"""
installed = [p for p in working_set if p.project_name == pkg_name]
if not installed or installed[0].location.endswith('.egg'):
def location(dist):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise for neat trick

return dist.locate_file('')

installed = [
dist for dist in metadata.distributions() if dist.name == pkg_name]
if not installed or location(installed[0]).endswith('.egg'):
if sys.platform == 'win32':
# In virtualenv on windows "Scripts" folder is used instead of "bin".
installer = str(self.virtualenv / 'Scripts' / installer + '.exe')
Expand All @@ -191,16 +194,17 @@ def install_package(self, pkg_name, installer='easy_install', build_egg=None):
# This is to circumvent #! line length limits :(
cmd = '%s %s %s' % (self.python, installer, pkg_name)
else:
pkg = installed[0]
dist = installed[0]
d = {'python': self.python,
'easy_install': self.easy_install,
'src_dir': pkg.location,
'name': pkg.project_name,
'version': pkg.version,
'pyversion': sysconfig.get_python_version(),
'src_dir': location(dist),
'name': dist.name,
'version': dist.version,
'pyversion': '{sys.version_info[0]}.{sys.version_info[1]}'
.format(**globals()),
}

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

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

res = {}
code = "from pkg_resources import working_set\n"\
"for i in working_set: print(i.project_name + ' ' + i.version + ' ' + i.location)"
code = "import importlib_metadata as metadata\n"\
"for i in metadata.distributions(): print(i.name + ' ' + i.version + ' ' + i.locate_file(''))"
lines = self.run([self.python, "-c", code], capture=True).split('\n')
for line in [i.strip() for i in lines if i.strip()]:
name, version, location = line.split()
Expand Down
1 change: 1 addition & 0 deletions pytest-virtualenv/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'pytest-shutil',
'pytest',
'virtualenv',
'importlib-metadata',
]

tests_require = [
Expand Down