Skip to main content

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/outfile it will use that file's parent directory). If neither is available, the formatter raises an error asking you to set BANDIT_MULTI_OUTPUT_DIR.

Notes:

  • The special format name multi cannot be used inside BANDIT_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 -o file must exist beforehand — Bandit will not create parent directories for the output file.
  • Using -o can be confusing with the multi formatter because Bandit itself will open (and typically create/truncate) the -o file before formatters run. That means an empty file may be created at the -o path. In some cases this results in an extra empty file unless the -o filename 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_FORMATS is a valid Bandit formatter available in your environment. The multi formatter will skip formats it cannot load and log errors.

Implementation notes

  • See src/bandit_multi_format/__init__.py for details: the formatter loads Bandit's registered formatters and calls each one, creating bandit_output.<fmt> files in the chosen directory.

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

bandit_multi_format-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bandit_multi_format-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

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

Hashes for bandit_multi_format-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7788908fc071ea965ccc327a5f746999afab19cb915b11550f7dee72c5decd81
MD5 3e2c58bde03b012aade1eee6be22dc87
BLAKE2b-256 6ac9614d17570cfdb0c8a474f5bab8b2257c6676771ed5d944a40461825e79c9

See more details on using hashes here.

File details

Details for the file bandit_multi_format-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bandit_multi_format-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 936613de0a82f60921a2e75f29d0014d8e987321a8afa8dcbe691833f406d30f
MD5 0fdaf6996f20452b5ef899212a10ca0b
BLAKE2b-256 987ab9386642908c02d6ee46652ffc33e58348c1a9a3e9dff02013b3202a8ac4

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