Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colorama not being disabled after being used #586

Merged
merged 1 commit into from
Mar 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions bandit/formatters/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@
from bandit.core import docs_utils
from bandit.core import test_properties

IS_WIN_PLATFORM = sys.platform.startswith('win32')
COLORAMA = False

# This fixes terminal colors not displaying properly on Windows systems.
# Colorama will intercept any ANSI escape codes and convert them to the
# proper Windows console API calls to change text color.
if sys.platform.startswith('win32'):
if IS_WIN_PLATFORM:
try:
import colorama
except ImportError:
pass
else:
colorama.init()
COLORAMA = True


LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -161,6 +165,9 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
:param lines: Number of lines to report, -1 for all
"""

if IS_WIN_PLATFORM and COLORAMA:
colorama.init()

bits = []
if not manager.quiet or manager.results_count(sev_level, conf_level):
bits.append(header("Run started:%s", datetime.datetime.utcnow()))
Expand All @@ -186,3 +193,6 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
if fileobj.name != sys.stdout.name:
LOG.info("Screen formatter output was not written to file: %s, "
"consider '-f txt'", fileobj.name)

if IS_WIN_PLATFORM and COLORAMA:
colorama.deinit()