-
Notifications
You must be signed in to change notification settings - Fork 19
Dev/add bandit linter support #204
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
Draft
mshafer-NI
wants to merge
12
commits into
ni:main
Choose a base branch
from
mshafer-NI:dev/add_bandit_linter_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8a93899
add bandit as a supported tool
mshafer-NI 108ef66
get it to run
mshafer-NI e267872
add running bandit linter to PR build
mshafer-NI 90f8256
format
mshafer-NI 5d0cd8e
add tests that config gets loaded / merged in
mshafer-NI 2e97f07
rename to accommodate another test
mshafer-NI 65c2fb4
add test that original config comes in if not overridden
mshafer-NI b315ce8
add doc-string
mshafer-NI 628c0c2
now that it's working, let's cleanup our configs
mshafer-NI 5e01cd9
fix the comnent
mshafer-NI b77806f
use the multi-access temp file since the tests need to read the file
mshafer-NI e75f532
no longer using tempfile directly
mshafer-NI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,21 @@ | ||
| """Linting methods.""" | ||
|
|
||
| import contextlib | ||
| import copy | ||
| import io | ||
| import logging | ||
| import pathlib | ||
| import sys | ||
| import typing | ||
|
|
||
| import bandit.cli.main | ||
| import flake8.main.application | ||
| import toml | ||
|
|
||
| from ni_python_styleguide import _config_constants, _Flake8Error, _utils | ||
|
|
||
| from ni_python_styleguide import _config_constants | ||
| from ni_python_styleguide import _Flake8Error | ||
| _logger = logging.getLogger(__name__) | ||
| _logger.addHandler(logging.NullHandler()) | ||
|
|
||
|
|
||
| def lint(qs_or_vs, exclude, app_import_names, format, extend_ignore, file_or_dir): | ||
|
|
@@ -41,3 +51,68 @@ def get_lint_output(qs_or_vs, exclude, app_import_names, format, extend_ignore, | |
| pass | ||
| capture.seek(0) | ||
| return capture.read() | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _temp_sys_argv(args): | ||
| old = sys.argv | ||
| sys.argv = list(args) | ||
| try: | ||
| yield | ||
| finally: | ||
| sys.argv = old | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _temp_merged_config(base_config: dict, override_config_file: pathlib.Path): | ||
| with _utils.temp_file.multi_access_tempfile(suffix=".toml") as temp: | ||
| target = copy.deepcopy(base_config) | ||
| merged = {"tool": {"bandit": target}} | ||
| override_config = toml.load(override_config_file)["tool"]["bandit"] | ||
| for key, value in override_config.items(): | ||
| if key in target and isinstance(value, list): | ||
| _logger.debug("Merging %s: %s", key, value) | ||
| target[key].extend(value) | ||
|
Comment on lines
+73
to
+75
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, this allows projects to extend exclude_dirs. |
||
| elif key in target and isinstance(value, dict): | ||
| _logger.debug("Merging %s: %s", key, value) | ||
| target[key].update(value) | ||
| else: | ||
| _logger.debug("Overriding %s: %s", key, value) | ||
| target[key] = value | ||
| _logger.debug("Merged config: %s", merged) | ||
|
|
||
| with temp.open("w") as fout: | ||
| toml.dump(merged, fout) | ||
|
|
||
| yield temp | ||
|
|
||
|
|
||
| def _relative_to_cwd(path: str) -> pathlib.Path: | ||
| try: | ||
| return (pathlib.Path(path)).relative_to(pathlib.Path.cwd()) | ||
| except ValueError: | ||
| return path | ||
|
|
||
|
|
||
| def lint_bandit(qs_or_vs, file_or_dir: typing.Tuple[pathlib.Path], pyproject_config: dict): | ||
| """Run the bandit linter.""" | ||
| with _temp_merged_config( | ||
| pyproject_config, _config_constants.BANDIT_CONFIG_FILE | ||
| ) as merged_config: | ||
| args_list = list( | ||
| filter( | ||
| None, | ||
| [ | ||
| "bandit", | ||
| qs_or_vs, | ||
| "-c", | ||
| str(merged_config), | ||
| "-r", | ||
| *[str(_relative_to_cwd(p)) for p in file_or_dir], | ||
| ], | ||
| ) | ||
| ) | ||
| _logger.debug("Running bandit with args: %s", args_list) | ||
| with _temp_sys_argv(args_list): | ||
| # return | ||
| bandit.cli.main.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, there is an optional
targetskey in the config file.I think defaulting to
.and supporting excludes is probably enough for most projects, though.