A collection of code maintenance tools for Python projects, intended to be used within git hooks.
Project description
🪝 Gadzooks 🪝
A collection of code maintenance tools for Python projects. These are especially useful as pre-commit hooks.
Table of Contents
Installation
pip install py-gadzooks
Requires Python 3.9 or higher.
Usage
Once installed, gadzooks
will be available as a command-line executable.
Different tools can be called as subcommands, which are as follows:
Subcommand | Description |
---|---|
build-docs |
Build project documentation |
check-format |
Check code formatting |
check-version |
Check version consistency |
loc-summarize |
Summarize lines of code |
To view the help and options for a particular subcommand, do:
gadzooks <SUBCOMMAND> --help
loc-summarize
Computes various lines-of-code metrics such as LOC (lines of code), LLOC (logical lines of code), and SLOC (source lines of code). Uses the radon
tool to do this.
The input argument is a path or list of paths to look for Python source files.
Example:
gadzooks loc-summarize .
Output:
Checked 4 source file(s)
LINE STATS
----------
LOC: 244
LLOC: 198
SLOC: 182
Comments: 6
Single comments: 15
Multi: 10
Blank: 37
check-version
Versions may appear in multiple locations within a project, including:
- A source file such as
__init__.py
or__version__.py
- Git tag
- Locally built wheels
- Can be useful for ensuring you have remembered to build the latest version and publish it to PyPI
- An entry in a CHANGELOG file
This subcommand checks for consistency across these locations.
For now, a valid version string is required to be a sequence of three integers separated by .
, for example, 3.7.1
. In the future, the format of version strings will become more flexible.
A valid Git tag may be a version string, optionally prefixed with v
, for example, v3.7.1
.
Option | Description | Default |
---|---|---|
--pkg-name PKG_NAME |
Name of package | Current directory |
--dist-name DIST_NAME |
Name of PyPI distribution | <PKG_NAME> |
--version-path VERSION_PATH |
Path to file where package version is defined | <PKG_NAME>/__init__.py |
--check-tag |
Check that latest tag is valid | |
--check-dist |
Check version of latest built wheel | |
--dist-dir DIST_DIR |
Directory where wheels are built | dist |
--changelog CHANGELOG |
Changelog file | |
--changelog-version-regex |
Pattern to match to find version in changelog file ({version} within the pattern marks the target version) |
{version} |
check-format
Runs an installed code formatter on source files. Currently supported formatters (for Python code only) are:
Example usage:
gadzooks check-format . --formatter black --ignore-patterns "\s*" -- --line-length 120 --skip-string-normalization
If --
is present, the arguments that come before it are gadzooks
' arguments; those that come after it are passed to the formatter program.
check-format
does not (by default) edit the code in place, but rather prints out a stream of diffs indicating what formatting changes would be made. The user may choose to apply the changes or not.
The program exits with return code 1 if there are any changes, and 0 otherwise.
Option | Description | Default |
---|---|---|
(positional) | Files or directories to check | (required) |
--formatter |
Formatter program (black , ruff , or yapf ) |
black |
--ignore-patterns |
One or more regular expressions that will be ignored in the diffs (e.g. "\s*" ignores changes where whitespace lines are added or removed) |
build-docs
Runs a command to build docs within your project.
A typical pattern is to build documentation from template files such as Markdown, then save them out as HTML. Often this is done external to source control via some CI process which builds and deploys docs to a website. However, in some cases you may want to commit the built documentation to your Git repo. This is an easy step to forget, so gadzooks
let you make the action into a pre-commit
hook.
Example usage:
gadzooks build-docs --src-docs docs -- make docs
The --src-docs
option lets you specify which files are the source docs to be built. gadzooks
will first check if any of these files has changed since the last time the docs were built (by means of a saved checksum file). If none has changed, it will do nothing. Otherwise, it will call the command following the final --
(in the example above, make docs
).
Option | Description | Default |
---|---|---|
--src-docs SRC_DOCS |
Files or directories containing source docs | |
--checksum-file CHECKSUM_FILE |
File where SHA-1 checksum is stored | .doc-checksum |
Pre-commit hooks
You can set up individual gadzooks
subcommands to run as pre-commit hooks by configuring your .pre-commit-config.yaml
file to point to this Github repo, specifying your desired subcommand by id
, along with the command-line arguments.
You should also set pass_filenames
to false
to avoid passing filenames as command-line arguments by default, and verbose
to true
so that gadzooks
output will be visible.
Here is an example of a section of a .pre-commit-config.yaml
file:
- repo: https://github.com/jeremander/gadzooks
rev: v0.2.0
hooks:
- id: loc-summarize
args: ['.']
pass_filenames: false
verbose: true
# before pushing, require the git tag match the package version
- id: check-version
args: ['--check-tag']
pass_filenames: false
verbose: true
stages: [push]
License
gadzooks
is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py_gadzooks-0.2.9.tar.gz
.
File metadata
- Download URL: py_gadzooks-0.2.9.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7b18e22791351a247f5a3058ae2615db63e55a90f3d13ce9fe77ca9ace76eba |
|
MD5 | eba993bb0dcd0e1efe58822c92eb557c |
|
BLAKE2b-256 | 326def8c89bfef0f73cbac653348ece6627557ce0e596e9927f99de8a24d253e |
File details
Details for the file py_gadzooks-0.2.9-py3-none-any.whl
.
File metadata
- Download URL: py_gadzooks-0.2.9-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa666c875b0463045b090f0b1373ac75856aadcbaa5188741283d5fa7d4a2ff7 |
|
MD5 | 963be4bc82d257f921d59b02c7bca614 |
|
BLAKE2b-256 | ca51702797606f8c3d771a0c11da4794787c4741cc1b7f323612b01810187c44 |