Skip to content

Commit 9133418

Browse files
committed
fix: change the pip runner string based on a pip version check
- if pip version is less than 19.3, choose to run main directly as its a function, whereas with pip versions starting with 19.3, execute the function `main` inside the pip.main module
1 parent fc2508d commit 9133418

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

aws_lambda_builders/workflows/python_pip/compat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ def pip_import_string(python_exe):
2121
return 'from pip._internal import main'
2222

2323

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+
2441
if os.name == 'nt':
2542
# windows
2643
# This is the actual patch used on windows to prevent distutils from

aws_lambda_builders/workflows/python_pip/packager.py

Lines changed: 3 additions & 2 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
12+
from .compat import pip_import_string, pip_runner_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,6 +553,7 @@ 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)
556557
self._import_string = import_string
557558

558559
def main(self, args, env_vars=None, shim=None):
@@ -561,7 +562,7 @@ def main(self, args, env_vars=None, shim=None):
561562
if shim is None:
562563
shim = ''
563564
run_pip = (
564-
'import sys; %s; sys.exit(main(%s))'
565+
self.run_pip_string
565566
) % (self._import_string, args)
566567
exec_string = '%s%s' % (shim, run_pip)
567568
invoke_pip = [self.python_exe, '-c', exec_string]

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)