Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
import subprocess as sp
import multiprocessing as mp
import os
import glob

PREFIX = "List of devices attached\r\n"
SUFFIX = "\tdevice\r\n\r\n"
MIDDLE = "\tdevice\r\n"
APKPATH = os.path.join(os.path.dirname(__file__))


class DeviceManager():
    def __init__(self):
        self.devices = []
        self.apks = []
        self.updateDeviceInfo()

    def doInstall(self, deviceIndex):
        deviceName = self.devices[deviceIndex]
        apps = self.apks[deviceIndex]
        for app in apps:
            cmd = f"adb -s {deviceName} install {app}"
            print(f"Installing for device {deviceIndex} apk: {app}")
            sp.call(cmd)

    def updateDeviceInfo(self):
        self.devices = sp.check_output("adb devices").decode(
            "utf-8").replace(PREFIX, "").replace(SUFFIX, "").split(MIDDLE)
        listApk = glob.glob(APKPATH + "/*.test")
        remainer = len(listApk) % len(self.devices)
        start = end = 0
        for i in range(len(self.devices)):
            num = len(listApk) // len(self.devices)
            if (remainer > 0):
                num += 1
                remainer -= 1
            self.apks.append([])
            end += num
            if (end > len(listApk)):
                end = len(listApk)
            for j in range(start, end):
                self.apks[i].append(listApk[j])
            start += num

    def lenOfDevices(self):
        return len(self.devices)

    def lenOfApks(self):
        return len(self.apks)


if __name__ == "__main__":
    deviceManager = DeviceManager()
    lenDevice = deviceManager.lenOfDevices()
    pool = mp.Pool(processes=lenDevice)
    r = pool.map_async(deviceManager.doInstall, range(lenDevice))
    pool.close()
    pool.join()