fix: unable to launch application with non path exec field

Only check for situations without shebang

Issue: https://github.com/linuxdeepin/developer-center/issues/8682
This commit is contained in:
zhangkun 2024-05-21 17:36:34 +08:00 committed by Comix
parent b2c0e41631
commit 4267b3b4b7

View File

@ -49,34 +49,33 @@ function log.debug() { log.log "debug" $@; }
function check_file_no_shebang(){ function check_file_no_shebang(){
local file_type=$(file -b "${1}") local file_type=$(file -b "${1}")
# 检查文件类型是否为纯文本文件、是否具有可执行权限,并且不是脚本文件 # 文件类型是纯文本文件、有可执行权限并且不是脚本文件则可能忘记加上shebang
if [[ ($file_type == *"ASCII text"* || $file_type == *"Unicode text"*) && -x "$exec_cmd_origin_path" && "$file_type" != *"script"* ]]; then if [[ ($file_type == *"ASCII text"* || $file_type == *"Unicode text"*) && -x "$exec_cmd_origin_path" && "$file_type" != *"script"* ]]; then
true true
else else
false false
fi fi
} }
function check_file_is_python(){ function check_file_is_python(){
if file -b "${1}" | grep -q "Python script" ;then if file -b "${1}" | grep -q "Python script" ;then
true true
else else
false false
fi fi
} }
function choose_python(){ function choose_python(){
pythonInterpreter=$(command -v python3 || command -v python) pythonInterpreter=$(command -v python3 || command -v python)
if [ -z "${pythonInterpreter}" ]; then if [ -z "${pythonInterpreter}" ]; then
log.error "Python interpreter not found." log.warn "Python interpreter not found. Trying to continue"
exit 1 exec "$@"
fi fi
log.debug "Automatically add interpreter ${pythonInterpreter} to launch app." log.debug "Automatically add interpreter ${pythonInterpreter} to launch app."
} }
exec_cmd_origin_path=$(realpath $1) exec_cmd_origin_path=$1
if [[ -e ${exec_cmd_origin_path} ]]; then if [[ -e ${exec_cmd_origin_path} ]]; then
@ -86,22 +85,15 @@ if [[ -e ${exec_cmd_origin_path} ]]; then
##### 2. If do not need to fix,just try to run. ##### 2. If do not need to fix,just try to run.
if check_file_no_shebang "${exec_cmd_origin_path}" ; then if check_file_no_shebang "${exec_cmd_origin_path}" ; then
log.debug "The executable is a script without shebang. Trying to fix..." log.debug "The executable is a script without shebang. Trying to fix..."
exec bash -c "$@"
if check_file_is_python "${exec_cmd_origin_path}" ; then elif check_file_is_python "${exec_cmd_origin_path}" ; then
choose_python choose_python
exec ${pythonInterpreter} "$@"
exec ${pythonInterpreter} "$@"
else
exec /bin/bash "$@"
fi
else else
exec "$@" exec "$@"
fi fi
else else
log.error "File not found." log.info "${exec_cmd_origin_path} maybe not an absolute path. Continue"
exit 1 exec "$@"
fi fi