-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
134 lines (118 loc) · 4.77 KB
/
main.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import sys
import argparse
from loguru import logger
from modules import apps
from modules import config
from modules import arg_parser
from modules.reporter import Reporter, BundleReporter
from modules.agent import Agent, ActivityAgent, MonkeyAgent
from modules.done_list_handler import Status
from modules.entities import Apk
from modules.exceptions import AbsentActivityException, UserExitException, ErrorInstallingException, ErrorUninstallingException, NotEnoughSpaceException
from modules.tester import Tester
from modules import shellhelper
def setup_logging():
logger.add("log.log", format="{time} - {level} - {message}")
def main():
setup_logging()
parser = arg_parser.get_parser()
args = parser.parse_args()
run_actions(parser, args)
def get_agent(args):
if args.monkey:
return MonkeyAgent(config.MONKEY_SEED, config.MONKEY_THROTTLE,
args.events)
else:
return ActivityAgent()
def run_actions(parser, args):
if args is None:
args = parser.parse_args()
if args.subcmd == "run":
reporter = Reporter(args.output_dir)
agent = get_agent(args)
directory, filename = os.path.split(args.apk_path)
apk = Apk(filename, directory)
logger.info("START - {}".format(apk.package))
tester = Tester(apk, agent, reporter)
tester.test(manual=False)
elif args.subcmd == "run_dir":
reporter = BundleReporter(args.output_dir)
agent = get_agent(args)
logger.info("INPUT: {}".format(args.input_dir))
all_apps_list = apps.get_all_file_paths(args.input_dir)
start_testing(all_apps_list, agent, args.wait, reporter)
elif args.subcmd == "run_list":
reporter = BundleReporter(args.output_dir)
agent = get_agent(args)
app_list = apps.read_apps_list(args.input_list)
start_testing(app_list, agent, args.wait, reporter)
else:
parser.print_usage()
def start_testing(all_apps_paths, agent, wait, reporter):
""" A simple install/launch automated tester. Reports the apps that
successfully run on a chooen device.
"""
logger.info("START EXPERIMENT")
logger.info("OUTPUT: {}".format(reporter.output_dir))
list_handler = reporter.done_list_handler
apps_to_process, done_project_count, overall_apps = apps.get_apps_to_process(
all_apps_paths, list_handler)
counter = done_project_count
fail_counter = list_handler.get_fail_counter()
for app_path in apps_to_process:
logger.info(
'================================================================================================================================================'
)
counter += 1
logger.info(
f'{app_path}: {counter} OF {overall_apps}, FAIL TO RUN: {fail_counter}'
)
apk = Apk(app_path)
fail_counter = run_single_app(apk,
list_handler,
agent,
reporter,
fail_counter=fail_counter,
overall_apps=overall_apps)
reporter.close_crash_report()
list_handler.close()
def run_single_app(apk,
list_handler,
agent,
reporter,
fail_counter=0,
overall_apps=1):
tester = Tester(apk, agent, reporter)
try:
tester.test(manual=False)
except ErrorInstallingException:
fail_counter += 1
logger.exception(f'Cannot install app {apk.name}')
list_handler.write(apk.name, Status.FAIL, reason='INSTALLATION ERROR')
except NotEnoughSpaceException:
logger.exception(
f'Cannot install app {apk.name} because there is not enough space. \
Stopping tool. Please, wipe data, then run tool again')
sys.exit()
except AbsentActivityException:
fail_counter += 1
logger.exception(f'Absent main activity for app {apk.name}')
tester.uninstall()
list_handler.write(apk.name, Status.FAIL, reason='ABSENT ACTIVITY')
except UserExitException:
logger.info(f'User has chosen to exit while testing {apk.name}')
list_handler.write(apk.name, Status.UNDEFINED, reason='USER_EXIT')
tester.uninstall()
sys.exit()
except ErrorUninstallingException:
logger.exception(f'Cannot uninstall {apk.name}')
list_handler.write(apk.name, Status.SUCCESS, comment='UNINSTALL ERROR')
except BaseException:
fail_counter += 1
logger.exception(f'Exception for app {apk.name}')
tester.uninstall()
list_handler.write(apk.name, Status.FAIL, reason='UNKNOWN')
return fail_counter
if __name__ == "__main__":
main()