Skip to content

Instantly share code, notes, and snippets.

@GeroZayas
Created December 27, 2025 11:51
Show Gist options
  • Select an option

  • Save GeroZayas/c70d47f61bf157a134827f73d1ed0d33 to your computer and use it in GitHub Desktop.

Select an option

Save GeroZayas/c70d47f61bf157a134827f73d1ed0d33 to your computer and use it in GitHub Desktop.
Bug Buster Script - Runs Black, Bandit and Flake8 on all Python files of the given dir
import os
import subprocess
from rich import print
print(
"""
==== πŸ† WELCOME TO BUG BUSTER ====
πŸ§‘β€πŸ’» Necessary modules to be installed: `black`, `bandit` and `flake8`
πŸ‘€ (copied from:
https://medium.com/pythoneers/18-insanely-useful-python-automation-scripts-i-use-everyday-b3aeb7671ce9
---> 14. BugBuster)
πŸ—’οΈ The user can input a path or hit Enter for CWD (current working directory) :)
This script does the following:
1- Gets all the Python files in the given directory
2- For each py file does `black <file> --check`
3- Then, for each does `flake8 <file>`
4- Then, for each does `bandit -r <file>`
πŸ“– IT CREATES a dir "Bug_Buster_Reports" and it puts there the txt files resulting from the analysis.
"""
)
def analyze_code(directory):
# List Python files in the directory
python_files = [file for file in os.listdir(directory) if file.endswith(".py")]
if not python_files:
print("No Python files found in the specified directory.")
return
report_dir = os.path.join(directory, "Bug_Buster_Reports")
os.makedirs(report_dir, exist_ok=True)
for file in python_files:
print(f"Analyzing file: {file}")
file_path = os.path.join(directory, file)
# Run Black (code formatter)
print("\nRunning Black...")
black_command = f"black {file_path} --check"
subprocess.run(black_command, shell=True)
# Run Flake8 (linter)
print("\nRunning Flake8...")
flake8_output_file = os.path.join(report_dir, f"{file}_flake8_report.txt")
with open(flake8_output_file, "w") as flake8_output:
flake8_command = f"flake8 {file_path}"
subprocess.run(
flake8_command,
shell=True,
stdout=flake8_output,
stderr=subprocess.STDOUT,
)
print(f"Flake8 report saved to {flake8_output_file}")
# Run Bandit (security analysis)
print("\nRunning Bandit...")
bandit_output_file = os.path.join(report_dir, f"{file}_bandit_report.txt")
with open(bandit_output_file, "w") as bandit_output:
bandit_command = f"bandit -r {file_path}"
subprocess.run(
bandit_command,
shell=True,
stdout=bandit_output,
stderr=subprocess.STDOUT,
)
print(f"πŸ—’οΈ Bandit report saved to {bandit_output_file}")
print(f"βœ… Analyzing file: {file} Completed!!!!")
print("================" * 5)
print("================" * 5)
if __name__ == "__main__":
user_path_input = input("INSERT PATH (hit Enter for cwd): >>> ")
if user_path_input.strip() == "":
user_path_input = "."
analyze_code(user_path_input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment