Skip to main content

Run pre-commit code checkers

Project description

This app does any number of checks such as unittests or pylint before git commit. If at least one check will not pass, commit is aborted.

https://cloud.githubusercontent.com/assets/898669/10948860/0dcede00-8330-11e5-8b14-5490c4a00d57.png https://cloud.githubusercontent.com/assets/898669/10948864/16ba38b6-8330-11e5-85b8-02bb0332105b.png

To use code-checker in your project execute command setup-githook. This command creates git pre-commit hook (.git/hooks/pre-commit) and precommit_checks.py listed below.

import os
import sys
from codechecker.checker import PylintChecker
from codechecker.checker import ExitCodeChecker
from codechecker import job_processor
from codechecker import helper

ACCEPTED_PYLINT_RATE = 9

# Execute checks only on files added to git staging area
file_list = helper.get_staged_files()

py_files = [f for f in file_list if f.endswith('.py')]
# Exclude test cases
py_files = [f for f in py_files if not os.path.basename(f).startswith('test_')]

# Add checkers
checkers = []
checkers.append(ExitCodeChecker('python3 -m unittest discover .',
                                'python unittest'))
for file_name in py_files:
    checkers.append(PylintChecker(file_name, ACCEPTED_PYLINT_RATE))
    checkers.append(ExitCodeChecker('pep8 {}'.format(file_name),
                                    'PEP8: {}'.format(file_name)))

sys.exit(job_processor.process_jobs(checkers))

Above script executes unit tests for project and pylint along with pep8 for every .py file.

precommit_checks.py are separated from .git/hooks/pre-commit so precommit_checks.py is under git version control.

Checks are treated as jobs divided among couple of workers. Number of workers is equal to number of your cpu logical cores, every worker is executed in separate process.

See Currently supported checkers

Installation

pip install code-checker

Uninstallation

pip uninstall code-checker

Git hooks setup

  1. Change current working directory to git repository cd /path/to/repository

2. Execute setup-githooks. This command creates pre-commit hook which run precommit_checker.py before commit

Customization

To customize pre-commit checking edit precommit_checker.py.

Currently supported checkers

ExitCodeChecker:

Description:

Run system shell command and fail if exit code is non 0

Usage: Create ExitCodeChecker object with arguments:

  1. command to execute (string)

  2. task name displayed before result in console

# ...
from checker import ExitCodeChecker
# ...
jobs = []
# ...
jobs.append(ExitCodeChecker('python3 -m unittest discover .',
                            'python unittest'))
Example result:

* python unittest: OK

pylint:

Description:

Check passes if pylint code rate for particular file is greather or equal to accepted code rate. Accepted code rate is

Requirements:

pylint

Usage:

# ...
from checker import PylintChecker
# ...
ACCEPTED_PYLINT_RATE = 9
jobs = []
# ...
jobs.append(PylintChecker(file_name, ACCEPTED_PYLINT_RATE))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code-checker-0.1.0.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file code-checker-0.1.0.tar.gz.

File metadata

  • Download URL: code-checker-0.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for code-checker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f9b6330645fb7cf2a38958c5ae3cd7d41f2d7f9005f77422c86a676e58456681
MD5 d87a1997159a4a0b05b86025432fd368
BLAKE2b-256 416c67e30c1d363c7e02a4f1a61ce99a2f14015d0dc754d6f50e7e04d23f3702

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page