Skip to content

Commit 4b05c36

Browse files
author
chendianbo
committed
fix shell run fail issue in windows
1 parent e97c998 commit 4b05c36

File tree

8 files changed

+141
-33
lines changed

8 files changed

+141
-33
lines changed

doc/README.assets/2.png

7.37 KB
Loading

extend/extend_adb.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# --------------------------------------------------------------------------------------------------------------------------
2626
def doTopInfo():
27-
AdbUtils.printlnDump()
27+
AdbUtils.dumpTop()
2828

2929

3030
def doPullPackage(projPath, pkg):
@@ -146,9 +146,26 @@ def doDump(env_path, _mode):
146146
LoggerUtils.println(e)
147147

148148

149+
def doListPrint(tag, apps):
150+
LoggerUtils.light('\n[' + tag + ']: ')
151+
if CmnUtils.isEmpty(apps):
152+
LoggerUtils.w('[' + tag + ']: None')
153+
return
154+
for app in apps:
155+
LoggerUtils.println('[' + tag + ']: ' + app)
156+
157+
def doList():
158+
apps = AdbUtils.getInstallAppsWithSystem()
159+
doListPrint('Sys', apps)
160+
apps = AdbUtils.getInstallAppsWithThird()
161+
doListPrint('App', apps)
162+
apps = AdbUtils.getInstallAppsWithDisable()
163+
doListPrint('Disabled', apps)
164+
149165
def run():
150166
"""
151167
wing -adb top
168+
wing -adb list
152169
wing -adb pull <package name>
153170
wing -adb stop <package name>
154171
wing -adb clear <package name>
@@ -161,6 +178,7 @@ def run():
161178
if typ == 'stop': return doStopApp(envPath, za.get(3))
162179
if typ == 'clear': return doClearApp(envPath, za.get(3))
163180
if typ == 'dump': return doDump(envPath, za.get(3))
181+
if typ == 'list': return doList()
164182
assert 0, 'Unsupported type: ' + typ
165183

166184

res/help.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
wing init workspace2 tag_1.0 release.xml
66
sync [f] sync code from remote from manifests
77
f: Force switch to new branch, discard all local changes
8+
-update update to the latest version
89

910
wing git commands:
1011
-status Check if the local code has changed
@@ -44,6 +45,8 @@
4445
pull target apk from Android device
4546
dump {ui/sys/log}
4647
get all running services and log info from Android device
48+
list
49+
get all install app list
4750

4851
wing project commands:
4952
-create [type name]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def doInstall():
7474
try:
7575
if os.path.isdir(g_wing_path): FileUtils.remove(g_wing_path)
7676
FileUtils.copyDir(g_this_path, g_wing_path)
77-
# if os.path.isdir(g_wing_path + os.sep + '.git'): FileUtils.remove(g_wing_path + os.sep + '.git')
77+
if os.path.isdir(g_wing_path + os.sep + '.git'): FileUtils.remove(g_wing_path + os.sep + '.git')
7878
if os.path.isdir(g_wing_path + os.sep + '.idea'): FileUtils.remove(g_wing_path + os.sep + '.idea')
7979
LoggerUtils.println('copy bins ...')
8080
if CmnUtils.isOsWindows():

utils/utils_adb.py

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# adb shell content call ...
88
# @date: 2023.08.10 14:40:50
99

10-
import sys, os
10+
import sys, os, re
1111

1212
from utils.utils_import import ImportUtils
1313
from utils.utils_cmn import CmnUtils
@@ -81,7 +81,7 @@ def getFileTimestamp(f):
8181
try:
8282
ret = AdbUtils.doAdbCmd(cmd)
8383
if None == ret: return 0
84-
LoggerUtils.println(ret)
84+
# LoggerUtils.println(ret)
8585
ret = ret.strip()
8686
if len(ret) <= 0 or not ret.isdigit(): return 0
8787
return int(ret)
@@ -123,14 +123,18 @@ def pull(src, des):
123123
def push(src, des):
124124
src = os.path.abspath(src)
125125
ts1 = AdbUtils.getFileTimestamp(des)
126-
des2 = des + os.sep + os.path.basename(src)
127-
ts2 = AdbUtils.getFileTimestamp(des2)
126+
if os.path.basename(src) != os.path.basename(des):
127+
des2 = des + os.sep + os.path.basename(src)
128+
ts2 = AdbUtils.getFileTimestamp(des2)
129+
else:
130+
des2 = None
128131

129132
AdbUtils.doAdbCmd('push "%s" "%s"' % (src, des))
130133
tsNew = AdbUtils.getFileTimestamp(des)
131134
if 0 != tsNew and ts1 <= tsNew: return True
132-
tsNew = AdbUtils.getFileTimestamp(des2)
133-
if 0 != tsNew and ts2 <= tsNew: return True
135+
if des2 is not None:
136+
tsNew = AdbUtils.getFileTimestamp(des2)
137+
if 0 != tsNew and ts2 <= tsNew: return True
134138
return False
135139

136140
@staticmethod
@@ -171,7 +175,9 @@ def isInstalled(pkgName):
171175
def install(apkFile):
172176
ret = AdbUtils.doAdbCmd('install -r "%s"' % apkFile)
173177
if None == ret: return False
174-
return 0 <= ret.lower().find('success')
178+
if 0 <= ret.lower().find('success'): return True
179+
print(ret)
180+
return False
175181

176182
@staticmethod
177183
def uninstall(pkgName):
@@ -226,6 +232,11 @@ def __getInstallApps__(apps, cmd):
226232
apps.add(item)
227233
return True
228234

235+
@staticmethod
236+
def launchApp(pkgName, runSteps=1):
237+
cmd = "shell monkey -p %s -c android.intent.category.LAUNCHER %d" % (pkgName, runSteps)
238+
return AdbUtils.doAdbCmd(cmd)
239+
229240
@staticmethod
230241
def startActivity(pkgName, activityName, tName, tVal, args):
231242
"""
@@ -272,11 +283,11 @@ def sendBroadcast(action, tName, tVal, args):
272283
# return AdbUtils.doAdbCmd(cmd)
273284

274285
@staticmethod
275-
def callProvider(authorities, tName, tVal, args):
276-
astring = ''
277-
for k, v in args.items():
278-
astring += ' --extra %s:s:%s' % (k, v)
279-
cmd = 'shell content call --uri content://%s --method abg --extra %s:i:%d%s' % (authorities, tName, tVal, astring)
286+
def callProvider(authorities, method, arg, extras):
287+
extrastring = ''
288+
for k, v in extras.items():
289+
extrastring += '--extra %s:s:%s ' % (k, v)
290+
cmd = 'shell content call --uri content://%s --method %s --arg %s %s' % (authorities, method, arg, extrastring)
280291
return AdbUtils.doAdbCmd(cmd)
281292

282293
@staticmethod
@@ -474,6 +485,44 @@ def printlnDump():
474485
LoggerUtils.println('Top Activity: ')
475486
for pkg, line in activities.items(): LoggerUtils.println('\tpackage: ' + pkg + ', ' + line)
476487

488+
@staticmethod
489+
def __do_parser_package_name__(s):
490+
items = s.split('\n')
491+
for item in items:
492+
item = item.strip()
493+
matches = re.findall(r'\s[A-Za-z0-9_.]+/', item)
494+
if CmnUtils.isEmpty(matches): continue
495+
return matches[0][:-1].strip()
496+
return None
497+
498+
@staticmethod
499+
def dumpTop():
500+
# top windows
501+
wret = AdbUtils.doAdbCmd('shell dumpsys window | grep mCurrentFocus')
502+
LoggerUtils.println(None if CmnUtils.isEmpty(wret) else wret.strip())
503+
# top activity
504+
aret = AdbUtils.doAdbCmd('shell "dumpsys activity activities | grep mResumedActivity"')
505+
if CmnUtils.isEmpty(aret):
506+
aret = AdbUtils.doAdbCmd('shell "dumpsys activity activities | grep ResumedActivity"')
507+
LoggerUtils.println(None if CmnUtils.isEmpty(aret) else aret.strip())
508+
509+
LoggerUtils.println(' ')
510+
if CmnUtils.isEmpty(wret):
511+
LoggerUtils.w("Top window none")
512+
elif 0 <= wret.find('error:'):
513+
LoggerUtils.w("dump top window fail")
514+
else:
515+
# mCurrentFocus=Window{3222263 u0 com.sdu.didi.gsui/com.sdu.didi.gsui.main.MainActivity}
516+
LoggerUtils.println('Top Window App: ' + AdbUtils.__do_parser_package_name__(wret))
517+
518+
if CmnUtils.isEmpty(aret):
519+
LoggerUtils.w("Top activity none")
520+
elif 0 <= aret.find('error:'):
521+
LoggerUtils.w("dump top activity fail")
522+
else:
523+
# mResumedActivity: ActivityRecord{38e7b1 u0 com.sdu.didi.gsui/.main.MainActivity t4265}
524+
LoggerUtils.println('Top Activity App: ' + AdbUtils.__do_parser_package_name__(aret))
525+
477526

478527
def run():
479528
pass

utils/utils_cmn.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ def doCmd(cmd):
120120
line = ''
121121
while True:
122122
l = p.stdout.readline()
123-
if not l: break
124-
l = l.decode().strip()
123+
# if not l: break
124+
if CmnUtils.isEmpty(l):
125+
if p.poll() is not None: break
126+
continue
127+
l = l.decode().strip('\r\n')
125128
if len(l) <= 0: continue
126129
line += l + '\n'
127130
return line
@@ -139,8 +142,11 @@ def doCmd2File(cmd, fname, ignoreEmpty=True):
139142
with open(fname, 'w') as f:
140143
while True:
141144
line = p.stdout.readline()
142-
if not line: break
143-
line = line.decode().strip()
145+
# if not line: break
146+
if CmnUtils.isEmpty(line):
147+
if p.poll() is not None: break
148+
continue
149+
line = line.decode().strip('\r\n')
144150
if len(line) <= 0: continue
145151
f.write(line + '\n')
146152
return True
@@ -160,6 +166,7 @@ def doCmdEx(cmd):
160166
outResults = ''
161167
errResults = ''
162168
cmd = CmnUtils.formatCommand(cmd)
169+
# print(cmd)
163170
if cmd is None: return outResults, errResults
164171

165172
try:
@@ -184,14 +191,18 @@ def doCmdEx(cmd):
184191
def doCmdCall(cmd):
185192
isWin = CmnUtils.isOsWindows()
186193
cmd = CmnUtils.formatCommand(cmd)
194+
# print(cmd)
187195
if cmd is None: return True
188196

189197
try:
190198
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
191199
while True:
192200
line = p.stdout.readline()
193-
if not line: break
194-
line = line.decode().strip()
201+
# if not line: break
202+
if CmnUtils.isEmpty(line):
203+
if p.poll() is not None: break
204+
continue
205+
line = line.decode().strip('\r\n')
195206
if len(line) <= 0: continue
196207
if isWin:
197208
l1 = line.replace('\n', '').replace('\r', '')

utils/utils_file.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,30 @@ def replaceLine(typ, fileName, kv):
248248
# save
249249
with open(fileName, 'wb') as f: f.writelines(newLines)
250250

251+
@staticmethod
252+
def listFiles(path):
253+
for dirpath, dirnames, filenames in os.walk(path): return filenames
254+
return []
255+
256+
@staticmethod
257+
def listAll(path):
258+
for dirpath, dirnames, filenames in os.walk(path): return dirnames + filenames
259+
return []
260+
261+
@staticmethod
262+
def findLastModifyFile(path, pre=None, tail=None):
263+
ff = FileUtils.listFiles(path)
264+
if None == ff: return None
265+
lastTs = 0
266+
lastFile = None
267+
for f in ff:
268+
if not CmnUtils.isEmpty(pre) and not f.startswith(pre): continue
269+
if not CmnUtils.isEmpty(tail) and not f.endswith(tail): continue
270+
ts = FileUtils.getModifyTime(path + os.sep + f)
271+
if ts < lastTs: continue
272+
lastTs = ts
273+
lastFile = path + os.sep + f
274+
return lastFile
251275

252276
def run():
253277
print(FileUtils.sizeToString(20))

wing.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
pass
3030

3131
# wing version, wing -v
32-
g_ver = '1.2.0'
32+
g_ver = '1.2.3'
3333
# wing publish time, wing -v
34-
g_date = '2024.01.01'
34+
g_date = '2024.03.18'
3535
g_git_host = '[email protected]:63e5fbe89dee9309492bc30c'
3636
g_git_wing_remote = 'platform/wing'
3737
g_git_wing_branch = 'master'
@@ -80,14 +80,17 @@ def formatArgument(arg): return arg.replace('\\', '/') if isOsWindows() else arg
8080
def doCmd(cmd):
8181
cmd = formatCommand(cmd)
8282
if cmd is None: return ''
83-
83+
# println(cmd)
8484
try:
8585
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
8686
lines = ''
8787
while True:
8888
line = p.stdout.readline()
89-
if not line: break
90-
line = line.decode().strip()
89+
# if not line: break
90+
if isEmpty(line):
91+
if p.poll() is not None: break
92+
continue
93+
line = line.decode().strip('\r\n')
9194
if 0 < len(line): lines += line + '\n'
9295
return lines
9396
except Exception as e:
@@ -98,19 +101,19 @@ def doCmd(cmd):
98101
def doCmdCall(cmd):
99102
cmd = formatCommand(cmd)
100103
if cmd is None: return True
101-
104+
# println(cmd)
102105
try:
103106
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
104-
isWin = isOsWindows()
107+
# isWin = isOsWindows()
105108
while True:
106109
line = p.stdout.readline()
107-
if not line: break
108-
line = line.decode().strip()
110+
# if not line: break
111+
if isEmpty(line):
112+
if p.poll() is not None: break
113+
continue
114+
line = line.decode().strip('\r\n')
109115
if len(line) <= 0: continue
110-
if isWin:
111-
l1 = line.replace('\n', '').replace('\r', '')
112-
if len(l1) <= 0: continue
113-
print(line)
116+
println(line)
114117
return p.returncode is None or p.returncode == 0 or p.returncode == '0'
115118
except Exception as e:
116119
println(e)

0 commit comments

Comments
 (0)