Skip to content

Commit 24d16a3

Browse files
authored
Merge pull request #132 from TheSriram/python_pip_builder_pip19.3_fix
fix: change the pip runner string based on a pip version check
2 parents fc2508d + c1f09ba commit 24d16a3

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

aws_lambda_builders/workflows/python_pip/compat.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ 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

tests/unit/workflows/python_pip/test_packager.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from collections import namedtuple
3+
from unittest import TestCase
34

45
import mock
56
import pytest
@@ -70,7 +71,7 @@ def osutils():
7071

7172
class FakePopen(object):
7273
def __init__(self, rc, out, err):
73-
self.returncode = 0
74+
self.returncode = rc
7475
self._out = out
7576
self._err = err
7677

@@ -303,14 +304,24 @@ def test_inject_unknown_error_if_no_stderr(self, pip_factory):
303304
assert str(einfo.value) == 'Unknown error'
304305

305306

306-
class TestSubprocessPip(object):
307+
class TestSubprocessPip(TestCase):
307308
def test_does_use_custom_pip_import_string(self):
308309
fake_osutils = FakePopenOSUtils([FakePopen(0, '', '')])
309310
expected_import_statement = 'foobarbaz'
310311
pip = SubprocessPip(osutils=fake_osutils,
311-
import_string=expected_import_statement)
312+
import_string=expected_import_statement,
313+
python_exe=sys.executable)
312314
pip.main(['--version'])
313315

314316
pip_execution_string = fake_osutils.popens[0][0][0][2]
315317
import_statement = pip_execution_string.split(';')[1].strip()
316318
assert import_statement == expected_import_statement
319+
320+
def test_check_pip_runner_string_pip(self):
321+
fake_osutils = FakePopenOSUtils([FakePopen(0, '', '')])
322+
pip = SubprocessPip(osutils=fake_osutils,
323+
python_exe=sys.executable)
324+
pip.main(['--version'])
325+
326+
pip_runner_string = fake_osutils.popens[0][0][0][2].split(";")[-1:][0]
327+
self.assertIn("main", pip_runner_string)

0 commit comments

Comments
 (0)