Skip to content

Commit

Permalink
Merge pull request mandiant#1 from fireeye/ana-hooks
Browse files Browse the repository at this point in the history
Add hooks for running linters and tests
  • Loading branch information
williballenthin authored Jun 22, 2020
2 parents 273ca81 + 96ad823 commit fa9bb94
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ venv.bak/
*.idb
*.i64
!rules/lib

# hooks output
style-checker-output.log
rule-linter-output.log
8 changes: 8 additions & 0 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ Next, use `pip` to install the source code in "editable" mode. This means that P
`$ pip install -e ./local/path/to/src`

You'll find that the `capa.exe` (Windows) or `capa` (Linux) executables in your path now invoke the capa binary from this directory.

### 4. Setup hooks [optional]

If you plan to contribute to capa, you may want to setup the hooks.
Run `scripts/setup-hooks.sh` to set the following hooks up:
- The `post-commit` hook runs the linter after every `git commit`, letting you know if there are code style or rule linter offenses you need to fix.
- The `pre-push` hook runs the linter and the tests and block the `git push` if they do not succeed.
This way you realise if everything is alright without the need of sending a PR.
34 changes: 34 additions & 0 deletions scripts/hooks/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# Use a console with emojis support for a better experience

# Stash uncommited changes
MSG="post-commit-$(date +%s)"
git stash push -kqum $MSG
STASH_LIST=$(git stash list)
if [[ "$STASH_LIST" == *"$MSG"* ]]; then
echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`"
fi

# Run style checker and print state (it doesn't block the commit)
pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1
if [ $? == 0 ]; then
echo 'Style checker succeeds!! 💘'
else
echo 'Style checker failed 😭\nCheck style-checker-output.log for details'
exit 1
fi

# Run rule linter and print state (it doesn't block the commit)
python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1
if [ $? == 0 ]; then
echo 'Rule linter succeeds!! 💖'
else
echo 'Rule linter failed 😭\nCheck rule-linter-output.log for details'
exit 2
fi

# Restore stashed changes
if [[ "$STASH_LIST" == *"$MSG"* ]]; then
git stash pop -q --index
echo "Stashed changes '$MSG' restored"
fi
52 changes: 52 additions & 0 deletions scripts/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# Use a console with emojis support for a better experience

# Stash uncommited changes
MSG="pre-push-$(date +%s)"
git stash push -kqum $MSG
STASH_LIST=$(git stash list)
if [[ "$STASH_LIST" == *"$MSG"* ]]; then
echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`"
fi

restore_stashed() {
if [[ "$STASH_LIST" == *"$MSG"* ]]; then
git stash pop -q --index
echo "Stashed changes '$MSG' restored"
fi
}

# Run style checker and print state
pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1
if [ $? == 0 ]; then
echo 'Style checker succeeds!! 💘'
else
echo 'Style checker failed 😭 PUSH ABORTED\nCheck style-checker-output.log for details'
restore_stashed
exit 1
fi

# Run rule linter and print state
python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1
if [ $? == 0 ]; then
echo 'Rule linter succeeds!! 💖'
else
echo 'Rule linter failed 😭 PUSH ABORTED\nCheck rule-linter-output.log for details'
restore_stashed
exit 2
fi

# Run tests
echo 'Running tests, please wait ⌛'
pytest tests/ --maxfail=1
if [ $? == 0 ]; then
echo 'Tests succeed!! 🎉'
else
echo 'Tests failed 😓 PUSH ABORTED\nRun `pytest -v --cov=capa test/` if you need more details'
restore_stashed
exit 3
fi

echo 'PUSH SUCCEEDED 🎉🎉'

restore_stashed
25 changes: 25 additions & 0 deletions scripts/setup-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

GIT_DIR=`git rev-parse --show-toplevel`
cd $GIT_DIR

# hooks may exist already (e.g. git-lfs configuration)
# If the `.git/hooks/$arg` file doesn't exist it, initialize with `#!/bin/sh`
# After that append `scripts/hooks/$arg` and ensure they can be run
create_hook() {
if [[ ! -e .git/hooks/$1 ]]; then
echo '#!/bin/sh' > .git/hooks/$1
fi
cat scripts/hooks/$1 >> .git/hooks/$1
chmod +x .git/hooks/$1
}

echo '\n#### Copying hooks into .git/hooks'
create_hook 'post-commit'
create_hook 'pre-push'

echo '\n#### Installing linter/test dependencies\n'
pip install pycodestyle
pytest-sugar
pip install https://github.com/williballenthin/vivisect/zipball/master
python setup.py develop

0 comments on commit fa9bb94

Please sign in to comment.