Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -m and -c in interpreter mode. #563

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,29 @@ def demote_bootstrap(cls):
TRACER.log(' * - paths that do not exist or will be imported via zipimport')

def execute_interpreter(self):
if sys.argv[1:]:
program = sys.argv[1]
try:
if program == '-':
content = sys.stdin.read()
else:
with open(program) as fp:
content = fp.read()
except IOError as e:
die("Could not open %s in the environment [%s]: %s" % (program, sys.argv[0], e))
sys.argv = sys.argv[1:]
self.execute_content(program, content)
args = sys.argv[1:]
if args:
# NB: We take care here to setup sys.argv to match how CPython does it for each case.
arg = args[0]
if arg == '-c':
content = args[1]
sys.argv = ['-c'] + args[2:]
self.execute_content('-c <cmd>', content, argv0='-c')
elif arg == '-m':
module = args[1]
sys.argv = args[1:]
self.execute_module(module)
else:
try:
if arg == '-':
content = sys.stdin.read()
else:
with open(arg) as fp:
content = fp.read()
except IOError as e:
die("Could not open %s in the environment [%s]: %s" % (arg, sys.argv[0], e))
sys.argv = args
self.execute_content(arg, content)
else:
self.demote_bootstrap()

Expand Down
56 changes: 54 additions & 2 deletions tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,40 @@ def test_activate_interpreter_different_from_current():
pytest.fail('PEX activation of %s failed with %s' % (pex, e))


def test_execute_interpreter_dashc_program():
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot)
pex_builder.freeze()
process = PEX(pex_chroot).run(args=['-c', 'import sys; print(" ".join(sys.argv))', 'one'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
blocking=False)
stdout, stderr = process.communicate()

assert 0 == process.returncode
assert b'-c one\n' == stdout
assert b'' == stderr


def test_execute_interpreter_dashm_module():
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot)
with temporary_file(root_dir=pex_chroot) as fp:
fp.write(b'import sys; print(" ".join(sys.argv))')
fp.close()
pex_builder.add_source(fp.name, 'foo/bar.py')
pex_builder.freeze()
process = PEX(pex_chroot).run(args=['-m', 'foo.bar', 'one', 'two'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
blocking=False)
stdout, stderr = process.communicate()

assert 0 == process.returncode
assert b'foo.bar one two\n' == stdout
assert b'' == stderr


def test_execute_interpreter_stdin_program():
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot)
Expand All @@ -356,13 +390,31 @@ def test_execute_interpreter_stdin_program():
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
blocking=False)
stdout, stderr = process.communicate(input=b'import sys; print(" ".join(sys.argv[1:]))')
stdout, stderr = process.communicate(input=b'import sys; print(" ".join(sys.argv))')

assert 0 == process.returncode
assert b'one two\n' == stdout
assert b'- one two\n' == stdout
assert b'' == stderr


def test_execute_interpreter_file_program():
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot)
pex_builder.freeze()
with temporary_file(root_dir=pex_chroot) as fp:
fp.write(b'import sys; print(" ".join(sys.argv))')
fp.close()
process = PEX(pex_chroot).run(args=[fp.name, 'one', 'two'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
blocking=False)
stdout, stderr = process.communicate()

assert 0 == process.returncode
assert '{} one two\n'.format(fp.name).encode('utf-8') == stdout
assert b'' == stderr


def test_pex_run_custom_setuptools_useable():
with temporary_dir() as resolve_cache:
dists = resolve(['setuptools==36.2.7'], cache=resolve_cache)
Expand Down