From e479a7fa41c0f288fbc7fda8eeae6321d80c2701 Mon Sep 17 00:00:00 2001 From: kunalsz Date: Wed, 18 Mar 2026 18:19:11 +0530 Subject: [PATCH 1/3] Refactor language coverage module Signed-off-by: kunalsz --- floss/language/cli_common.py | 39 +++++++++++++++++++++++++++++++++ floss/language/go/coverage.py | 32 +++++---------------------- floss/language/rust/coverage.py | 34 +++++----------------------- 3 files changed, 50 insertions(+), 55 deletions(-) create mode 100644 floss/language/cli_common.py diff --git a/floss/language/cli_common.py b/floss/language/cli_common.py new file mode 100644 index 000000000..8d1bda5c7 --- /dev/null +++ b/floss/language/cli_common.py @@ -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 diff --git a/floss/language/go/coverage.py b/floss/language/go/coverage.py index 5081dfc59..1115de6d9 100644 --- a/floss/language/go/coverage.py +++ b/floss/language/go/coverage.py @@ -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) - 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) diff --git a/floss/language/rust/coverage.py b/floss/language/rust/coverage.py index 0596a1ec1..6b67b1795 100644 --- a/floss/language/rust/coverage.py +++ b/floss/language/rust/coverage.py @@ -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) + + pe = open_pe_or_none(args.path, logger) + if pe is None: return 1 path = pathlib.Path(args.path) From f35a183947a5843ca26524ab49b3c60272e544fb Mon Sep 17 00:00:00 2001 From: kunalsz Date: Thu, 9 Apr 2026 17:51:23 +0530 Subject: [PATCH 2/3] improvements Signed-off-by: kunalsz --- floss/language/cli_common.py | 29 ++++++++++++++++++++++++----- floss/language/go/coverage.py | 8 +++----- floss/language/rust/coverage.py | 6 +++--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/floss/language/cli_common.py b/floss/language/cli_common.py index 8d1bda5c7..488c53bb3 100644 --- a/floss/language/cli_common.py +++ b/floss/language/cli_common.py @@ -1,8 +1,24 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import logging import argparse import pefile +logger = logging.getLogger(__name__) + def add_common_args(parser: argparse.ArgumentParser, default_min_length: int): parser.add_argument("path", help="file or path to analyze") @@ -25,13 +41,16 @@ def add_common_args(parser: argparse.ArgumentParser, default_min_length: int): ) -def configure_logging(debug): - level = logging.DEBUG if debug else logging.INFO - logging.basicConfig(level=level) - logging.getLogger().setLevel(level) +def configure_logging(args: argparse.Namespace): + if args.debug: + logging.basicConfig(level=logging.DEBUG) + logging.getLogger().setLevel(logging.DEBUG) + else: + logging.basicConfig(level=logging.INFO) + logging.getLogger().setLevel(logging.INFO) -def open_pe_or_none(path: str, logger: logging.Logger): +def open_pe_or_none(path: str): try: return pefile.PE(path) except pefile.PEFormatError as err: diff --git a/floss/language/go/coverage.py b/floss/language/go/coverage.py index 1115de6d9..ec8a9b090 100644 --- a/floss/language/go/coverage.py +++ b/floss/language/go/coverage.py @@ -18,8 +18,6 @@ import argparse from typing import List -import pefile - from floss.utils import get_static_strings from floss.results import StaticString, StringEncoding from floss.language.utils import get_extract_stats @@ -33,12 +31,12 @@ def main(): parser = argparse.ArgumentParser(description="Get Go strings") - add_common_args(parser, default_min_length=MIN_STR_LEN) + add_common_args(parser, MIN_STR_LEN) args = parser.parse_args() - configure_logging(debug=args.debug) + configure_logging(args) - pe = open_pe_or_none(args.path, logger) + pe = open_pe_or_none(args.path) if pe is None: return 1 diff --git a/floss/language/rust/coverage.py b/floss/language/rust/coverage.py index 6b67b1795..cef346a91 100644 --- a/floss/language/rust/coverage.py +++ b/floss/language/rust/coverage.py @@ -33,12 +33,12 @@ def main(): parser = argparse.ArgumentParser(description="Get Rust strings") - add_common_args(parser, default_min_length=MIN_STR_LEN) + add_common_args(parser, MIN_STR_LEN) args = parser.parse_args() - configure_logging(debug=args.debug) + configure_logging(args) - pe = open_pe_or_none(args.path, logger) + pe = open_pe_or_none(args.path) if pe is None: return 1 From 6e6c71e09ee05ecb3b04f8c0d3b8af3c27d0d20d Mon Sep 17 00:00:00 2001 From: kunalsz Date: Fri, 10 Apr 2026 03:47:00 +0530 Subject: [PATCH 3/3] Remove open_pe_or_none Signed-off-by: kunalsz --- floss/language/cli_common.py | 12 +----------- floss/language/go/coverage.py | 10 +++++++--- floss/language/rust/coverage.py | 8 +++++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/floss/language/cli_common.py b/floss/language/cli_common.py index 488c53bb3..7dbcb5d94 100644 --- a/floss/language/cli_common.py +++ b/floss/language/cli_common.py @@ -1,4 +1,4 @@ -# Copyright 2023 Google LLC +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,8 +15,6 @@ import logging import argparse -import pefile - logger = logging.getLogger(__name__) @@ -48,11 +46,3 @@ def configure_logging(args: argparse.Namespace): else: logging.basicConfig(level=logging.INFO) logging.getLogger().setLevel(logging.INFO) - - -def open_pe_or_none(path: str): - try: - return pefile.PE(path) - except pefile.PEFormatError as err: - logger.debug(f"NOT a valid PE file: {err}") - return None diff --git a/floss/language/go/coverage.py b/floss/language/go/coverage.py index ec8a9b090..7598121e0 100644 --- a/floss/language/go/coverage.py +++ b/floss/language/go/coverage.py @@ -18,10 +18,12 @@ import argparse from typing import List +import pefile + 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.cli_common import add_common_args, configure_logging from floss.language.go.extract import extract_go_strings logger = logging.getLogger(__name__) @@ -36,8 +38,10 @@ def main(): configure_logging(args) - pe = open_pe_or_none(args.path) - if pe is None: + try: + pe = pefile.PE(args.path) + except pefile.PEFormatError as err: + logger.debug(f"NOT a valid PE file: {err}") return 1 path = pathlib.Path(args.path) diff --git a/floss/language/rust/coverage.py b/floss/language/rust/coverage.py index cef346a91..1e3f8492d 100644 --- a/floss/language/rust/coverage.py +++ b/floss/language/rust/coverage.py @@ -23,7 +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.cli_common import add_common_args, configure_logging from floss.language.rust.extract import extract_rust_strings logger = logging.getLogger(__name__) @@ -38,8 +38,10 @@ def main(): configure_logging(args) - pe = open_pe_or_none(args.path) - if pe is None: + try: + pe = pefile.PE(args.path) + except pefile.PEFormatError as err: + logger.debug(f"NOT a valid PE file: {err}") return 1 path = pathlib.Path(args.path)