11# -*- coding: utf-8 -*-
2-
2+ import re
33import socket
44import json
55import time
66import os
7+ import glob
78import typing
89import subprocess
9- import hashlib
1010from datetime import datetime
1111from functools import cached_property
1212
1818
1919UITEST_SERVICE_PORT = 8012
2020SOCKET_TIMEOUT = 20
21+ ASSETS_PATH = os .path .join (os .path .dirname (__file__ ), "assets" )
22+ AGENT_CLEAR_PATH = ["app" , "commons-" , "agent" , "libagent_antry" ]
2123
2224
2325class HmClient :
@@ -169,39 +171,53 @@ def _create_hdriver(self) -> DriverData:
169171 def _init_so_resource (self ):
170172 "Initialize the agent.so resource on the device."
171173
172- logger .debug ("init the agent.so resource on the device." )
173-
174- def __get_so_local_path () -> str :
175- current_path = os .path .realpath (__file__ )
176- return os .path .join (os .path .dirname (current_path ), "assets" , "uitest_agent_v1.1.0.so" )
177-
178- def __check_device_so_file_exists () -> bool :
179- """Check if the agent.so file exists on the device."""
180- command = "[ -f /data/local/tmp/agent.so ] && echo 'so exists' || echo 'so not exists'"
181- result = self .hdc .shell (command ).output .strip ()
182- return "so exists" in result
183-
184- def __get_remote_md5sum () -> str :
185- """Get the MD5 checksum of the file on the device."""
186- command = "md5sum /data/local/tmp/agent.so"
187- data = self .hdc .shell (command ).output .strip ()
188- return data .split ()[0 ]
189-
190- def __get_local_md5sum (f : str ) -> str :
191- """Calculate the MD5 checksum of a local file."""
192- hash_md5 = hashlib .md5 ()
193- with open (f , "rb" ) as f :
194- for chunk in iter (lambda : f .read (4096 ), b"" ):
195- hash_md5 .update (chunk )
196- return hash_md5 .hexdigest ()
197-
198- local_path = __get_so_local_path ()
199- remote_path = "/data/local/tmp/agent.so"
200-
201- if __check_device_so_file_exists () and __get_local_md5sum (local_path ) == __get_remote_md5sum ():
202- return
203- self .hdc .send_file (local_path , remote_path )
204- self .hdc .shell (f"chmod +x { remote_path } " )
174+ file_postfix = ".so"
175+ device_agent_path = "/data/local/tmp/agent.so"
176+ arch_info = self .hdc .shell ("file /system/bin/uitest" ).output .strip ()
177+ if "x86_64" in arch_info :
178+ file_postfix = ".x86_64_so"
179+ local_path = ""
180+ local_ver = "0.0.0"
181+ for agent_file in glob .glob (os .path .join (ASSETS_PATH , "uitest_agent*so" )):
182+ file_name = os .path .split (agent_file )[1 ]
183+ if not agent_file .endswith (file_postfix ):
184+ continue
185+ matcher = re .search (r'\d{1,3}[.]\d{1,3}[.]\d{1,3}' , file_name )
186+ if not matcher :
187+ continue
188+ ver = matcher .group ()[0 ]
189+ if ver .split ('.' ) > local_ver .split ('.' ):
190+ local_ver , local_path = ver , agent_file
191+ device_ver_info = self .hdc .shell (f"cat { device_agent_path } | grep -a UITEST_AGENT_LIBRARY" ).output .strip ()
192+ matcher = re .search (r'\d{1,3}[.]\d{1,3}[.]\d{1,3}' , device_ver_info )
193+ device_ver = matcher .group (0 ) if matcher else "0.0.0"
194+ logger .debug (f"local agent version { local_ver } , device agent version { device_ver } " )
195+ if device_ver .split ('.' ) < local_ver .split ('.' ):
196+ logger .debug (f"start update agent, path is { local_path } " )
197+ self ._kill_uitest_service ()
198+ for file in AGENT_CLEAR_PATH :
199+ self .hdc .shell (f"rm /data/local/tmp/{ file } *" )
200+ self .hdc .send_file (local_path , device_agent_path )
201+ self .hdc .shell (f"chmod +x { device_agent_path } " )
202+ logger .debug ("Update agent finish." )
203+ else :
204+ logger .debug ("Device agent is up to date!" )
205+
206+ def get_devicetest_proc_pid (self ):
207+ proc_pids = []
208+ result = self .hdc .shell ("ps -ef" ).output .strip ()
209+ lines = result .splitlines ()
210+ filtered_lines = [line for line in lines if 'uitest' in line and 'singleness' in line ]
211+ for line in filtered_lines :
212+ if 'uitest start-daemon singleness' not in line :
213+ continue
214+ proc_pids .append (line .split ()[1 ])
215+ return proc_pids
216+
217+ def _kill_uitest_service (self ):
218+ for pid in self .get_devicetest_proc_pid ():
219+ self .hdc .shell (f"kill -9 { pid } " )
220+ logger .debug (f"Killed uitest process with PID { pid } " )
205221
206222 def _restart_uitest_service (self ):
207223 """
@@ -213,17 +229,7 @@ def _restart_uitest_service(self):
213229 shell 44416 1 2 11:03:42 ? 00:00:01 uitest start-daemon com.hmtest.uitest@4x9@1"
214230 """
215231 try :
216- result = self .hdc .shell ("ps -ef" ).output .strip ()
217- lines = result .splitlines ()
218- filtered_lines = [line for line in lines if 'uitest' in line and 'singleness' in line ]
219-
220- for line in filtered_lines :
221- if 'uitest start-daemon singleness' in line :
222- parts = line .split ()
223- pid = parts [1 ]
224- self .hdc .shell (f"kill -9 { pid } " )
225- logger .debug (f"Killed uitest process with PID { pid } " )
226-
232+ self ._kill_uitest_service ()
227233 self .hdc .shell ("uitest start-daemon singleness" )
228234 time .sleep (.5 )
229235
0 commit comments