Bandit formatter that writes the same report in multiple formats (json, txt, xml).
Project description
bandit-multi-format
Provides a multi output formatter for Bandit that writes the same Bandit report in multiple formats (e.g. json, txt, xml). This is a small solution for https://github.com/PyCQA/bandit/issues/447.
Purpose
When you want Bandit to produce multiple output formats from a single run, the multi formatter will call the configured Bandit formatters and write one output file per format.
Install
Install from PyPI in your Bandit environment:
pip install bandit-multi-format
In case of using pipx to manage Bandit installation, you can inject the package into the existing Bandit pipx environment:
pipx inject bandit bandit-multi-format
Usage
The multi formatter requires one environment variable to be set:
BANDIT_MULTI_FORMATS(required): comma-separated list of Bandit formatter names to invoke (for example:json,txt).
Optional environment variable:
BANDIT_MULTI_OUTPUT_DIR(optional): path to a directory where all outputs will be written. If not set, the formatter attempts to determine an output directory from Bandit's output file object (for example, when Bandit is run with-o /path/to/outfileit will use that file's parent directory). If neither is available, the formatter raises an error asking you to setBANDIT_MULTI_OUTPUT_DIR.
Notes:
- The special format name
multicannot be used insideBANDIT_MULTI_FORMATS(the package blocks it to avoid recursion). - Output files are written as
bandit_output.<format>(for example:bandit_output.json,bandit_output.txt) in the chosen output directory.
Example usage (write JSON and TXT outputs to ./bandit_outputs):
export BANDIT_MULTI_FORMATS="json,txt"
export BANDIT_MULTI_OUTPUT_DIR="./bandit_outputs"
bandit -r path/to/project -f multi
If you prefer to let the formatter infer the output directory from Bandit's -o option, provide an output file when running Bandit:
export BANDIT_MULTI_FORMATS="json,txt"
bandit -r path/to/project -f multi -o ./reports/bandit_report.out
# This will create ./reports/bandit_output.json and ./reports/bandit_output.txt
Warning about using -o:
- The directory for the
-ofile must exist beforehand — Bandit will not create parent directories for the output file. - Using
-ocan be confusing with themultiformatter because Bandit itself will open (and typically create/truncate) the-ofile before formatters run. That means an empty file may be created at the-opath. In some cases this results in an extra empty file unless the-ofilename matches what a formatter would write itself.
In practice: if you want clean outputs in a specific folder, it's often simpler to set BANDIT_MULTI_OUTPUT_DIR to the desired directory instead of relying on -o.
If you run Bandit without -o and without setting BANDIT_MULTI_OUTPUT_DIR, the multi formatter will raise an error and ask you to set BANDIT_MULTI_OUTPUT_DIR.
Examples and troubleshooting
- Make sure each format listed in
BANDIT_MULTI_FORMATSis a valid Bandit formatter available in your environment. Themultiformatter will skip formats it cannot load and log errors.
Implementation notes
- See
src/bandit_multi_format/__init__.pyfor details: the formatter loads Bandit's registered formatters and calls each one, creatingbandit_output.<fmt>files in the chosen directory.
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
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 bandit_multi_format-0.1.1.tar.gz.
File metadata
- Download URL: bandit_multi_format-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7788908fc071ea965ccc327a5f746999afab19cb915b11550f7dee72c5decd81
|
|
| MD5 |
3e2c58bde03b012aade1eee6be22dc87
|
|
| BLAKE2b-256 |
6ac9614d17570cfdb0c8a474f5bab8b2257c6676771ed5d944a40461825e79c9
|
File details
Details for the file bandit_multi_format-0.1.1-py3-none-any.whl.
File metadata
- Download URL: bandit_multi_format-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
936613de0a82f60921a2e75f29d0014d8e987321a8afa8dcbe691833f406d30f
|
|
| MD5 |
0fdaf6996f20452b5ef899212a10ca0b
|
|
| BLAKE2b-256 |
987ab9386642908c02d6ee46652ffc33e58348c1a9a3e9dff02013b3202a8ac4
|