IMPALA-11951: Add tools for checking/fixing python 3 syntax

This adds the bin/check-python-syntax.sh script, which
runs "python -m compileall" for all python files in
Impala with both python2 and python3. This detects
syntax errors in the python files. This will be
incorporated into precommit once it is clean.

This also adds future to the impala-python virtualenv.
This provides the futurize script (exposed via
impala-futurize), which can be used to automatically
fix some py2/py3 issues. Future also provides the
builtins library, which can provide python 3
functionality on python 2.

Testing:
 - Ran impala-futurize locally
 - Ran the script repeatedly while fixing syntax errors

Change-Id: Iae2c51bc6ddc9b6a04469ee1b8284227fed3bd45
Reviewed-on: http://gerrit.cloudera.org:8080/19550
Reviewed-by: Michael Smith <michael.smith@cloudera.com>
Tested-by: Michael Smith <michael.smith@cloudera.com>
This commit is contained in:
Joe McDonnell
2023-02-26 13:04:52 -08:00
parent 2ae0e4139c
commit ff62a4df39
3 changed files with 93 additions and 0 deletions

71
bin/check-python-syntax.sh Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -euo pipefail
pushd ${IMPALA_HOME} > /dev/null 2>&1
RETCODE=0
for file in $(git ls-files '**/*.py'); do
# Skip the shell's ext-py code
if [[ "${file}" =~ "shell/ext-py" ]]; then
continue
fi
# Skip the shell's pkg_resources.py
if [[ "${file}" == "shell/pkg_resources.py" ]]; then
continue
fi
# Python 2 checks
# -l = no recursion
# -q = only print errors
# -f = force recompile
if ! python2 -m compileall -l -q -f ${file} > /dev/null 2>&1; then
RETCODE=1
echo "Python 2 compilation failed for ${file}:"
set +e
python2 -m compileall -l -q -f ${file}
set -e
fi
# Clean up the .pyc files generated by compilation
if [[ -f "${file}c" ]]; then
rm "${file}c"
fi
# Python 3 checks
# -l = no recursion
# -q = only print errors
# -f = force recompile
if ! python3 -m compileall -l -q -f ${file} > /dev/null 2>&1 ; then
RETCODE=1
echo "Python 3 compilation failed for ${file}:"
set +e
python3 -m compileall -l -q -f ${file}
set -e
fi
# Clean up the __pycache__ directories generated by compilation
py_cache_dir="$(dirname ${file})/__pycache__"
if [[ -d "${py_cache_dir}" ]]; then
rm -rf ${py_cache_dir}
fi
done
popd > /dev/null 2>&1
exit ${RETCODE}

21
bin/impala-futurize Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
source "$(dirname "$0")/impala-python-common.sh"
exec "$PY_ENV_DIR/bin/futurize" "$@"

View File

@@ -37,6 +37,7 @@ flake8 == 3.9.2
contextlib2 == 0.6.0
pathlib2 == 2.3.7.post1
zipp == 1.2.0
future == 0.18.3
gcovr == 4.2
Jinja2 == 2.11.3
MarkupSafe == 1.1.1