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

Improvements/python linter harness #751

60 changes: 46 additions & 14 deletions travisci/python-linter_harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,61 @@
# See the License for the specific language governing permissions and
# limitations under the License.

PYTHON_SOURCE_LOCATIONS=('scripts' 'src/python')

# Setup the environment variables
# shellcheck disable=SC2155
export PYTHONPATH=$PYTHONPATH:$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
# more info: https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules
export MYPYPATH=$MYPYPATH:src/python/lib

PYLINT_OUTPUT_FILE=$(mktemp)
PYLINT_ERRORS=$(mktemp)
# CITest project is on hold and it needs to be updated before resuming its linter checker
find "${PYTHON_SOURCE_LOCATIONS[@]}" -type f -name "*.py" \! -name "Ortheus.py" \! -name "*citest*.py" \! -path "*/citest/*" -print0 | xargs -0 pylint --rcfile pyproject.toml --verbose | tee "$PYLINT_OUTPUT_FILE"
grep -v "\-\-\-\-\-\-\-\-\-" "$PYLINT_OUTPUT_FILE" | grep -v "Your code has been rated" | grep -v "\n\n" | sed '/^$/d' > "$PYLINT_ERRORS"
! [ -s "$PYLINT_ERRORS" ]
# Function to run pylint
run_pylint() {
local pylint_output_file=$(mktemp)

# Run pylint, excluding specific files and directories
find "${PYTHON_SOURCE_LOCATIONS[@]}" -type f -name "*.py" \
\! -name "Ortheus.py" \
\! -name "*citest*.py" \
\! -path "*/citest/*" -print0 |
xargs -0 pylint --rcfile=pyproject.toml --verbose \
--msg-template='COMPARA_PYLINT_MSG:{path}:{line}:{column}: {msg_id}: {msg} ({symbol})' |
tee "$pylint_output_file"

# Return 1 if pylint messages were found, otherwise 0
thiagogenez marked this conversation as resolved.
Show resolved Hide resolved
# -c option counts the number of matches, -m 1 stops after the first match to optimize performance,
local result=$(grep -c -m 1 -E '^COMPARA_PYLINT_MSG:' "$pylint_output_file")

# Cleanup
rm "$pylint_output_file"

return "$result"
}

# Function to run mypy, excluding certain files and paths, and capturing the outcome
run_mypy() {
find "${PYTHON_SOURCE_LOCATIONS[@]}" -type f -name "*.py" \
\! -name "Ortheus.py" \
\! -name "*citest*.py" \
\! -path "*/citest/*" -print0 |
xargs -0 mypy --config-file pyproject.toml --namespace-packages --explicit-package-bases
}

# Define Python source locations
PYTHON_SOURCE_LOCATIONS=('scripts' 'src/python')

# Run pylint and mypy, capturing their return codes
run_pylint
rt1=$?
rm "$PYLINT_OUTPUT_FILE" "$PYLINT_ERRORS"

# CITest project is on hold and it needs to be updated before resuming its static type checker
find "${PYTHON_SOURCE_LOCATIONS[@]}" -type f -name "*.py" \! -name "Ortheus.py" \! -name "*citest*.py" \! -path "*/citest/*" -print0 | xargs -0 mypy --config-file pyproject.toml --namespace-packages --explicit-package-bases
run_mypy
rt2=$?

if [[ ($rt1 -eq 0) && ($rt2 -eq 0) ]]; then
exit 0
# Determine exit code based on results
if [[ $rt1 -eq 0 && $rt2 -eq 0 ]]; then
exit 0 # success
elif [[ $rt1 -ne 0 ]]; then
exit 1 # pylint error
elif [[ $rt2 -ne 0 ]]; then
exit 2 # mypy error
else
exit 255
exit 3 # error on both
fi