check-empty
A simple, dependency-free pre-commit / prek hook, CLI, library and GitHub Action conglomerate written in Python.
Makes sure selected files, even within directories, are empty according to as little filesystem stat calls as possible, and clears them effectively with minimal I/O if specified.
Prerequisites
Supports CPython 3.6+, PyPy 7.0+, GraalPy 19.0+ out-of-the-box, and most likely every Python 3.6 runtime you can think of. This is the only requirement to use this tool.
Quickstart
If using double-asterisk globbing in the CLI, make sure it is enabled:
shopt -s globstar
similarly for extglob:
shopt -s extglob
Without installation (just trying out the capabilities):
uvx check-empty -Q src/mylib/py.typed docs/.nojekyll static/.gitkeep some_dir **/*.lock
Installation
# uv
uv tool install check-empty # bare executable on PATH
uv pip install check-empty # if you want to import check_empty for programmatic usage
pip install check-empty # pip
Show the help with:
check-empty --help # or check-empty -?
Usage
All the snippets below are equivalent, assuming globstar is on.
Run the CLI:
check-empty -Q src/mylib/py.typed docs/.nojekyll static/.gitkeep some_dir **/*.lock
In Python:
from check_empty import check
import glob
a = ['src/mylib/py.typed', 'docs/.nojekyll', 'static/.gitkeep', 'some_dir']
a.extend(glob.iglob('**/*.lock', recursive=True))
# build a list of paths to files or directories by manual globbing
check(a, verbosity=1)
# default verbosity is 2; in the command line, each -Q decreases it by 1 and
# each -V increases it by 1
As a pre-commit hook:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/jonathandung/check-empty
rev: v1.1.2 # repository version
hooks:
- id: check-empty # the hook
args: # example list of arguments
- -Q # flag to decrease output, applicable twice (shorthand for --quiet)
files: ^src/mylib/py\.typed|docs/\.nojekyll|static/\.gitkeep|some_dir/.*|.*\.lock$
# paths to files/directories to clear or keep empty as a single regular
# expression (as per the somewhat restrictive pre-commit config schema),
# relative to project root
equivalent in prek.toml format:
[[repos]]
repo = "https://github.com/jonathandung/check-empty"
rev = "v1.1.2"
[[repos.hooks]]
id = "check-empty"
args = ["-Q"]
[[repos.hooks.files]]
glob = [ # globset reference: https://docs.rs/globset/latest/globset/#syntax
# this form is only supported by prek; see
# https://prek.j178.dev/reference/configuration/?h=globs#files
"src/mylib/py.typed",
"docs/.nojekyll",
"static/.gitkeep",
"some_dir/**", # since directories cannot be passed directly, glob the files within
"**/*.lock"
]
or (TOML 1.1+):
# using multiline inline tables
[[repos]]
repo = "https://github.com/jonathandung/check-empty"
rev = "v1.1.2"
hooks = [{
id = "check-empty",
args = ["-Q"],
files = {
glob = [
"src/mylib/py.typed",
"docs/.nojekyll",
"static/.gitkeep",
"some_dir/**",
"**/*.lock"
]
},
}]
As a GitHub Actions workflow step:
steps:
- uses: jonathandung/check-empty@v1.1.2 # the latest version on the GitHub Actions
# marketplace; this step will fail and subsequent jobs will not run if any file is
# not empty
with:
python-version: '3.14' # run the script on the latest stable Python version
# Python down to 3.6 is supported but not recommended due to end-of-life
quiet: true
filenames: |
src/mylib/py.typed
docs/.nojekyll
static/.gitkeep
some_dir
globs: '**/*.lock'
# can also be an array of globs joined into a newline-delimited multiline string,
# as in filenames
Accepted action inputs and descriptions thereof
Notes
- If your file name starts with a hyphen, to avoid having it misinterpreted as a flag,
use a command of the form
check-empty -- -this_is_actually_a_file.txt. - Forward slashes can be used even on Windows, so there is no need to escape anything.
- Glob patterns are supported on *nix only. If on Windows, use a shell like Git Bash.
- To pass an
argfile, use
the
@prefix, and escape files whose names actually start with@using the double-hyphen syntax. - It may be unintuitive that a directory being "empty" means all its files are empty, but this project explicitly targets files, since version control systems track files rather than directories.
- The program does not recurse into archives, since identification of compressed archives would require reading the first few bytes of each file seen, which is error-prone and inefficient.
Development
If you wish to contribute to this project, you are more than welcome, but please remember to read the contributing guide. Tests are run with:
python -m test_check_empty # explicit
python -m unittest discover # alternative
at the project root.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file check_empty-1.1.2.tar.gz.
File metadata
- Download URL: check_empty-1.1.2.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e06b1a89af9bf7bfafa289f9e5f76d4ab3f4257cbea0650aa3d32d2879248616
|
|
| MD5 |
6956d85e9b827fa21f351a9a68570934
|
|
| BLAKE2b-256 |
c108e457c9faf5f30b32fa9714b26399aa59ee68d663c8e1e737dd6e9192fbd4
|
File details
Details for the file check_empty-1.1.2-py3-none-any.whl.
File metadata
- Download URL: check_empty-1.1.2-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.6 {"installer":{"name":"uv","version":"0.12.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86ff5d11245d7cf938bdc6281e4beaa6f690e11e2e152a0aec24caf2db64aa4a
|
|
| MD5 |
e89fa92abb5e613d44aed6d37662aa0c
|
|
| BLAKE2b-256 |
3a65845feca7eec9b31106d72fdea1a80a993c20d15c0668248d83428af64f37
|