-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
42 lines (31 loc) · 1.36 KB
/
utils.py
File metadata and controls
42 lines (31 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
import re
from flask import jsonify
import subprocess
class Utils:
def __init__(self) -> None:
pass
def _validate_package_name(self, package_name: str):
"""Returns whether package_name is installed.
Checks whether package_name is installed. package_name could be a
partial name. Returns the full package name if only one entry matches.
if more than one matches, it exits with an error.
To avoid using partial name matches, -f should be used from command
line.
Args:
package_name: A string representing the name of the application to
be targeted.
Returns:
A string representing a validated full package name.
"""
cmd = ('adb', 'shell', 'pm', 'list', 'packages')
outstr = subprocess.run(cmd, check=True, encoding='utf-8',
capture_output=True).stdout.strip()
partial_pkg_regexp = fr'^package:(.*{re.escape(package_name)}.*)$'
regexp = partial_pkg_regexp
# IGNORECASE is needed because some package names use uppercase letters.
matches = re.findall(regexp, outstr, re.MULTILINE | re.IGNORECASE)
if len(matches) == 0:
return jsonify([f'No installed package matches "{package_name}"'])
if len(matches) >= 1:
return jsonify(matches)