Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
213 lines (171 loc) · 7.18 KB
/
Copy pathapp.py
File metadata and controls
213 lines (171 loc) · 7.18 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
import subprocess
from typing import Union
import click
import logging
import os
from enum import Enum
from application import Application
from device import DeviceType
from device.emulator import Emulator
from device.geny_aws import GenyAWS
from device.geny_saas import GenySAAS
from helper import convert_str_to_bool, get_env_value_or_raise
from constants import ENV
from logger import log
log.init()
logger = logging.getLogger("App")
def get_device(given_input: str) -> Union[Emulator, GenyAWS, GenySAAS, None]:
"""
Get Device object based on given input
:param given_input: device in string
:return: Platform object
"""
input_lower = given_input.lower()
if input_lower == DeviceType.EMULATOR.value.lower():
emu_av = get_env_value_or_raise(ENV.EMULATOR_ANDROID_VERSION)
emu_img_type = get_env_value_or_raise(ENV.EMULATOR_IMG_TYPE)
emu_sys_img = get_env_value_or_raise(ENV.EMULATOR_SYS_IMG)
emu_device = os.getenv(ENV.EMULATOR_DEVICE, "Nexus 5")
emu_data_partition = os.getenv(ENV.EMULATOR_DATA_PARTITION, "550m")
emu_additional_args = os.getenv(ENV.EMULATOR_ADDITIONAL_ARGS, "")
emu_name = os.getenv(ENV.EMULATOR_NAME, "{d}_{v}".format(
d=emu_device.replace(" ", "_").lower(), v=emu_av))
emu = Emulator(emu_name, emu_device, emu_av, emu_data_partition,
emu_additional_args, emu_img_type, emu_sys_img)
return emu
elif input_lower == DeviceType.GENY_AWS.value.lower():
return GenyAWS()
elif input_lower == DeviceType.GENY_SAAS.value.lower():
return GenySAAS()
else:
return None
@click.group(context_settings=dict(help_option_names=['-h', '--help']))
def cli():
pass
def start_appium() -> None:
if convert_str_to_bool(os.getenv(ENV.APPIUM)):
cmd = f"/usr/local/bin/appium"
app_appium = Application("Appium", cmd,
os.getenv(ENV.APPIUM_ADDITIONAL_ARGS, ""), False)
app_appium.start()
else:
logger.info("env APPIUM cannot be found, Appium is not started!")
def start_device() -> None:
given_pt = get_env_value_or_raise(ENV.DEVICE_TYPE)
selected_device = get_device(given_pt)
if selected_device is None:
raise RuntimeError(f"'{given_pt}' is invalid! Please check again!")
selected_device.create()
selected_device.start()
selected_device.wait_until_ready()
selected_device.reconfigure()
selected_device.keep_alive()
def start_display_screen() -> None:
cmd = "/usr/bin/Xvfb"
args = f"{os.getenv(ENV.DISPLAY)} " \
f"-screen {os.getenv(ENV.SCREEN_NUMBER)} " \
f"{os.getenv(ENV.SCREEN_WIDTH)}x" \
f"{os.getenv(ENV.SCREEN_HEIGHT)}x" \
f"{os.getenv(ENV.SCREEN_DEPTH)}"
d_screen = Application("d_screen", cmd, args, False)
d_screen.start()
def start_display_wm() -> None:
cmd = "/usr/bin/openbox-session"
d_wm = Application("d_wm", cmd)
d_wm.start()
def start_port_forwarder() -> None:
import socket
local_ip = socket.gethostbyname(socket.gethostname())
cmd = f"/usr/bin/socat tcp-listen:5554,bind={local_ip},fork tcp:127.0.0.1:5554 & " \
f"/usr/bin/socat tcp-listen:5555,bind={local_ip},fork tcp:127.0.0.1:5555"
pf = Application("port_forwarder", cmd)
pf.start()
def start_vnc_server() -> None:
cmd = "/usr/bin/x11vnc"
vnc_pass = os.getenv(ENV.VNC_PASSWORD)
if vnc_pass:
pass_path = os.path.join(os.getenv(ENV.WORK_PATH), ".vncpass")
subprocess.check_call(f"{cmd} -storepasswd {vnc_pass} {pass_path}", shell=True)
last_arg = f"-rfbauth {pass_path}"
else:
last_arg = "-nopw"
display = os.getenv(ENV.DISPLAY)
args = f"-display {display} -forever -shared {last_arg}"
vnc_server = Application("vnc_web", cmd, args, False)
vnc_server.start()
def start_vnc_web() -> None:
if convert_str_to_bool(os.getenv(ENV.WEB_VNC)):
vnc_port = get_env_value_or_raise(ENV.VNC_PORT)
vnc_web_port = get_env_value_or_raise(ENV.WEB_VNC_PORT)
cmd = "/opt/noVNC/utils/novnc_proxy"
args = f"--vnc localhost:{vnc_port} localhost:{vnc_web_port}"
vnc_web = Application("vnc_web", cmd, args, False)
vnc_web.start()
else:
logger.info("env WEB_VNC cannot be found, VNC_WEB is not started!")
@cli.command()
@click.argument("app", type=click.Choice([app.value for app in Application.App]))
def start(app):
selected_app = str(app).lower()
if selected_app == Application.App.APPIUM.value.lower():
start_appium()
elif selected_app == Application.App.DEVICE.value.lower():
start_device()
elif selected_app == Application.App.DISPLAY_SCREEN.value.lower():
start_display_screen()
elif selected_app == Application.App.DISPLAY_WM.value.lower():
start_display_wm()
elif selected_app == Application.App.PORT_FORWARDER.value.lower():
start_port_forwarder()
elif selected_app == Application.App.VNC_SERVER.value.lower():
start_vnc_server()
elif selected_app == Application.App.VNC_WEB.value.lower():
start_vnc_web()
else:
logger.error(f"application '{selected_app}' is not supported!")
class SharedComponent(Enum):
LOG = "log"
def shared_log() -> None:
if convert_str_to_bool(os.getenv(ENV.WEB_LOG)):
from http.server import BaseHTTPRequestHandler, HTTPServer
log_path = get_env_value_or_raise(ENV.LOG_PATH)
log_port = int(get_env_value_or_raise(ENV.WEB_LOG_PORT))
logger.info(f"Shared log is enabled! all logs can be found on port '{log_port}'")
class LogSharedHandler(BaseHTTPRequestHandler):
def do_GET(self):
# root path
if self.path == "/":
html = "<html><body>"
for f in os.listdir(log_path):
html += f"<p><a href=\"{f}\">{f}</a></p>"
html += "</body></html>"
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(html.encode())
# open each selected log file
else:
p = log_path + self.path
try:
with open(p, "rb") as file:
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(file.read())
except FileNotFoundError:
self.send_error(404, "File not found")
httpd = HTTPServer(('0.0.0.0', log_port), LogSharedHandler)
httpd.serve_forever()
else:
logger.info(f"Shared log is disabled! nothing to do!")
@cli.command()
@click.argument("component", type=click.Choice([component.value for component in SharedComponent]))
def share(component):
selected_component = str(component).lower()
if selected_component == SharedComponent.LOG.value.lower():
shared_log()
else:
logger.error(f"component '{component}' is not supported!")
if __name__ == '__main__':
cli()