Replace regexes with parse()

This commit is contained in:
LuK1337
2021-10-13 17:08:06 +02:00
parent 7abac34edf
commit 4e86318340

View File

@@ -14,12 +14,16 @@
import errno import errno
from glob import glob from glob import glob
import os import os
import re
import subprocess import subprocess
import sys import sys
from xml.etree import ElementTree from xml.etree import ElementTree
# Get external packages # Get external packages
try:
from parse import parse
except ImportError:
print('Please install the "parse" package via pip3.')
exit(errno.ENOPKG)
try: try:
import requests import requests
except ImportError: except ImportError:
@@ -92,8 +96,7 @@ for apk in glob(GLOB_APK_STR):
# Extract package name from the output # Extract package name from the output
# Output looks like: # Output looks like:
# package: my.package.name # package: my.package.name
matches = re.search(r'package: ([\S]+)', lines[0]) package_name = parse('package: {}', lines[0])[0]
package_name = matches.group(1)
# Create empty entry if package is not in dic # Create empty entry if package is not in dic
if package_name not in privapp_permissions_dict: if package_name not in privapp_permissions_dict:
privapp_permissions_dict[package_name] = (set(), set()) privapp_permissions_dict[package_name] = (set(), set())
@@ -101,13 +104,11 @@ for apk in glob(GLOB_APK_STR):
# Relevant output looks like: # Relevant output looks like:
# uses-permission: name='permission' # uses-permission: name='permission'
for line in lines[1:]: for line in lines[1:]:
if line.startswith('uses-permission'): # Extract permission name and add it to the dictionary if it's
# Extract permission name and add it to the dictionary if it's # one of the privileged permissions we extracted earlier
# one of the privileged permissions we extracted earlier if perm_name := parse('uses-permission: name=\'{}\'', line):
matches = re.search(r"uses-permission.*: name='([\S]+)'", line) if perm_name[0] in privileged_permissions:
perm_name = matches.group(1) privapp_permissions_dict[package_name][1].add(perm_name[0])
if perm_name in privileged_permissions:
privapp_permissions_dict[package_name][1].add(perm_name)
# Keep track of exit code # Keep track of exit code
rc = 0 rc = 0