Commit 2c1d3639 by Eric Coissac

Change the way to call PIP. We are now using system calls rather than

the pre-PIP10 internal API to conform with PIP developer
recommendations.
parent bafdf166
......@@ -5,32 +5,35 @@ Created on 2 oct. 2014
'''
import re
import os
try:
import pip # @UnresolvedImport
from pip.utils import get_installed_distributions # @UnresolvedImport
except ImportError:
from .bootstrappip import bootstrap
bootstrap()
from distutils.version import StrictVersion # @UnusedImport
from distutils.errors import DistutilsError
from distutils import log
import os.path
import sys
import subprocess
class RequirementError(Exception):
pass
def is_installed(requirement):
pipcommand = os.path.join(os.path.dirname(sys.executable),'pip')
pipjson = subprocess.run([pipcommand,"list","--format=json"],
capture_output=True).stdout
packages = eval(pipjson)
requirement_project,requirement_relation,requirement_version = parse_package_requirement(requirement)
package = [x for x in get_installed_distributions() if x.project_name==requirement_project]
package = [x for x in packages if x["name"]==requirement_project]
if len(package)==1:
if requirement_version is not None and requirement_relation is not None:
rep = (len(package)==1) and eval("StrictVersion('%s') %s StrictVersion('%s')" % (package[0].version,
if ( requirement_version is not None
and requirement_relation is not None):
rep = (len(package)==1) and eval("StrictVersion('%s') %s StrictVersion('%s')" % (package[0]["version"],
requirement_relation,
requirement_version)
)
......@@ -44,20 +47,23 @@ def is_installed(requirement):
log.info("Look for package %s (%s%s) : ok version %s installed" % (requirement_project,
requirement_relation,
requirement_version,
package[0].version))
package[0]["version"]))
else:
log.info("Look for package %s : ok version %s installed" % (requirement_project,
package[0].version))
package[0]["version"]))
else:
if len(package)!=1:
log.info("Look for package %s (%s%s) : not installed" % (requirement_project,
requirement_relation,
requirement_version))
if requirement_version is not None and requirement_relation is not None:
log.info("Look for package %s (%s%s) : not installed" % (requirement_project,
requirement_relation,
requirement_version))
else:
log.info("Look for package %s : not installed" % requirement_project)
else:
log.info("Look for package %s (%s%s) : failed only version %s installed" % (requirement_project,
requirement_relation,
requirement_version,
package[0].version))
package[0]["version"]))
return rep
......@@ -86,7 +92,7 @@ def install_requirements(requirementfile='requirements.txt'):
ok = is_installed(x)
if not ok:
log.info(" Installing requirement : %s" % x)
pip_install_package(x)
pip_install_package(x,requirement=requirementfile)
install_something=True
if x[0:3]=='pip':
return True
......@@ -139,8 +145,9 @@ def get_package_requirement(package,requirementfile='requirements.txt'):
return None
def pip_install_package(package,directory=None,upgrade=True):
def pip_install_package(package,directory=None,requirement=None):
pipcommand = os.path.join(os.path.dirname(sys.executable),'pip')
if directory is not None:
log.info(' installing %s in directory %s' % (package,str(directory)))
......@@ -150,8 +157,9 @@ def pip_install_package(package,directory=None,upgrade=True):
args = ['install']
if upgrade:
args.append('--upgrade')
if requirement:
args.append('--requirement')
args.append(requirement)
if 'https_proxy' in os.environ:
args.append('--proxy=%s' % os.environ['https_proxy'])
......@@ -161,5 +169,7 @@ def pip_install_package(package,directory=None,upgrade=True):
args.append(package)
return pip.main(args)
pip = subprocess.run([pipcommand] + args)
return pip
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment