#!/usr/bin/python3

import glob
import re
import subprocess
import sys
from pathlib import Path


def is_excluded(f: str) -> bool:
    return re.match(r"^([.]git|config/chroot_local-includes/usr/share/doc/tails/website|submodules|tmp|wiki/src)/",
                    f) is not None or Path(f).is_dir()


def mimetype(f: str) -> str:
    return subprocess.run(['file', '--brief', '--mime-type', f],
                          stdout=subprocess.PIPE,
                          universal_newlines=True,
                          check=True).stdout.rstrip()


def is_python_file(f: str) -> bool:
    return f.endswith('.py') or mimetype(f) == "text/x-script.python"


python_files = [
    f for f in glob.glob("**/*", recursive=True)
    if not is_excluded(f) and is_python_file(f)
]

sys.exit(
    subprocess.run(['bandit'] + sys.argv[1:] + python_files).returncode)
