-
Notifications
You must be signed in to change notification settings - Fork 533
Refactor language coverage module #1240
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
Open
kunalsz
wants to merge
3
commits into
mandiant:master
Choose a base branch
from
kunalsz:refactor-language-coverage
base: master
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.
+54
−46
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import logging | ||
| import argparse | ||
|
|
||
| import pefile | ||
|
|
||
|
|
||
| def add_common_args(parser: argparse.ArgumentParser, default_min_length: int): | ||
| parser.add_argument("path", help="file or path to analyze") | ||
| parser.add_argument( | ||
| "-n", | ||
| "--minimum-length", | ||
| dest="min_length", | ||
| type=int, | ||
| default=default_min_length, | ||
| help="minimum string length", | ||
| ) | ||
|
|
||
| logging_group = parser.add_argument_group("logging arguments") | ||
| logging_group.add_argument("-d", "--debug", action="store_true", help="enable debugging output on STDERR") | ||
| logging_group.add_argument( | ||
| "-q", | ||
| "--quiet", | ||
| action="store_true", | ||
| help="disable all status output except fatal errors", | ||
| ) | ||
|
|
||
|
|
||
| def configure_logging(debug): | ||
| level = logging.DEBUG if debug else logging.INFO | ||
| logging.basicConfig(level=level) | ||
| logging.getLogger().setLevel(level) | ||
|
|
||
|
|
||
| def open_pe_or_none(path: str, logger: logging.Logger): | ||
| try: | ||
| return pefile.PE(path) | ||
| except pefile.PEFormatError as err: | ||
| logger.debug(f"NOT a valid PE file: {err}") | ||
| return None | ||
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| from floss.utils import get_static_strings | ||
| from floss.results import StaticString, StringEncoding | ||
| from floss.language.utils import get_extract_stats | ||
| from floss.language.cli_common import add_common_args, open_pe_or_none, configure_logging | ||
| from floss.language.go.extract import extract_go_strings | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -32,36 +33,13 @@ | |
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Get Go strings") | ||
| parser.add_argument("path", help="file or path to analyze") | ||
| parser.add_argument( | ||
| "-n", | ||
| "--minimum-length", | ||
| dest="min_length", | ||
| type=int, | ||
| default=MIN_STR_LEN, | ||
| help="minimum string length", | ||
| ) | ||
| logging_group = parser.add_argument_group("logging arguments") | ||
| logging_group.add_argument("-d", "--debug", action="store_true", help="enable debugging output on STDERR") | ||
| logging_group.add_argument( | ||
| "-q", | ||
| "--quiet", | ||
| action="store_true", | ||
| help="disable all status output except fatal errors", | ||
| ) | ||
| add_common_args(parser, default_min_length=MIN_STR_LEN) | ||
| args = parser.parse_args() | ||
|
|
||
| if args.debug: | ||
| logging.basicConfig(level=logging.DEBUG) | ||
| logging.getLogger().setLevel(logging.DEBUG) | ||
| else: | ||
| logging.basicConfig(level=logging.INFO) | ||
| logging.getLogger().setLevel(logging.INFO) | ||
| configure_logging(debug=args.debug) | ||
|
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. |
||
|
|
||
| try: | ||
| pe = pefile.PE(args.path) | ||
| except pefile.PEFormatError as err: | ||
| logger.debug(f"NOT a valid PE file: {err}") | ||
| pe = open_pe_or_none(args.path, logger) | ||
| if pe is None: | ||
| return 1 | ||
|
|
||
| path = pathlib.Path(args.path) | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
|
|
||
| from floss.strings import extract_ascii_unicode_strings | ||
| from floss.language.utils import get_extract_stats | ||
| from floss.language.cli_common import add_common_args, open_pe_or_none, configure_logging | ||
| from floss.language.rust.extract import extract_rust_strings | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -32,36 +33,13 @@ | |
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Get Rust strings") | ||
| parser.add_argument("path", help="file or path to analyze") | ||
| parser.add_argument( | ||
| "-n", | ||
| "--minimum-length", | ||
| dest="min_length", | ||
| type=int, | ||
| default=MIN_STR_LEN, | ||
| help="minimum string length", | ||
| ) | ||
| logging_group = parser.add_argument_group("logging arguments") | ||
| logging_group.add_argument("-d", "--debug", action="store_true", help="enable debugging output on STDERR") | ||
| logging_group.add_argument( | ||
| "-q", | ||
| "--quiet", | ||
| action="store_true", | ||
| help="disable all status output except fatal errors", | ||
| ) | ||
| add_common_args(parser, default_min_length=MIN_STR_LEN) | ||
| args = parser.parse_args() | ||
|
|
||
| if args.debug: | ||
| logging.basicConfig(level=logging.DEBUG) | ||
| logging.getLogger().setLevel(logging.DEBUG) | ||
| else: | ||
| logging.basicConfig(level=logging.INFO) | ||
| logging.getLogger().setLevel(logging.INFO) | ||
|
|
||
| try: | ||
| pe = pefile.PE(args.path) | ||
| except pefile.PEFormatError as err: | ||
| logger.debug(f"NOT a valid PE file: {err}") | ||
| configure_logging(debug=args.debug) | ||
|
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. |
||
|
|
||
| pe = open_pe_or_none(args.path, logger) | ||
| if pe is None: | ||
| return 1 | ||
|
|
||
| path = pathlib.Path(args.path) | ||
|
|
||
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.
The
configure_loggingfunction doesn't handle the--quietargument. It should set the logging level toCRITICALwhenquietis true to suppress all but fatal errors. Additionally, the call tologging.getLogger().setLevel(level)is redundant becauselogging.basicConfig()already configures the root logger's level. I've updated the function to handle this, removed the redundant call, and added type hints for clarity.