Skip to content

Commit

Permalink
Find bandit.yaml when in virtualenv
Browse files Browse the repository at this point in the history
When running bandit without the '-c' parameter, it has the inability
to find bandit.yaml within a virtualenv.

This patch detects if running in a virtualenv and prepends that path
to an appropriate location of bandit.yaml (depending on platform).

Change-Id: I6b7faa8f4eefd91c9fff9da47dc1074075ad9494
Closes-Bug: #1484757
  • Loading branch information
ericwb committed Aug 14, 2015
1 parent 3b6acb7 commit 8cecf88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ GNU/Linux:
- ~/.config/bandit/bandit.yaml
- /etc/bandit/bandit.yaml
- /usr/local/etc/bandit/bandit.yaml
- <path to venv>/etc/bandit/bandit.yaml (if running within virtualenv)

Mac OSX:
- ./bandit.yaml
- /Users/${USER}/Library/Application Support/bandit/bandit.yaml
- /Library/Application Support/bandit/bandit.yaml
- /usr/local/etc/bandit/bandit.yaml
- <path to venv>/bandit/config/bandit.yaml (if running within virtualenv)

Exclusions
----------
Expand Down
17 changes: 15 additions & 2 deletions bandit/bandit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import logging
import os
import sys
import sysconfig

import appdirs

from bandit.core import manager as b_manager
from bandit.core import utils

BASE_CONFIG = '/bandit.yaml'
BASE_CONFIG = 'bandit.yaml'


def _init_logger(debug=False, log_format=None):
Expand Down Expand Up @@ -60,13 +61,25 @@ def _init_extensions():
return ext_loader.MANAGER


def _running_under_virtualenv():
if hasattr(sys, 'real_prefix'):
return True
elif sys.prefix != getattr(sys, 'base_prefix', sys.prefix):
return True


def _find_config():
# prefer config file in the following order:
# 1) current directory, 2) user home directory, 3) bundled config
config_dirs = (
['.'] + [appdirs.user_config_dir("bandit")] +
appdirs.site_config_dir("bandit", multipath=True).split(':'))
config_locations = [s + BASE_CONFIG for s in config_dirs]
if _running_under_virtualenv():
config_dirs.append(os.path.join(sys.prefix, 'etc', 'bandit'))
config_dirs.append(
os.path.join(sysconfig.get_paths().get('purelib', ''),
'bandit', 'config'))
config_locations = [os.path.join(s, BASE_CONFIG) for s in config_dirs]

# pip on Mac installs to the following path, but appdirs expects to
# follow Mac's BPFileSystem spec which doesn't include this path so
Expand Down

0 comments on commit 8cecf88

Please sign in to comment.