Skip to content

Commit c1f09ba

Browse files
committed
fix: adopt same methodology as chalice for determining pip import string
1 parent 9133418 commit c1f09ba

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

aws_lambda_builders/workflows/python_pip/compat.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,27 @@ def pip_import_string(python_exe):
88
cmd = [
99
python_exe,
1010
"-c",
11-
"import pip; assert int(pip.__version__.split('.')[0]) <= 9"
11+
"import pip; print(pip.__version__)"
1212
]
13-
p = os_utils.popen(cmd,stdout=os_utils.pipe, stderr=os_utils.pipe)
14-
p.communicate()
13+
p = os_utils.popen(cmd, stdout=os_utils.pipe, stderr=os_utils.pipe)
14+
stdout, stderr = p.communicate()
15+
pip_version = stdout.decode('utf-8').strip()
16+
pip_major_version = int(pip_version.split('.')[0])
17+
pip_minor_version = int(pip_version.split('.')[1])
18+
1519
# Pip moved its internals to an _internal module in version 10.
1620
# In order to be compatible with version 9 which has it at at the
1721
# top level we need to figure out the correct import path here.
18-
if p.returncode == 0:
22+
if pip_major_version == 9:
1923
return 'from pip import main'
24+
# Pip changed their import structure again in 19.3
25+
# https://github.com/pypa/pip/commit/09fd200
26+
elif pip_major_version >= 19 and pip_minor_version >= 3:
27+
return 'from pip._internal.main import main'
2028
else:
2129
return 'from pip._internal import main'
2230

2331

24-
def pip_runner_string(python_exe):
25-
os_utils = OSUtils()
26-
cmd = [
27-
python_exe,
28-
"-c",
29-
"import pip; assert (int(pip.__version__.split('.')[0]) <= 19 and int(pip.__version__.split('.')[1]) < 3)"
30-
]
31-
p = os_utils.popen(cmd, stdout=os_utils.pipe, stderr=os_utils.pipe)
32-
p.communicate()
33-
# Pip changed main to be a module instead of a function from 19.3
34-
# and added a separate main function within the main module.
35-
if p.returncode == 0:
36-
return 'import sys; %s; sys.exit(main(%s))'
37-
else:
38-
return 'import sys; %s; sys.exit(main.main(%s))'
39-
40-
4132
if os.name == 'nt':
4233
# windows
4334
# This is the actual patch used on windows to prevent distutils from

aws_lambda_builders/workflows/python_pip/packager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from email.parser import FeedParser
1010

1111

12-
from .compat import pip_import_string, pip_runner_string
12+
from .compat import pip_import_string
1313
from .compat import pip_no_compile_c_env_vars
1414
from .compat import pip_no_compile_c_shim
1515
from .utils import OSUtils
@@ -553,7 +553,6 @@ def __init__(self, osutils=None, python_exe=None, import_string=None):
553553
self.python_exe = python_exe
554554
if import_string is None:
555555
import_string = pip_import_string(python_exe=self.python_exe)
556-
self.run_pip_string = pip_runner_string(python_exe=self.python_exe)
557556
self._import_string = import_string
558557

559558
def main(self, args, env_vars=None, shim=None):
@@ -562,7 +561,7 @@ def main(self, args, env_vars=None, shim=None):
562561
if shim is None:
563562
shim = ''
564563
run_pip = (
565-
self.run_pip_string
564+
'import sys; %s; sys.exit(main(%s))'
566565
) % (self._import_string, args)
567566
exec_string = '%s%s' % (shim, run_pip)
568567
invoke_pip = [self.python_exe, '-c', exec_string]

0 commit comments

Comments
 (0)