Files
ci-emsdk/python_selector
Piotr Paczkowski (trzeci.eu) 29b91e38e0 python_selector checks if found python command is valid (#275)
Addresses emscripten-core/emscripten#8792

I've found what's the reason of the strange behaviour of last changes (#273).

In order to reproduce this problem it's needed:

    To don't have python3 installed in system
    To have pyenv with installed at least one version from each family, like:

pyenv install 3.7.0
pyenv install 2.7.15

    To activate a version from python2

pyenv local 2.7.15

Then we have a strange situation that python3 is available as a command, but it's just a mock for pyenv:

➜ pyenv versions
  system
* 2.7.15 (set by /home/trzeci/Projects/emsdk/.python-version)
  3.7.0

➜ which python3
~/.pyenv/shims/python3

➜ python3 --version
pyenv: python3: command not found

The `python3' command exists in these Python versions:
  3.7.0

➜ ./emsdk --help        
pyenv: python3: command not found

The `python3' command exists in these Python versions:
  3.7.0

As you see in an example above I didn't even call emsdk script, just tried to execute python3 --version and I've got exactly the same error. The problem of pyenv is that even if python3 isn't active it's available in the system PATH.
Calling ./emsdk --help get's the same error, as python3 was used.

At the moment this entirely breaks logic in python_selector and in pyenv working command is indistinguishable from stub python command.

@kripken: A couple of ways to go from here:

    revert logic of python_selector and advertise mac users to use python3
    keep logic and advertise pyenv users to use python3
    This PR: extend logic of python_selector by checking if found python command is valid (for the instance by calling python --version), this will filter out false positive matches that coming from pyenv

After patch:

➜ pyenv versions        
  system
* 2.7.15 (set by /home/trzeci/Projects/emsdk/.python-version)
  3.7.0

➜ which python3 
~/.pyenv/shims/python3

➜ python3 --version
pyenv: python3: command not found

The `python3' command exists in these Python versions:
  3.7.0


➜ ./emsdk --help
 emsdk: Available commands:

   emsdk list [--old] [--uses]  - Lists all available SDKs and tools and their
                                  current installation status. With the --old
   ....

As you can see python3 --version command fails as it was, but that's expected behaviour. What's important is that ./emsdk --help gets executed as python_selector recognizes a stub.
2019-06-13 15:47:29 -07:00

34 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2019 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
"""Provides a way to run a script on the preferred Python version"""
import os
import subprocess
import sys
def which(program):
for path in [""] + os.environ.get("PATH", "").split(os.pathsep):
exe_file = os.path.join(path, program)
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
# in case of pyenv we might have `python3` command available, but it's just a stub
# https://github.com/emscripten-core/emscripten/issues/8792
p = subprocess.Popen([exe_file, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
if p.returncode == 0:
return exe_file
return None
# Look for the best choice for python, favours Python3 over Python2
# In case of Windows it uses always python that was used for this script
if sys.platform in ["linux", "linux2", "darwin"]:
python = which("python3") or which("python") or sys.executable
else:
python = sys.executable
sys.exit(subprocess.call([python] + sys.argv[1:]))