-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathauto.py
163 lines (137 loc) · 4.49 KB
/
auto.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
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
import sys
import argparse
import os
import subprocess
import traceback
OPT_ARCH = "arch"
OPT_PATH = "path"
HELP_ARCH = "The target device architecture. Please specify arm or x86"
HELP_PATH = "The working directory to store our binary. /data/local/tmp for example."
WARN_ARCH = "The --arch argument is not designated."
WARN_PATH = "The --work argument is not designated."
WARN_INVALID_ARCH = "The --arch argument is invalid. Please specify arm or x86."
ARCH_ARM = "arm"
ARCH_X86 = "x86"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--install", dest="install", action="store_true")
parser.add_argument("--deploy", dest="deploy", action="store_true")
parser.add_argument("--%s" % (OPT_ARCH), help=HELP_ARCH)
parser.add_argument("--%s" % (OPT_PATH), help=HELP_PATH)
parser.set_defaults(install=False)
parser.set_defaults(deploy=False)
args = parser.parse_args()
install = args.install
deploy = args.deploy
args = vars(args)
if install:
compile_engine()
if deploy:
arch = None
if OPT_ARCH in args:
arch = args[OPT_ARCH]
if not arch:
print WARN_ARCH
return
if arch != ARCH_ARM and arch != ARCH_X86:
print WARN_INVALID_ARCH
return
path_device = None
if OPT_PATH in args:
path_device = args[OPT_PATH]
if not path_device:
print WARN_PATH
return
deploy_engine_and_tool(arch, path_device)
def compile_engine():
cmd_ndk_build = "ndk-build"
cmd_ant_compile = "ant compile"
cmd_ant_jar = "ant build-jar"
path_curr = os.getcwd()
# Compile the ProbeDroid launcher.
print("[Build Launcher ...]")
path_launcher = os.path.join(path_curr, "launcher", "jni")
os.chdir(path_launcher)
rc = run_command(cmd_ndk_build)
if rc == False:
return
os.chdir(path_curr)
print("\n")
# Compile the ProbeDroid core library.
print("[Build Core Library ...]")
path_core = os.path.join(path_curr, "engine", "jni")
os.chdir(path_core)
rc = run_command(cmd_ndk_build)
if rc == False:
return
os.chdir(path_curr)
print("\n")
# Compile the ProbeDroid exported API jar.
print("[Build Instrument API Jar ...]")
path_engine = os.path.join(path_curr, "engine")
os.chdir(path_engine)
rc = run_command(cmd_ant_compile)
if rc == False:
return
rc = run_command(cmd_ant_jar)
if rc == False:
return
os.chdir(path_curr)
print("\n")
def deploy_engine_and_tool(arch, path_device):
prefix_adb_push = "adb push"
path_curr = os.getcwd()
# Migrate the ProbeDroid launcher.
print("[Migrate Launcher ...]")
path_launcher = os.path.join(path_curr, "launcher", "libs")
if arch == ARCH_ARM:
path_launcher = os.path.join(path_launcher, "armeabi-v7a", "launcher")
else:
path_launcher = os.path.join(path_launcher, "x86", "launcher")
cmd_push = "%s %s %s" % (prefix_adb_push, path_launcher, path_device)
rc = run_command(cmd_push)
if rc == False:
return
print("")
# Migrate the ProbeDroid core library.
print("[Migrate Core Library ...]")
path_core = os.path.join(path_curr, "engine", "libs")
if arch == ARCH_ARM:
path_core = os.path.join(path_core, "armeabi-v7a", "libProbeDroid.so")
else:
path_core = os.path.join(path_core, "x86", "libProbeDroid.so")
cmd_push = "%s %s %s" % (prefix_adb_push, path_core, path_device)
rc = run_command(cmd_push)
if rc == False:
return
print("")
# Migrate the instrumentation tools.
print("[Migrate Instrumentation Tools ...]")
path_tool = os.path.join(path_curr, "tools")
list_file = os.listdir(path_tool)
for name_file in list_file:
if name_file.endswith(".apk") == False:
continue
path_file = os.path.join(path_tool, name_file)
cmd_push = "%s %s %s" % (prefix_adb_push, path_file, path_device)
print("%s" % (name_file))
rc = run_command(cmd_push)
if rc == False:
return
print("")
def run_command(cmd):
rc = True
try:
env = os.environ
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env, shell=True)
while True:
line = proc.stdout.readline()
if line == '':
break
print line.rstrip()
except:
traceback.print_exc()
rc = False
return rc
if __name__ == "__main__":
main()