Kea2-python 0.0.1b2__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Kea2-python might be problematic. Click here for more details.

kea2/fastbotManager.py ADDED
@@ -0,0 +1,144 @@
1
+ from dataclasses import asdict
2
+ import subprocess
3
+ import threading
4
+ import requests
5
+ from time import sleep
6
+ from .adbUtils import push_file
7
+ from pathlib import Path
8
+ from .utils import getLogger
9
+
10
+ from typing import IO, TYPE_CHECKING
11
+ if TYPE_CHECKING:
12
+ from .keaUtils import Options
13
+
14
+
15
+ logger = getLogger(__name__)
16
+
17
+
18
+ class FastbotManager:
19
+ def __init__(self, options: "Options", log_file: str):
20
+ self.options:"Options" = options
21
+ self.log_file: str = log_file
22
+ self.port = None
23
+ self.thread = None
24
+
25
+
26
+ def _activateFastbot(self, options: "Options") -> threading.Thread:
27
+ """
28
+ activate fastbot.
29
+ :params: options: the running setting for fastbot
30
+ :params: port: the listening port for script driver
31
+ :return: the fastbot daemon thread
32
+ """
33
+ cur_dir = Path(__file__).parent
34
+ push_file(
35
+ Path.joinpath(cur_dir, "assets/monkeyq.jar"),
36
+ "/sdcard/monkeyq.jar",
37
+ device=options.serial
38
+ )
39
+ push_file(
40
+ Path.joinpath(cur_dir, "assets/fastbot-thirdpart.jar"),
41
+ "/sdcard/fastbot-thirdpart.jar",
42
+ device=options.serial,
43
+ )
44
+ push_file(
45
+ Path.joinpath(cur_dir, "assets/framework.jar"),
46
+ "/sdcard/framework.jar",
47
+ device=options.serial
48
+ )
49
+ push_file(
50
+ Path.joinpath(cur_dir, "assets/fastbot_libs/arm64-v8a"),
51
+ "/data/local/tmp",
52
+ device=options.serial
53
+ )
54
+ push_file(
55
+ Path.joinpath(cur_dir, "assets/fastbot_libs/armeabi-v7a"),
56
+ "/data/local/tmp",
57
+ device=options.serial
58
+ )
59
+ push_file(
60
+ Path.joinpath(cur_dir, "assets/fastbot_libs/x86"),
61
+ "/data/local/tmp",
62
+ device=options.serial
63
+ )
64
+ push_file(
65
+ Path.joinpath(cur_dir, "assets/fastbot_libs/x86_64"),
66
+ "/data/local/tmp",
67
+ device=options.serial
68
+ )
69
+
70
+ t = self._startFastbotService(options)
71
+ logger.info("Running Fastbot...")
72
+
73
+ return t
74
+
75
+
76
+ def check_alive(self, port):
77
+ """
78
+ check if the script driver and proxy server are alive.
79
+ """
80
+ for _ in range(10):
81
+ sleep(2)
82
+ try:
83
+ requests.get(f"http://localhost:{port}/ping")
84
+ return
85
+ except requests.ConnectionError:
86
+ logger.info("waiting for connection.")
87
+ pass
88
+ raise RuntimeError("Failed to connect fastbot")
89
+
90
+
91
+ def _startFastbotService(self) -> threading.Thread:
92
+ shell_command = [
93
+ "CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar:/sdcard/fastbot-thirdpart.jar",
94
+ "exec", "app_process",
95
+ "/system/bin", "com.android.commands.monkey.Monkey",
96
+ "-p", *self.options.packageNames,
97
+ "--agent-u2" if self.options.agent == "u2" else "--agent",
98
+ "reuseq",
99
+ "--running-minutes", f"{self.options.running_mins}",
100
+ "--throttle", f"{self.options.throttle}",
101
+ "--bugreport",
102
+ ]
103
+
104
+ if self.options.profile_period:
105
+ shell_command += ["--profile-period", f"{self.options.profile_period}"]
106
+
107
+ shell_command += ["-v", "-v", "-v"]
108
+
109
+ full_cmd = ["adb"] + (["-s", self.options.serial] if self.options.serial else []) + ["shell"] + shell_command
110
+
111
+ outfile = open(self.log_file, "w", encoding="utf-8", buffering=1)
112
+
113
+ logger.info("Options info: {}".format(asdict(self.options)))
114
+ logger.info("Launching fastbot with shell command:\n{}".format(" ".join(full_cmd)))
115
+ logger.info("Fastbot log will be saved to {}".format(outfile.name))
116
+
117
+ # process handler
118
+ proc = subprocess.Popen(full_cmd, stdout=outfile, stderr=outfile)
119
+ t = threading.Thread(target=self.close_on_exit, args=(proc, outfile), daemon=True)
120
+ t.start()
121
+
122
+ return t
123
+
124
+ def close_on_exit(self, proc: subprocess.Popen, f: IO):
125
+ self.return_code = proc.wait()
126
+ f.close()
127
+ if self.return_code != 0:
128
+ raise RuntimeError(f"Fastbot Error: Terminated with [code {self.return_code}]")
129
+
130
+ def get_return_code(self):
131
+ if self.thread:
132
+ logger.info("Waiting for Fastbot to exit.")
133
+ self.thread.join()
134
+ return self.return_code
135
+
136
+ def start(self):
137
+ self.thread = self._startFastbotService()
138
+
139
+ def join(self):
140
+ if self.thread:
141
+ self.thread.join()
142
+
143
+
144
+