Я пытался использовать модуль подпроцесса и win32, но он не выполнил мое требование, потому что SendKeys работает только тогда, когда мы не выполняем никаких действий во время выполнения скрипта
Мне нужно подключить устройства adb к ПК, а затем передать команду adb reboot bootloader каждому устройству, запустив командные строки для каждого устройства
Могу ли я получить помощь в этом?
import time
import re
import win32com.client
'''import win32api
from win32gui import GetWindowText
import win32clipboard
import win32con
from win32gui import GetForegroundWindow
from win32gui import SetForegroundWindow
from win32gui import EnumWindows'''
from win32process import GetWindowThreadProcessId
class ActivateVenv:
''' def set_cmd_to_foreground(self, hwnd, extra):
"""sets first command prompt to forgeround"""
if "cmd.exe" in GetWindowText(hwnd):
SetForegroundWindow(hwnd)
return'''
def get_pid(self):
"""gets process id of command prompt on foreground"""
#window = GetForegroundWindow()
#return GetWindowThreadProcessId(window)[0]
return GetWindowThreadProcessId()
def activate_venv(self, shell, venv_location):
"""activates venv of the active command prompt"""
print shell.AppActivate(self.get_pid())
def run_py_script(self, shell,command):
"""runs the py script"""
shell.SendKeys(command)
def open_cmd(self, shell):
""" opens cmd """
shell.run("cmd.exe")
time.sleep(1)
if __name__ == "__main__":
shell = win32com.client.Dispatch("WScript.Shell")
run_venv = ActivateVenv()
command1 = "adb devices>output.txt {ENTER}"
command3 = "exit {ENTER}"
#command2 = "fastboot devices {ENTER}"
#for i in{0,2}:
run_venv.open_cmd(shell)
#EnumWindows(run_venv.set_cmd_to_foreground, None)
run_venv.run_py_script(shell,command1)
run_venv.run_py_script(shell, command3)
#run_venv.run_py_script(shell,command2)
devices_list=[]
f = open('output.txt', 'r')
x = f.readlines()
for i in x:
r1 = re.findall(r"^\w+",i)
if r1:
devices_list.append(r1)
f.close()
print devices_list[2]
#new_device = str(devices_list[2])
#print(new_device.strip("',[,',]"))
command2 = "adb -s "+ str(devices_list[2]).strip("',[,',]") + " reboot bootloader {ENTER}"
run_venv.open_cmd(shell)
#run_venv.run_py_script(shell, command2)