1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 18:29:17 +02:00

improve the solution with one-liner command 'ps -lef||ps'

This commit is contained in:
song 2018-02-07 23:47:26 +08:00
parent 963de7fbd0
commit febac1dc0a

View file

@ -5,8 +5,6 @@ var Promise = require('bluebird')
var devutil = module.exports = Object.create(null) var devutil = module.exports = Object.create(null)
var commands = Object.create(null)
function closedError(err) { function closedError(err) {
return err.message.indexOf('closed') !== -1 return err.message.indexOf('closed') !== -1
} }
@ -41,48 +39,28 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) {
shell: true shell: true
} }
return new Promise(function(resolve) { return adb.shell(serial, 'ps -lef||ps')
if ('ps' in commands) { .then(function(out) {
resolve(commands) return new Promise(function(resolve) {
} var header = true
else { var pids = []
adb.getProperties(serial) out.pipe(split())
.then(function(properties) { .on('data', function(chunk) {
var version = properties['ro.build.version.release'] if (header&&chunk.toString().indexOf('bad pid') === -1) {
if (version.match(/^8\./) !== null) { header = false
commands.ps = 'ps -lef' }
} else {
else { var cols = chunk.toString().split(/\s+/)
commands.ps = 'ps' if (cols.pop() === bin && users[cols[0]]) {
} pids.push(Number(cols[1]))
resolve(commands)
})
}
})
.then(function(commands) {
return adb.shell(serial, [commands.ps])
.then(function(out) {
return new Promise(function(resolve) {
var header = true
var pids = []
out.pipe(split())
.on('data', function(chunk) {
if (header) {
header = false
} }
else { }
var cols = chunk.toString().split(/\s+/) })
if (cols.pop() === bin && users[cols[0]]) { .on('end', function() {
pids.push(Number(cols[1])) resolve(pids)
} })
}
})
.on('end', function() {
resolve(pids)
})
})
}) })
}) })
} }
devutil.waitForProcsToDie = function(adb, serial, comm, bin) { devutil.waitForProcsToDie = function(adb, serial, comm, bin) {