Skip to main content

rifleman

PyPi version Python 3.7|3.8|3.9 PRs Welcome

An extendible dispatcher to lint/format code, based on rifle

This lets you run one command over lots of files/directories which could be in different languages - it classifies them by inspecting the mime type, extension, name or shebang, and then runs a command on those files.

This heavily simplifies and modifies the rifle config file format; including a condition to help check the 'shebang' value for scripts.

See config/format.conf for the default configuration file, I recommend you customize it to include the commands you use.

By default, I've included lots of the formaters/linters I use, an excerpt:

# html
ext x?html?, has prettier = prettier -w "$@"
# web technologies, handled by prettier
ext vue|yaml|json|graphql|tsx?|jsx?|s?css|less|md, has prettier = prettier -w "$@"

# golang
ext go, has go = go fmt "$@"

# python
ext py, has black = black "$@"
mime text/x-script.python, has black = black "$@"
shebang python(2|3)?, has black = black "$@"

# shell script
shebang zsh|bash, has shfmt = shfmt -w "$@"
shebang \/bin\/sh, has shfmt = shfmt -w "$@"
ext sh|(ba|z)sh, has shfmt = shfmt -w "$@"
mime text/x-shellscript, has shfmt = shfmt -w "$@"

Used emacs-format-all-the-code as reference.

Though the example is here is for code, similar to rifle, this could be used to dispatch against any file/mime type. I've included image.conf, which takes any files as inputs, matches the image/png|jpeg|gif mimetypes, and runs optipng/jpegtran/gifsicle to optimize those files to reduce their file size. That could be run like fd -X rifleman -a image, to optimize all images in any subdirectores.

Feel free to PR additional formatters!

Installation

Requires python3.7+

To install with pip, run:

pip install rifleman

Usage

Usage: rifleman [-] [-ljpcah] [files]...

Pass '-' to read filenames from STDIN, separated by newlines

Options:
  -h, --help      show this help message and exit
  -l              list actions for files
  -j              list actions for files as JSON
  -m              list computed mime type for each file
  -p              prompt before running each command
  -c CONFIG_FILE  read config from specified file instead of default
  -a ACTION       name of configuration file in config directory to use
                  (lint|format|image)

This doesn't offer a way to discover/search for files, because so many tools already exist to do that.

With shell globbing:

rifleman *.md ./project/*.py

To run this against all files in a git-tracked directory:

git ls-files | rifleman -

You can find (with the -exec flag), or the friendlier fd, to run against all files in the directory recursively:

fd -X rifleman

The -j and -l flags print what commands which would be used on each file instead of running the command.

The -c and -a files are used to determine which config file to use, completely altering the functionality of this.

By default, it uses the format.conf file in the ${XDG_CONFIG_HOME:-${HOME}/.config}/rifleman directory. -a is a shorthand; specifying -a lint looks for a file in the configuration directory called lint.conf

When this is first run, it will try to download the configuration files into the corresponding directories.

If you're trying to debug which mimetype to use for a particular file, you can use the -m flag, to dump the mimetype rifleman determines.

$ git ls-files | shuf -n 3 | rifleman - -m
pytest.ini:text/plain
rifleman/py.typed:inode/x-empty
rifleman/__main__.py:text/x-python

For reference, that works by checking the known user-defined mimetypes at ~/.mime.types, using the builtin mimetypes python module. If that fails to find a mimetype, it runs the command: file --mime-type -Lb <file>.

Example

$ rifleman *.md $(fd \.py$)
Running: /bin/sh -c set -- 'README.md'; prettier -w "$@"
README.md 88ms
Running: /bin/sh -c set -- 'rifleman/__init__.py' 'rifleman/__main__.py' 'setup.py' 'tests/test_rifleman.py'; black "$@"
All done! ✨ 🍰 ✨
4 files left unchanged.

Tests

git clone 'https://github.com/seanbreckenridge/rifleman'
cd ./rifleman
pip install '.[testing]'
mypy ./rifleman
pytest

Metadata

Release files for rifleman 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rifleman 0.1.9
File Size Uploaded Provenance Actions
rifleman-0.1.9.tar.gz 23.2 kB Without provenance Details for rifleman-0.1.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rifleman 0.1.9
File Interpreter ABI Platform Actions
rifleman-0.1.9-py3-none-any.whl Python 3 none any Details for rifleman-0.1.9-py3-none-any.whl

Total release size: 44.8 kB

Release files / rifleman-0.1.9.tar.gz

Download URL rifleman-0.1.9.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5732c04719469bf7fa6da3599591fcab9443c54d6a666d356dcb64b05883e3fa
BLAKE2b-256 checksum
How to use checksums
effe781804d48faec11511da04b80fa03119a83202892931f0e01b0a0ac762e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / rifleman-0.1.9-py3-none-any.whl

Download URL rifleman-0.1.9-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92bbca696960b810e4d7b108895c9bc0a2e0a318f9cf22df1539ea6877f2255a
BLAKE2b-256 checksum
How to use checksums
55d4aba5c1f5a3bf7e7b5a4d7062445448af178a456c2f177789a3e77fec367f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Security for rifleman 0.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page