diff options
author | Jim Tang <jimtang@google.com> | 2021-04-16 03:43:19 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-04-16 03:43:19 +0000 |
commit | 01e170749f094d974345a81d1a3025a55a3fbed3 (patch) | |
tree | b217ad3709083906b43f679456f47ce4b1a08f4f | |
parent | 1b1f76188ffec5dc78b6df8382a6841c74ed9ae5 (diff) | |
parent | 920b89c58a930035869d77918db70413ff17282c (diff) | |
download | asuite-01e170749f094d974345a81d1a3025a55a3fbed3.tar.gz |
Prevent "/usr/bin/env: 'python': No such file or directory" error. am: 078b9d3bbc am: f32100cdae am: 920b89c58a
Original change: https://android-review.googlesource.com/c/platform/prebuilts/asuite/+/1675588
Change-Id: I79db9d6d320d0f97f589857c3b5526be1ae75353
-rwxr-xr-x | atest/linux-x86/atest | 103 |
1 files changed, 46 insertions, 57 deletions
diff --git a/atest/linux-x86/atest b/atest/linux-x86/atest index 6fa785b..92aef34 100755 --- a/atest/linux-x86/atest +++ b/atest/linux-x86/atest @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env bash # Copyright 2020, The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,62 +13,51 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function +ATEST_DIR=$(dirname $0) +[ "$(uname -s)" == "Darwin" ] && { realpath(){ echo "$(cd $(dirname $1);pwd -P)/$(basename $1)"; }; } -import os -import subprocess +function get_python_version() { + python - << END import sys +print(sys.version_info.major) +END +} -# Version and disclaimers. -MIN_PY3_VERSION = (3, 5) -PYTHON = 'python3' -HELPER = ('Please install python {}+ for' - ' running the latest atest.').format(MIN_PY3_VERSION) -FALLBACK_MSG = ('[Warning] You are running a FALLBACK version of atest!' - '\nThis fallback version of atest will be NO LONGER MAINTAINED/BUG FIXING!' - '\n{}').format(HELPER) -DISCLAIMER_MSG = '[Warning] Outdated version of python3 detected!\n{}'.format(HELPER) -ATEST_DIR = os.path.dirname(os.path.realpath(__file__)) -ATEST = os.path.join(ATEST_DIR, 'atest-py3') -FALLBACK_ATEST = os.path.join(ATEST_DIR, 'atest-py2') - - -def print_message(context, separator='#'): - """A printer function with separator. - - Args: - context: A string of context to print. - separator: A string which will be used as separator char. - """ - print('\n' + separator*72) - print(context) - print(separator*72 + '\n') - - -def has_valid_python3(version): - proc = subprocess.Popen( - [PYTHON, '-c', 'import sys; ' - 'print(sys.version_info.major, sys.version_info.minor)'], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (output, _) = proc.communicate() - current_ver = tuple(int(x) for x in output.decode('utf-8').split()) - return current_ver >= version - - -def has_command(cmd): - paths = os.getenv('PATH', '').split(':') - for path in paths: - if os.path.isfile(os.path.join(path, cmd)): - return True - return False - - -if __name__ == '__main__': - if not has_command(PYTHON): - print_message(FALLBACK_MSG) - os.execv(FALLBACK_ATEST, sys.argv) - else: - # Print warning if the installed python3 doesn't meet the requirement. - if not has_valid_python3(MIN_PY3_VERSION): - print_message(DISCLAIMER_MSG) - os.execv(ATEST, sys.argv) +function is_python3() { + python3 - << END +import sys +current_version = (sys.version_info.major, sys.version_info.minor) +if current_version >= (3, 4): + print(True) +else: + print(False) +END +} + +# 1. Expect in most Linux that comes with python3. +if which python3 >/dev/null 2>&1; then + if [[ x$(is_python3) == x"False" ]]; then + echo "[Warning] Outdated version of python3 detected!" + echo "Please install python 3.5+ to run the latest atest." + fi + ATEST="$(realpath $ATEST_DIR)/atest-py3" +# 2. For legacy mechines that only have python2 +elif which python2 >/dev/null 2>&1; then + ATEST="$(realpath $ATEST_DIR)/atest-py2" + echo "[Warning] You are running a FALLBACK version of atest!" + echo "This fallback version of atest will be NO LONGER MAINTAINED/BUG FIXING!" +# In some distros there are no explicit python2/python3 executables. +elif which python >/dev/null 2>&1; then + if [[ "$(get_python_version)" -eq 3 ]]; then + echo "Please point $(which python) to /usr/bin/python3 to run atest." + else + echo "Please point $(which python) to /usr/bin/python2 to run atest." + fi + exit 0 +# Unlikely to happen, exit if no python/python2/python3 executables at all. +else + echo "Please install python 3.5+ to run the latest atest." + exit 0 +fi + +$ATEST "$@" |