-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mila-iqia/staged_pre_commit
New pre-commit script
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/bin/sh | ||
|
||
# exit at the first error | ||
set -e | ||
|
||
# flake8 linting | ||
|
||
# Only extract the python files that git has staged; no need to lint WIP files that are not staged. | ||
|
||
# We'll use files to use intermediate results; it's easier to use grep on files. | ||
staged_file_name="staged_file.tmp" | ||
python_staged_file_name="python_staged_file.tmp" | ||
|
||
git diff --name-only --cached > $staged_file_name | ||
|
||
# Grep has the following behavior: | ||
# the exit code is 0 if there is a match | ||
# the exit code is 1 if there is no match | ||
# the exit code is >1 if there is an error | ||
# | ||
# No match is a legitimate result, but the exit code of 1 is interpreted as an error by bash. | ||
# To work around this issue, we'll use | ||
# grep [...] || [[ $? == 1]] | ||
# which has an exit code of 0 if there is a match or if there are no match. | ||
|
||
grep '.*py$' $staged_file_name > $python_staged_file_name || [[ $? == 1 ]] | ||
python_staged_files=`cat $python_staged_file_name` | ||
|
||
python_non_tests_staged_files=`grep -v '^tests' $python_staged_file_name || [[ $? == 1 ]]` | ||
|
||
rm $python_staged_file_name $staged_file_name | ||
|
||
|
||
if [ -z "${python_staged_files}" ]; then | ||
echo "No python file to lint" | ||
else | ||
flake8 --ignore D,W503 --max-line-length=120 $python_staged_files # Check everything but docstrings | ||
isort --check $python_staged_files # Check imports | ||
fi | ||
|
||
if [ -z "${python_non_tests_staged_files}" ]; then | ||
echo "No non-test python file to docstring lint" | ||
else | ||
flake8 --select D --ignore D104,D100,D401 --docstring-convention google --exclude tests/ $python_non_tests_staged_files # Check only the docstrings | ||
isort --check $python_non_tests_staged_files # Check imports | ||
fi | ||
|
||
|
||
# Raise error if any staged notebooks contain outputs | ||
GITDIR=$(git rev-parse --show-toplevel) # Full path to git working directory | ||
IPYNB_FILES=$(git diff --name-only --cached | grep .ipynb || true) # Find all committed notebooks | ||
if [ "$IPYNB_FILES" != "" ] && [ -z $ALLOW_IPYNB ]; then | ||
for f in $IPYNB_FILES | ||
do | ||
DIFF=$(jupyter nbconvert --log-level ERROR --ClearOutputPreprocessor.enabled=True --ClearMetadataPreprocessor.enabled=True --to notebook --stdout $GITDIR/$f | diff $GITDIR/$f - || :) | ||
if [ "$DIFF" != "" ]; then | ||
echo " | ||
The notebook $GITDIR/$f contains outputs. | ||
Remove them all before committing. | ||
***Hint*** use the command: | ||
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --ClearMetadataPreprocessor.enabled=True --to notebook --inplace $GITDIR/$f | ||
To ignore this error, and add a notebook with outputs, use: | ||
export ALLOW_IPYNB=1 | ||
" | ||
exit 1 | ||
fi | ||
done | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters