Skip to content

Commit 8499d4f

Browse files
committed
2015.01.26
1 parent a9da0ef commit 8499d4f

File tree

8 files changed

+135
-112
lines changed

8 files changed

+135
-112
lines changed

python/batch_install.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,56 @@
1-
#coding=utf-8
1+
# -*- coding: utf-8 -*-
22

3-
#需要在脚本所在目录下有个AndroidAdb.exe程序,该adb可以支持安装以中文命名的apk
3+
'''
4+
Created on 2015年1月26日
5+
6+
@author: xuxu
7+
'''
8+
9+
#需要在脚本所在目录AndroidAdb目录下有个AndroidAdb.exe程序,该adb可以支持安装以中文命名的apk
410
#需要将apk文件放在脚本所在目录下的Apps目录下
511

612
import os
713
import time
814
import sys
915

16+
#检查AndroidAdb.exe
1017
def check_adb():
11-
if os.path.isfile(os.getcwd() + "\\AndroidAdb\\AndroidAdb.exe"):
18+
if os.path.isfile("%s\\AndroidAdb\\AndroidAdb.exe" %os.getcwd()):
1219
return True
1320
else:
1421
return False
1522

23+
#检查Apps目录
1624
def check_dir():
17-
if os.path.isdir(os.getcwd() + "\\Apps"):
25+
if os.path.isdir("%s\\Apps" %os.getcwd()):
1826
return True
1927
else:
2028
return False
2129

30+
#安装应用
2231
def install():
2332
count = 0
24-
for path, subdir, files in os.walk(os.getcwd() + "\\Apps"):
33+
apps_dir = "%s\\Apps" %os.getcwd()
34+
for path, subdir, files in os.walk(apps_dir):
2535
for apk in files:
26-
os.system(os.getcwd() + "\\AndroidAdb\\AndroidAdb.exe install " + os.path.join(path, apk))
36+
os.popen("%s\\AndroidAdb\\AndroidAdb.exe install %s" %(os.getcwd(), os.path.join(path, apk)))
2737
count += 1
2838

29-
print "\n" + str(count) + " apps install complete."
39+
print "\n%s apps install complete." %str(count)
3040

3141
if __name__ == "__main__":
3242
if check_adb():
3343
pass
3444
else:
3545
print "AndroidAdb.exe not exist."
36-
time.sleep(5)
37-
sys.exit(1)
46+
time.sleep(3)
47+
sys.exit(0)
3848

3949
if check_dir():
4050
pass
4151
else:
4252
print "Apps Directory not exist"
43-
time.sleep(5)
44-
sys.exit(1)
53+
time.sleep(3)
54+
sys.exit(0)
4555

4656
install()

python/batch_uninstall.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
#!/usr/bin/python
2-
#coding=utf-8
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
33

4-
import os
5-
import sys
4+
'''
5+
Created on 2015年1月26日
66
7-
#卸载手机上的第三方应用
7+
@author: xuxu
8+
'''
9+
import os
810

911
def uninstall():
1012
os.popen("adb wait-for-device")
@@ -17,4 +19,4 @@ def uninstall():
1719
if __name__ == "__main__":
1820
uninstall()
1921
print " "
20-
print "All the third-party applications uninstall successes."
22+
print "All the third-party applications uninstall successes."

python/getApp.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
1-
#!/usr/bin/python
2-
#coding=utf-8
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
33

4-
import os
5-
import re
6-
import platform
4+
'''
5+
Created on 2015年1月26日
76
8-
#打开手机应用,运行脚本,会将该应用对应的apk复制到本地的Apps文件夹下
7+
@author: xuxu
8+
'''
99

10-
PATH = lambda p: os.path.abspath(p)
10+
import os
1111

12-
system = platform.system()
13-
if system is "Windows":
14-
find_util = "findstr"
15-
else:
16-
find_util = "grep"
12+
from scriptUtils import utils
1713

18-
def get_current_package_name():
19-
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
20-
os.popen("adb wait-for-device")
21-
out = os.popen("adb shell dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).read()
22-
package_name = pattern.findall(out)[0].split("/")[0]
14+
#打开手机上的第三方应用,运行脚本,会将该应用对应的apk复制到本地的App文件夹下
2315

24-
return package_name
16+
PATH = lambda p: os.path.abspath(p)
2517

2618
def get_match_apk(package_name, path):
2719
list = []
28-
for packages in os.popen("adb shell pm list packages -f %s" %package_name).readlines():
20+
for packages in utils.shell("pm list packages -f %s" %package_name).stdout.readlines():
2921
list.append(packages.split(":")[-1].split("=")[0])
30-
apk_name = list[0].split("/")[-1]
31-
os.popen("adb pull %s %s" %(list[0], path))
22+
# apk_name = list[0].split("/")[-1]
23+
utils.adb("pull %s %s" %(list[0], path)).wait()
3224

3325
if __name__ == "__main__":
34-
path = PATH(os.getcwd() + "/Apps")
35-
if not os.path.isdir(PATH(os.getcwd() + "/Apps")):
26+
path = PATH("%s/App" %os.getcwd())
27+
if not os.path.isdir("%s/App" %PATH(os.getcwd())):
3628
os.makedirs(path)
3729

38-
get_match_apk(get_current_package_name(), path)
30+
get_match_apk(utils.get_current_package_name(), path)
31+

python/get_current_activity.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
#!/usr/bin/python
2-
#coding=utf-8
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2015年1月26日
6+
7+
@author: xuxu
8+
'''
39

410
import os
5-
import platform
6-
import re
711
import sys
812

9-
#获取设备当前界面的activity,保存至当前目录下的CurrentActivity.txt文件中
13+
from scriptUtils import utils
1014

1115
PATH = lambda p: os.path.abspath(p)
1216

13-
system = platform.system()
14-
if system is "Windows":
15-
find_util = "findstr"
16-
else:
17-
find_util = "grep"
18-
19-
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
20-
21-
def get_activity():
22-
os.popen("adb wait-for-device")
23-
out = os.popen("adb shell dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).read()
24-
return pattern.findall(out)[0]
25-
2617

2718
if __name__ == "__main__":
28-
f = open(PATH(os.getcwd() + "/CurrentActivity.txt"), "w")
29-
f.write("Activity: \n" + get_activity())
19+
f = open(PATH("%s/CurrentActivity.txt" %os.getcwd()), "w")
20+
f.write("Activity: \n%s" %utils.get_current_activity())
3021
f.close()
31-
sys.exit(0)
22+
print "Completed"
23+
sys.exit(0)

python/get_current_pkginfo.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
#coding=utf-8
1+
# -*- coding: utf-8 -*-
2+
3+
'''
4+
Created on 2015年1月26日
5+
6+
@author: xuxu
7+
'''
28

39
import os
410
import tempfile
511
import re
612

7-
#使用aapt获取设备上当前应用的apk信息,保存至当前目录下的PackageInfo.txt中
13+
from scriptUtils import utils
814

915
tempFile = tempfile.gettempdir()
1016

@@ -17,24 +23,16 @@ def get_aapt():
1723
else:
1824
return "ANDROID_HOME not exist"
1925

20-
def get_current_package_name():
21-
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
22-
os.popen("adb wait-for-device")
23-
out = os.popen("adb shell dumpsys window w | findstr \/ | findstr name=").read()
24-
package_name = pattern.findall(out)[0].split("/")[0]
25-
26-
return package_name
27-
2826
def get_match_apk(package_name):
2927
list = []
30-
for packages in os.popen("adb shell pm list packages -f %s" %package_name).readlines():
28+
for packages in utils.shell("pm list packages -f %s" %package_name).stdout.readlines():
3129
list.append(packages.split(":")[-1].split("=")[0])
3230
apk_name = list[0].split("/")[-1]
33-
os.popen("adb pull %s %s" %(list[0], tempFile))
31+
utils.adb("pull %s %s" %(list[0], tempFile)).wait()
3432

35-
return tempFile + "\\" + apk_name
33+
return "%s\\%s" %(tempFile, apk_name)
3634

3735
if __name__ == "__main__":
38-
os.popen(get_aapt() + \
39-
" dump badging %s > PackageInfo.txt" %get_match_apk(get_current_package_name()))
40-
os.popen("del %s\\*.apk" % tempFile)
36+
os.popen("%s dump badging %s > PackageInfo.txt" %(get_aapt(), utils.get_current_package_name()))
37+
os.popen("del %s\\*.apk" %tempFile)
38+
print "Completed"

python/get_package_name.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1-
#!/usr/bin/python
2-
#coding=utf-8
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2015年1月26日
6+
7+
@author: xuxu
8+
'''
39

410
import os
5-
import platform
6-
import re
711
import sys
812

9-
#获取当前应用的包名,保存至当前目录下的PackageName.txt文件中
13+
from scriptUtils import utils
1014

1115
PATH = lambda p: os.path.abspath(p)
1216

13-
system = platform.system()
14-
if system is "Windows":
15-
find_util = "findstr"
16-
else:
17-
find_util = "grep"
18-
19-
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
20-
21-
def get_package_name():
22-
os.popen("adb wait-for-device")
23-
out = os.popen("adb shell dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).read()
24-
package_name = pattern.findall(out)[0].split("/")[0]
25-
26-
return package_name
27-
28-
2917
if __name__ == "__main__":
30-
f = open(PATH(os.getcwd() + "/PackageName.txt"), "w")
31-
f.write("Package: \n" + get_package_name())
18+
f = open(PATH("%s/PackageName.txt" %os.getcwd()), "w")
19+
f.write("Package: \n%s" %utils.get_current_package_name())
3220
f.close()
21+
print "Completed"
3322
sys.exit(0)

python/screenrecord.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2015年1月26日
6+
7+
@author: xuxu
8+
'''
9+
import os
10+
11+
from scriptUtils import utils
12+
13+
#需要Android4.4及4.4以上版本,运行脚本后可录制设备上的操作,默认使用手机分辨率,时间5min。手动按Enter结束录制。
14+
#录制结果存放于当前目录下的video目录下
15+
16+
PATH = lambda p: os.path.abspath(p)
17+
18+
def record():
19+
utils.shell("screenrecord --time-limit 300 /data/local/tmp/video.mp4")
20+
21+
input_key = raw_input("Please press the Enter key to stop recording:\n")
22+
if input_key == "":
23+
utils.adb("kill-server")
24+
25+
print "Get Video file..."
26+
27+
path = PATH("%s/video" %os.getcwd())
28+
if not os.path.isdir("%s/video" %PATH(os.getcwd())):
29+
os.makedirs(path)
30+
31+
utils.adb("pull /data/local/tmp/video.mp4 %s" %PATH("%s/video" %PATH(os.getcwd()))).wait()
32+
utils.adb("pull /data/local/tmp/video.mp4 %s" %PATH("%s/%s.mp4" %(path, utils.timestamp()))).wait()
33+
34+
if __name__ == "__main__":
35+
record()
36+
print "Completed"

python/screenshot.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#!/usr/bin/env python
2-
#coding=utf-8
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2015年1月26日
6+
7+
@author: xuxu
8+
'''
39

410
import os
5-
import time
611

7-
#截取设备上的屏幕,保存至当前目录下的screenshot目录
12+
from scriptUtils import utils
813

914
PATH = lambda p: os.path.abspath(p)
1015

1116
def screenshot():
12-
path = PATH(os.getcwd() + "/screenshot")
13-
timestamp = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
14-
os.popen("adb wait-for-device")
15-
os.popen("adb shell screencap -p /data/local/tmp/tmp.png")
16-
if not os.path.isdir(PATH(os.getcwd() + "/screenshot")):
17+
path = PATH("%s/screenshot" %os.getcwd())
18+
utils.shell("screencap -p /data/local/tmp/tmp.png").wait()
19+
if not os.path.isdir("%s/screenshot" %PATH(os.getcwd())):
1720
os.makedirs(path)
18-
os.popen("adb pull /data/local/tmp/tmp.png " + PATH(path + "/" + timestamp + ".png"))
19-
os.popen("adb shell rm /data/local/tmp/tmp.png")
20-
print "success"
21+
utils.adb("pull /data/local/tmp/tmp.png %s" %PATH("%s/%s.png" %(path, utils.timestamp()))).wait()
22+
utils.shell("rm /data/local/tmp/tmp.png")
2123

2224
if __name__ == "__main__":
2325
screenshot()
26+
print "success"

0 commit comments

Comments
 (0)