2016-12-28 1 views
-6

통합 테스트를 위해 제공된 단위 테스트를 디버깅하려고합니다.왜 파이썬이 bash 코드를 실행하지 않습니까?

전 로컬 컴퓨터에서이 파일을 테스트했지만 파일이 변경되지 않았으므로 그 이후로 변경된 사항을 알 수 없습니다.

독점 소프트웨어이기 때문에 원래의 단위 테스트에서 주석을 제거하고 일부 이름을 변경했습니다.

구문 오류는 다음과 같습니다

File "unitTests.sh", line 39 
    gLastFullPath=`python -c "import os; print os.path.realpath('${1}')"` 
       ^
SyntaxError: invalid syntax 

전체 스크립트는 여기에 있습니다 :

#!/bin/bash 

# If non-zero, then run in debug mode, outputting debug information 
debug=0 

# Set the following to 1 to force an error for testing purposes 
forceError=0 

separator="====================================================================================================" 

#------------------------------------------------------------------------------- 
# Convert the specified path to a full path and return it in the gLastFullPath 
# global variable. 
# 
# Input params: 
# $1 - Path to convert to full 
# 
# Output params: 
# $gLastFullPath - Set to the converted full path 
gLastFullPath="" 
getFullPath() 
{ 
    # Use Python (because it's easier than Bash) to convert the passed path to 
    # a full path. 
    gLastFullPath=`python -c "import os; print os.path.realpath('${1}')"` 
} 

#------------------------------------------------------------------------------- 
fatalError() 
{ 
    echo "${separator}" 
    echo "Fatal Error: $1" 
    echo "${separator}" 
    exit 1 
} 

#------------------------------------------------------------------------------- 
# If a file or folder exists at the specified path, then delete it. If it's a 
# directory, then its entire contents is deleted. 
#------------------------------------------------------------------------------- 
deleteIfExists() 
{ 
    if [[ 0 -ne $debug ]]; then 
     echo "deleteIfExists called..." 
    fi 

    if [[ -e "$1" ]]; then 
     # If it's a directory, then make sure it contains no locked files 
     if [[ -d "$1" ]]; then 
      chflags -R nouchg "$1" 
     fi 

     if [[ 0 -ne $debug ]]; then 
      echo " Deleting the existing file or directory:" 
      echo " $1" 
     fi 

     # Do the remove and check for an error. 
     /bin/rm -rf "$1" 
     if [[ $? -ne 0 ]]; then 
      fatalError "Unable to delete $1." 
     fi 
    fi 

    if [[ 0 -ne $debug ]]; then 
     echo 
    fi 
} 


#------------------------------------------------------------------------------- 
# Script starts here 
#------------------------------------------------------------------------------- 

# Get the full path to this script 
scriptPath=`which "$0"` 
getFullPath "${scriptPath}" 
scriptFullPath="${gLastFullPath}" 
scriptDir=`dirname "${scriptFullPath}"` 
scriptName=`basename "${scriptFullPath}"` 

if [[ 0 -ne $debug ]]; then 
    echo "$scriptName: Debug tracing is on." 
    echo 
fi 

# Get the SDK project root path 
getFullPath "${scriptDir}/.." 
projRoot="${gLastFullPath}" 

# Get the top of the server tree 
getFullPath "${projRoot}/SUBSYS_TOP" 
subsysTop="${gLastFullPath}" 

libPythonBase="${projRoot}/src/lib/py/devilsoftPy" 
devilsoftPython="${libPythonBase}/devilsoftpy" 

if [[ 0 -ne $debug ]]; then 
    echo "$scriptName: Project root dir:  \"${projRoot}\"" 
    echo "$scriptName: SUBSYS_TOP:   \"${subsysTop}\"" 
    echo "$scriptName: Lib python base:  \"${libPythonBase}\"" 
    echo "$scriptName: devilsoft python: \"${devilsoftPython}\"" 
    echo 
fi 

# First we have to launch the test python server. This is used by some of the other client tests to 
# run against. 
testServer="${devilsoftPython}/test/TestServer.py" 
if [[ ! -f "${testServer}" ]]; then 
    fatalError "Could not find the expected test server: \"${testServer}\"" 
fi 

# Carve out a place for our test server log file 
tempFolder="/tmp/devilsoft" 
mkdir -p "${tempFolder}" 
testServerLogFile="${tempFolder}/TestServer.log" 

echo "Starting the test server: \"${testServer}\"" 
echo " Logging to this file: \"${testServerLogFile}\"" 
export PYTHONPATH="${libPythonBase}:${PYTHONPATH}"; "${testServer}" > "${testServerLogFile}" 2>&1 & 
testServerPid=$! 
echo " Server started with pid ${testServerPid}..." 
echo 

echo " Taking a little snooze to let the test server initialize..." 
sleep 2 

# If we're forcing errors for testing, then kill the test server. This will cause downstream scripts 
# to fail because there will be no server to talk to. 
if [[ $forceError -ne 0 ]]; then 
    echo "Forcing downstream errors by killing the test server..." 
    kill ${testServerPid} 
    wait ${testServerPid} 
    testServerPid=0 
    echo 
fi 

testResultsLogFile="${tempFolder}/TestResults.log" 
echo "Testing each python script in the library..." 
echo " Test results will be written to this log file: \"${testResultsLogFile}\"" 
echo 
deleteIfExists "${testResultsLogFile}" 

# Save and set the field separator so that we can handle spaces in paths 
SAVEIFS=$IFS 
IFS=$'\n' 

failedScripts=() 
lastError=0 
pythonSources=($(find "${devilsoftPython}" -name '*.py' ! -name '*.svn*' ! -name '__init__.py' ! -name 'TestServer.py' ! -name 'ServerClient.py')) 
for pythonSourceFile in ${pythonSources[*]}; do 
    echo " Testing python source \"${pythonSourceFile}\"" 
    export PYTHONPATH="${libPythonBase}:${PYTHONPATH}"; "${pythonSourceFile}" >> "${testResultsLogFile}" 2>&1 
    result=$? 
    if [[ $result -ne 0 ]]; then 
     pythonSourceName=`basename "${pythonSourceFile}"` 
     echo " Error ${result} returned from the above script ${pythonSourceName}!" 
     lastError=${result} 
     failedScripts+=("${pythonSourceFile}") 
    fi 
done 
echo 

# Restore the original field separator 
IFS=$SAVEIFS 

if [[ ${testServerPid} -ne 0 ]]; then 
    echo "Telling the test server to quit..." 
    kill ${testServerPid} 
    wait ${testServerPid} 
    echo 
fi 

# If we got an error, tell the user 
if [[ $lastError -ne 0 ]]; then 
    echo "IMPORTANT! The following scripts failed with errors:" 
    for failedScript in "${failedScripts[@]}"; do 
     echo " \"${failedScript}\"" 
    done 
    echo 

    fatalError "Review the log files to figure out why the above scripts failed." 
fi 

echo "${separator}" 
echo " Hurray! All tests passed!" 
echo "${separator}" 
echo 

exit 0 

모든 파이썬에서 실행되는 2.7

+9

이것은 나에게 파이썬 코드처럼 보이지 않습니다. 파이썬은 함수 본문을 둘러싸 기 위해 중괄호를 사용하지 않습니다. 첫번째 줄에는'#!/bin/bash'라고 쓰여 있습니다. 이 스크립트가 bash 스크립트가 아닌 것은 확실합니까? – Kevin

+3

다음은 의심되는 부분입니다. OP는 파이썬 인터프리터를 사용하여이 파일을 실행하려고하기 때문에 파이썬 오류가 발생합니다. "내가 로컬 컴퓨터에서이 테스트를 마지막으로 수행했음을 확신합니다"라고 말하면서 마지막으로 테스트 한 결과 bash로 실행했습니다. – Kevin

+1

Ahh yes : 이전의 모든 명령문이 올바른 파이썬이기도합니다. 이상한. – RemcoGerlich

답변

2

이것은 bash는 스크립트가 아닌 파이썬입니다 스크립트. python script_name.sh 대신 ./script_name.sh 또는 bash script_name.sh을 사용하여 실행하십시오.

+1

필자는 너무 피곤해서 바보가되어 파이썬에서 쉘 스크립트를 실행하려고한다는 것을 알아 차렸다. 도움을 주셔서 감사합니다! ;) – Yoda

관련 문제