Bug 1607522 - improve dependency detection r=sefeng,perftest-reviewers,sparky

When a package is installed with "pip --user", the import works right after the virtualenv activation.
Even if we force its install via mozbuild virtualenv it will fail. This patch checks if the installed package is **inside** or
**outside** the virtualenv. If it's outside, it forces its installation **inside**.

Notice that we might want to move that logic in mozbuild/virtualenv.py

Differential Revision: https://phabricator.services.mozilla.com/D67592

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Tarek Ziadé 2020-03-20 14:16:34 +00:00
parent f5459056be
commit 5fb873ad16

View File

@ -402,6 +402,17 @@ class MachBrowsertime(MachCommandBase):
return append_env
def _need_install(self, package):
from pip._internal.req.constructors import install_req_from_line
req = install_req_from_line("Pillow")
req.check_if_exists(use_user_site=False)
if req.satisfied_by is None:
return True
venv_site_lib = os.path.abspath(os.path.join(self.virtualenv_manager.bin_path, "..",
"lib"))
site_packages = os.path.abspath(req.satisfied_by.location)
return not site_packages.startswith(venv_site_lib)
def _activate_virtualenv(self, *args, **kwargs):
r'''Activates virtualenv.
@ -409,19 +420,11 @@ class MachBrowsertime(MachCommandBase):
It will raise an error in case the install failed.
'''
MachCommandBase._activate_virtualenv(self, *args, **kwargs)
# installing Python deps on the fly
try:
import PIL
if PIL.__version__ != PILLOW_VERSION:
raise ImportError("Wrong version %s" % PIL.__version__)
except ImportError:
self.virtualenv_manager.install_pip_package('Pillow==%s' % PILLOW_VERSION)
try:
# No __version__ in that package.
# We make the assumption it's fine.
import ssim # noqa
except ImportError:
self.virtualenv_manager.install_pip_package('pyssim==%s' % PYSSIM_VERSION)
for dep in ("Pillow==%s" % PILLOW_VERSION, "pyssim==%s" % PYSSIM_VERSION):
if self._need_install(dep):
self.virtualenv_manager._run_pip(["install", dep])
def check(self):
r'''Run `visualmetrics.py --check`.'''