From bcc191dc2d5a33ae4a754e6cb829cd6d294080af Mon Sep 17 00:00:00 2001 From: May Blonder Date: Tue, 24 May 2022 12:16:56 +0300 Subject: [PATCH 1/2] Adding option to not print line's number: --no-line-numbers --- bandit/cli/main.py | 19 +++++++++++++++++++ bandit/core/issue.py | 13 +++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bandit/cli/main.py b/bandit/cli/main.py index 47588859d..368d28864 100644 --- a/bandit/cli/main.py +++ b/bandit/cli/main.py @@ -367,6 +367,15 @@ def main(): default=False, help="exit with 0, " "even with results found", ) + parser.add_argument( + "-no", + "--no-line-numbers", + dest="no_line_numbers", + action="store", + default=False, + type=str, + help="flag for not showing code line's", + ) python_ver = sys.version.replace("\n", "") parser.add_argument( "--version", @@ -451,6 +460,9 @@ def main(): args.confidence = 4 # Other strings will be blocked by argparse + if args.no_line_numbers is not None: + os.environ["BANDIT_NO_LINES"] = args.no_line_numbers + try: b_conf = b_config.BanditConfig(config_file=args.config_file) except utils.ConfigError as e: @@ -593,6 +605,13 @@ def main(): "path of a baseline report", ) + args.no_line_numbers = _log_option_source( + parser.get_default("no_line_numbers"), + args.baseline, + ini_options.get("no-line-numbers"), + "do not print code's lines.", + ) + if not args.targets: parser.print_usage() sys.exit(2) diff --git a/bandit/core/issue.py b/bandit/core/issue.py index 875e5e418..210dd8e2c 100644 --- a/bandit/core/issue.py +++ b/bandit/core/issue.py @@ -3,6 +3,7 @@ # # SPDX-License-Identifier: Apache-2.0 import linecache +import os from bandit.core import constants @@ -181,7 +182,11 @@ def get_code(self, max_lines=3, tabbed=False): for line_num in range(1, lmin): self.fdata.readline() - tmplt = "%i\t%s" if tabbed else "%i %s" + no_lines = os.getenv("BANDIT_NO_LINES") + if no_lines == "True" or no_lines == "true" or no_lines == "TRUE": + tmplt = "\t%s" if tabbed else " %s" + else: + tmplt = "%i\t%s" if tabbed else "%i %s" for line in range(lmin, lmax): if self.fname == "": text = self.fdata.readline() @@ -193,7 +198,11 @@ def get_code(self, max_lines=3, tabbed=False): if not len(text): break - lines.append(tmplt % (line, text)) + if no_lines == "True" or no_lines == "true" or no_lines == "TRUE": + lines.append(tmplt % (text)) + else: + lines.append(tmplt % (line, text)) + return "".join(lines) def as_dict(self, with_code=True, max_lines=3): From ad3f998eb3262f999b07e52ca48d21b37477049c Mon Sep 17 00:00:00 2001 From: mayblo <105641951+mayblo@users.noreply.github.com> Date: Tue, 24 May 2022 14:22:49 +0300 Subject: [PATCH 2/2] Update main.py little change --- bandit/cli/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bandit/cli/main.py b/bandit/cli/main.py index 368d28864..2e5273c23 100644 --- a/bandit/cli/main.py +++ b/bandit/cli/main.py @@ -461,7 +461,7 @@ def main(): # Other strings will be blocked by argparse if args.no_line_numbers is not None: - os.environ["BANDIT_NO_LINES"] = args.no_line_numbers + os.environ["BANDIT_NO_LINES"] = str(args.no_line_numbers) try: b_conf = b_config.BanditConfig(config_file=args.config_file)