single function to tame androguard's verbose default output

This commit is contained in:
Hans-Christoph Steiner 2024-04-10 16:46:06 +02:00
parent 39e0b12957
commit 426f08ccbc
No known key found for this signature in database
GPG Key ID: 3E177817BA1B9BFA
3 changed files with 25 additions and 5 deletions

View File

@ -2629,12 +2629,34 @@ def get_file_extension(filename):
return os.path.splitext(filename)[1].lower()[1:]
def _androguard_logging_level(level=logging.ERROR):
"""Tames androguard's default debug output.
There should be no debug output when the functions are being used
via the API. Otherwise, the output is controlled by the --verbose
flag.
"""
if options and options.verbose:
level = logging.WARNING
for name in (
'androguard.apk',
'androguard.axml',
'androguard.core.api_specific_resources',
'androguard.core.apk',
'androguard.core.axml',
):
logging.getLogger(name).setLevel(level)
def _get_androguard_APK(apkfile):
try:
# these were moved in androguard 4.0
from androguard.core.apk import APK
except ImportError:
from androguard.core.bytecodes.apk import APK
_androguard_logging_level()
return APK(apkfile)
@ -2681,6 +2703,8 @@ def is_debuggable_or_testOnly(apkfile):
from androguard.core.axml import AXMLParser, format_value, START_TAG
except ImportError:
from androguard.core.bytecodes.axml import AXMLParser, format_value, START_TAG
_androguard_logging_level()
with ZipFile(apkfile) as apk:
with apk.open('AndroidManifest.xml') as manifest:
axml = AXMLParser(manifest.read())
@ -2753,6 +2777,7 @@ def get_apk_id_androguard(apkfile):
from androguard.core.axml import AXMLParser, format_value, START_TAG, END_TAG, TEXT, END_DOCUMENT
except ImportError:
from androguard.core.bytecodes.axml import AXMLParser, format_value, START_TAG, END_TAG, TEXT, END_DOCUMENT
_androguard_logging_level()
appid = None
versionCode = None

View File

@ -49,8 +49,6 @@ class BuildTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('androguard.axml')
logger.setLevel(logging.INFO) # tame the axml debug messages
self.basedir = os.path.join(localmodule, 'tests')
os.chdir(self.basedir)
fdroidserver.common.config = None

View File

@ -82,9 +82,6 @@ class UpdateTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.INFO)
logging.getLogger('androguard.apk').setLevel(logging.WARNING)
logging.getLogger('androguard.axml').setLevel(logging.INFO)
logging.getLogger('androguard.core.api_specific_resources').setLevel(logging.INFO)
from PIL import PngImagePlugin
logging.getLogger(PngImagePlugin.__name__).setLevel(logging.INFO)