Add linting pre-commit hook and hook install script #86

Merged
merenber merged 6 commits from feature-80 into master 2023-05-29 00:14:06 -04:00
Member
No description provided.
j24chung added 1 commit 2022-11-19 18:37:28 -05:00
continuous-integration/drone/pr Build is passing Details
da51c4309c
Add linting pre-commit hook and hook install script
r345liu requested changes 2022-11-26 20:57:24 -05:00
r345liu left a comment
Owner

Might want to consider the case where file name can be white space. Just adding a IFS='\n' before the for loop is probably enough.

Might want to consider the case where file name can be white space. Just adding a `IFS='\n'` before the for loop is probably enough.
merenber requested changes 2022-11-26 23:17:39 -05:00
@ -0,0 +8,4 @@
PASS=true
# Check for flake8
Owner

I suggest using the flake8 script which gets installed in venv/bin, so that the user doesn't have to install a ilbrary globally if they don't want to.

I suggest using the flake8 script which gets installed in venv/bin, so that the user doesn't have to install a ilbrary globally if they don't want to.
Owner

I think we can assume git commit would be running inside virtual env, plus venv directory can be located elsewhere too

I think we can assume git commit would be running inside virtual env, plus venv directory can be located elsewhere too
Author
Member

Do you mind clarifying this flake8 script you're referring to? @merenber

Do you mind clarifying this flake8 script you're referring to? @merenber
Owner

It's in dev-requirements.txt. On my machine, I run flake8 from inside one of the Docker containers, in the activated venv, but I run git commit outside of the container, directly on the host. I cannot activate the venv outside of the container because my hosts's Python version is higher than that used in the Docker containers.

I think it's fair to assume that people will want to use git outside of the container, since their SSH keys and global gitconfig are not present in the container. Instead of executing flake8 directly, I suggest we run it via docker-compose, e.g. docker-compose exec phosphoric-acid flake8.

I also suggest that we disable this hook by default, and require some kind of explicit user action to enable it. Not everyone will want to run a potentially time-consuming lint on every commit. I usually only run flake8 right before I push.

It's in dev-requirements.txt. On my machine, I run `flake8` from inside one of the Docker containers, in the activated venv, but I run `git commit` outside of the container, directly on the host. I cannot activate the venv outside of the container because my hosts's Python version is higher than that used in the Docker containers. I think it's fair to assume that people will want to use git outside of the container, since their SSH keys and global gitconfig are not present in the container. Instead of executing flake8 directly, I suggest we run it via docker-compose, e.g. `docker-compose exec phosphoric-acid flake8`. I also suggest that we disable this hook by default, and require some kind of explicit user action to enable it. Not everyone will want to run a potentially time-consuming lint on every commit. I usually only run flake8 right before I push.
Author
Member

It's in dev-requirements.txt. On my machine, I run flake8 from inside one of the Docker containers, in the activated venv, but I run git commit outside of the container, directly on the host. I cannot activate the venv outside of the container because my hosts's Python version is higher than that used in the Docker containers.

I think it's fair to assume that people will want to use git outside of the container, since their SSH keys and global gitconfig are not present in the container. Instead of executing flake8 directly, I suggest we run it via docker-compose, e.g. docker-compose exec phosphoric-acid flake8.

I agree and I also use Docker for development myself. But since README also has instructions for setting up a VM, would it be necessary for the script to suppor that? Since the script isn't enabled by default, then maybe it's okay to not support it since I believe most people would use the docker setup instead.

I also suggest that we disable this hook by default, and require some kind of explicit user action to enable it. Not everyone will want to run a potentially time-consuming lint on every commit. I usually only run flake8 right before I push.

No problem. The script is currently a git hook script ran before each commit but I could make it a script ran ony before each push. The git hooks also require running the "git-hook-install.sh" script before it's enabled.

> It's in dev-requirements.txt. On my machine, I run `flake8` from inside one of the Docker containers, in the activated venv, but I run `git commit` outside of the container, directly on the host. I cannot activate the venv outside of the container because my hosts's Python version is higher than that used in the Docker containers. > > I think it's fair to assume that people will want to use git outside of the container, since their SSH keys and global gitconfig are not present in the container. Instead of executing flake8 directly, I suggest we run it via docker-compose, e.g. `docker-compose exec phosphoric-acid flake8`. I agree and I also use Docker for development myself. But since README also has instructions for setting up a VM, would it be necessary for the script to suppor that? Since the script isn't enabled by default, then maybe it's okay to not support it since I believe most people would use the docker setup instead. > I also suggest that we disable this hook by default, and require some kind of explicit user action to enable it. Not everyone will want to run a potentially time-consuming lint on every commit. I usually only run flake8 right before I push. No problem. The script is currently a git hook script ran before each commit but I could make it a script ran ony before each push. The git hooks also require running the "git-hook-install.sh" script before it's enabled.
Author
Member

Update: The latest commit runs flake8 through docker but since it's just code linting, it just starts a python docker image and uses flake8 installed in the venv location created when setting up the docker setup. This should be good to close

Update: The latest commit runs flake8 through docker but since it's just code linting, it just starts a python docker image and uses flake8 installed in the venv location created when setting up the docker setup. This should be good to close
j24chung marked this conversation as resolved
@ -0,0 +19,4 @@
for FILE in $STAGED_FILES
do
flake8 "$FILE"
Owner

Is there a reason why we need to invoke flake8 on each file separately? If you just run flake8 once from the root directory, it'll lint all of the source files at once.

Is there a reason why we need to invoke flake8 on each file separately? If you just run `flake8` once from the root directory, it'll lint all of the source files at once.
Author
Member

We are just linting all the files that are being committed. Technically, all files should be free of linting issues but I've just decided to only apply linting on the files that need to be changed.

We are just linting all the files that are being committed. Technically, all files should be free of linting issues but I've just decided to only apply linting on the files that need to be changed.
Owner

What if one of the staged files is not a Python file? flake8 will exit with an error code if you run it directly on a non-Python file.

What if one of the staged files is not a Python file? flake8 will exit with an error code if you run it directly on a non-Python file.
Author
Member

At the end of the STAGED_FILES variable declaration it has grep ".py\{0,1\}$" so it should only grab python files

At the end of the STAGED_FILES variable declaration it has `grep ".py\{0,1\}$"` so it should only grab python files
j24chung marked this conversation as resolved
@ -0,0 +22,4 @@
flake8 "$FILE"
if [[ "$?" == 0 ]]; then
echo -e "\t\033[32mPassed: $FILE\033[0m"
Owner

flake8 provides its own diagnostic messages for each lint error, so I don't think we need to add an extra "Passed" or "Failed" message.

flake8 provides its own diagnostic messages for each lint error, so I don't think we need to add an extra "Passed" or "Failed" message.
Author
Member

Fair. Will address it rn.

Fair. Will address it rn.
j24chung marked this conversation as resolved
@ -0,0 +32,4 @@
echo -e "\nPython linting complete!\n"
if ! $PASS; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
Owner

Maybe we should change "ESLint" to "flake8"?

Maybe we should change "ESLint" to "flake8"?
Author
Member

Haha oops. Need to fix my typo

Haha oops. Need to fix my typo
j24chung marked this conversation as resolved
j24chung changed title from Add linting pre-commit hook and hook install script to WIP: Add linting pre-commit hook and hook install script 2023-02-04 00:07:53 -05:00
j24chung changed title from WIP: Add linting pre-commit hook and hook install script to Add linting pre-commit hook and hook install script 2023-02-04 00:18:09 -05:00
j24chung added 1 commit 2023-02-04 21:44:59 -05:00
continuous-integration/drone/pr Build is failing Details
8109758914
Fix typo in linting check
Author
Member

Might want to consider the case where file name can be white space. Just adding a IFS='\n' before the for loop is probably enough.

Good point. That'll be addressed

> Might want to consider the case where file name can be white space. Just adding a `IFS='\n'` before the for loop is probably enough. Good point. That'll be addressed
j24chung added 1 commit 2023-05-27 16:42:35 -04:00
continuous-integration/drone/pr Build is failing Details
42a74c2bc9
Implement feedback in pre-commit script
merenber requested changes 2023-05-27 21:42:32 -04:00
@ -0,0 +1,12 @@
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".py\{0,1\}$")
Owner

That regex looks wrong to me - that will also match files which end with ".p" as well as "xp" (since the "." is a wildcard). I think maybe you want e.g. grep '\.py$'.

That regex looks wrong to me - that will also match files which end with ".p" as well as "xp" (since the "." is a wildcard). I think maybe you want e.g. `grep '\.py$'`.
Owner

Actually, we can get rid of the STAGED_FILES variable altogether if we just invoke flake8 with no arguments - see comment below.

Actually, we can get rid of the STAGED_FILES variable altogether if we just invoke flake8 with no arguments - see comment below.
j24chung marked this conversation as resolved
@ -0,0 +6,4 @@
exit 0
fi
docker run --rm -v "$PWD:$PWD:z" -w "$PWD" -e "STAGED_FILES=$STAGED_FILES" python:3.7-buster sh -c './lint-docker.sh "$STAGED_FILES"'
Owner

We use the 3.9-bullseye image now.

We use the `3.9-bullseye` image now.
j24chung marked this conversation as resolved
lint-docker.sh Outdated
@ -0,0 +10,4 @@
PASS=true
while IFS= read -r FILE; do
Owner

I think this was mentioned before but in case it wasn't - why are we linting each file individually? flake8 can just be run with no arguments from the root directory, and it will be faster than creating a new process for each file.

I think this was mentioned before but in case it wasn't - why are we linting each file individually? flake8 can just be run with no arguments from the root directory, and it will be faster than creating a new process for each file.
Author
Member

My oriignal thought before was we'd just lint the files that's being modified but rethinking about it, there shouldn't it be a problem with just relinting everything which is easier

My oriignal thought before was we'd just lint the files that's being modified but rethinking about it, there shouldn't it be a problem with just relinting everything which is easier
j24chung marked this conversation as resolved
j24chung added 2 commits 2023-05-28 17:54:45 -04:00
j24chung requested review from merenber 2023-05-28 17:54:56 -04:00
merenber approved these changes 2023-05-28 18:27:07 -04:00
merenber added 1 commit 2023-05-28 18:35:41 -04:00
continuous-integration/drone/pr Build is passing Details
ce3f5978a4
Merge branch 'master' into feature-80
merenber approved these changes 2023-05-28 19:01:43 -04:00
j24chung requested review from r345liu 2023-05-28 22:38:58 -04:00
j24chung scheduled this pull request to auto merge when all checks succeed 2023-05-28 22:46:37 -04:00
j24chung scheduled this pull request to auto merge when all checks succeed 2023-05-28 22:47:00 -04:00
merenber merged commit 968f0815c7 into master 2023-05-29 00:14:06 -04:00
merenber deleted branch feature-80 2023-05-29 00:14:07 -04:00
Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: public/pyceo#86
No description provided.