11import functools
22import subprocess
3- import typing
43
54try :
65 # This fails when the code is executed directly and not as a part of python package installation,
1110 from output_helper import print_error , print_error_and_exit , print_verbose
1211
1312
14- _adb_prefix = ' adb'
13+ _adb_prefix = " adb"
1514_IGNORED_LINES = [
16- ' WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.'
15+ " WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix." ,
1716]
1817
1918# Below version 24, if an adb shell command fails, then it still has an incorrect exit code of 0.
@@ -24,24 +23,24 @@ def get_adb_prefix() -> str:
2423 return _adb_prefix
2524
2625
27- def set_adb_prefix (adb_prefix ):
26+ def set_adb_prefix (adb_prefix ) -> None :
2827 # pylint: disable=global-statement
2928 global _adb_prefix
3029 _adb_prefix = adb_prefix
3130
3231
33- def get_adb_shell_property (property_name , device_serial = None ) -> typing . Optional [ str ] :
34- _ , stdout , _ = execute_adb_shell_command2 (' getprop %s' % property_name , device_serial = device_serial )
32+ def get_adb_shell_property (property_name , device_serial = None ) -> str | None :
33+ _ , stdout , _ = execute_adb_shell_command2 (f" getprop { property_name } " , device_serial = device_serial )
3534 return stdout
3635
3736
3837def execute_adb_shell_command2 (adb_cmd , piped_into_cmd = None , ignore_stderr = False , device_serial = None ):
39- return execute_adb_command2 (' shell %s' % adb_cmd , piped_into_cmd = piped_into_cmd ,
38+ return execute_adb_command2 (f" shell { adb_cmd } " , piped_into_cmd = piped_into_cmd ,
4039 ignore_stderr = ignore_stderr , device_serial = device_serial )
4140
4241
4342def execute_adb_command2 (adb_cmd , piped_into_cmd = None , ignore_stderr = False , device_serial = None ) -> \
44- typing . Tuple [int , typing . Optional [ str ] , str ]:
43+ tuple [int , str | None , str ]:
4544 """
4645 :param adb_cmd: command to run inside the adb shell (so, don't prefix it with "adb")
4746 :param piped_into_cmd: command to pipe the output of this command into
@@ -51,21 +50,21 @@ def execute_adb_command2(adb_cmd, piped_into_cmd=None, ignore_stderr=False, devi
5150 """
5251 adb_prefix = _adb_prefix
5352 if device_serial :
54- adb_prefix = '%s -s %s' % ( adb_prefix , device_serial )
53+ adb_prefix = f" { adb_prefix } -s { device_serial } "
5554
56- final_cmd = '%s %s' % ( adb_prefix , adb_cmd )
55+ final_cmd = f" { adb_prefix } { adb_cmd } "
5756 if piped_into_cmd :
58- final_cmd = '%s | %s' % ( final_cmd , piped_into_cmd )
57+ final_cmd = f" { final_cmd } | { piped_into_cmd } "
5958
60- print_verbose (" Executing \" %s \" " % final_cmd )
59+ print_verbose (f' Executing " { final_cmd } "' )
6160 with subprocess .Popen (final_cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE ) as ps1 :
6261 stdout_data , stderr_data = ps1 .communicate ()
6362 return_code = ps1 .returncode
6463 try :
65- stdout_data = stdout_data .decode (' utf-8' )
64+ stdout_data = stdout_data .decode (" utf-8" )
6665 except UnicodeDecodeError :
67- print_error (' Unable to decode data as UTF-8, defaulting to printing the binary data' )
68- stderr_data = stderr_data .decode (' utf-8' )
66+ print_error (" Unable to decode data as UTF-8, defaulting to printing the binary data" )
67+ stderr_data = stderr_data .decode (" utf-8" )
6968
7069 _check_for_adb_not_found_error (stderr_data )
7170 _check_for_more_than_one_device_error (stderr_data )
@@ -78,13 +77,13 @@ def execute_adb_command2(adb_cmd, piped_into_cmd=None, ignore_stderr=False, devi
7877
7978 # stdout_data is not None
8079 if isinstance (stdout_data , bytes ):
81- print_verbose (" Result is \" %s \" " % stdout_data )
80+ print_verbose (f' Result is " { stdout_data } "' )
8281 return return_code , stdout_data , stderr_data
8382 # str for Python 3, this used to be unicode type for python 2
84- elif isinstance (stdout_data , str ):
85- output = ''
83+ if isinstance (stdout_data , str ):
84+ output = ""
8685 first_line = True
87- for line in stdout_data .split (' \n ' ):
86+ for line in stdout_data .split (" \n " ):
8887 line = line .strip ()
8988 if not line :
9089 continue
@@ -94,29 +93,29 @@ def execute_adb_command2(adb_cmd, piped_into_cmd=None, ignore_stderr=False, devi
9493 output += line
9594 first_line = False
9695 else :
97- output += ' \n ' + line
98- print_verbose (" Result is \" %s \" " % output )
96+ output += " \n " + line
97+ print_verbose (f' Result is " { output } "' )
9998 return return_code , output , stderr_data
100- else :
101- print_error_and_exit ( 'stdout_data is weird type: %s' % type ( stdout_data ))
99+ print_error_and_exit ( f"stdout_data is weird type: { type ( stdout_data ) } " )
100+ return None
102101
103102
104103def execute_adb_shell_command (adb_cmd , piped_into_cmd = None , ignore_stderr = False , device_serial = None ) -> str :
105104 _ , stdout , _ = execute_adb_command2 (
106- "shell %s" % adb_cmd , piped_into_cmd , ignore_stderr , device_serial = device_serial )
105+ f "shell { adb_cmd } " , piped_into_cmd , ignore_stderr , device_serial = device_serial )
107106 return stdout
108107
109108
110109def execute_file_related_adb_shell_command (
111- adb_shell_cmd : str , file_path : str , piped_into_cmd : typing . Optional [ str ] = None ,
112- ignore_stderr : bool = False , device_serial : typing . Optional [ str ] = None ) -> str :
110+ adb_shell_cmd : str , file_path : str , piped_into_cmd : str | None = None ,
111+ ignore_stderr : bool = False , device_serial : str | None = None ) -> str :
113112 file_not_found_message = "No such file or directory"
114113 is_a_directory_message = "Is a directory" # Error when someone tries to delete a dir without "-r"
115114
116115 adb_cmds_prefix = []
117116 run_as_package = get_package (file_path )
118117 if run_as_package :
119- adb_cmds_prefix .append ("shell run-as %s" % run_as_package )
118+ adb_cmds_prefix .append (f "shell run-as { run_as_package } " )
120119 if root_required_to_access_file (file_path ):
121120 adb_cmds_prefix .append ("shell su root" )
122121 # As a backup, still try with a plain-old access, if run-as is not possible and root is not available.
@@ -127,15 +126,15 @@ def execute_file_related_adb_shell_command(
127126 for adb_cmd_prefix in adb_cmds_prefix :
128127 print_verbose ('Attempt %d/%d: "%s"' % (attempt_count , len (adb_cmds_prefix ), adb_cmd_prefix ))
129128 attempt_count += 1
130- adb_cmd = "%s %s" % ( adb_cmd_prefix , adb_shell_cmd )
129+ adb_cmd = f" { adb_cmd_prefix } { adb_shell_cmd } "
131130 return_code , stdout , stderr = execute_adb_command2 (adb_cmd , piped_into_cmd , ignore_stderr ,
132131 device_serial = device_serial )
133132
134133 if stderr .find (file_not_found_message ) >= 0 :
135- print_error ("File not found: %s" % file_path )
134+ print_error (f "File not found: { file_path } " )
136135 return stderr
137136 if stderr .find (is_a_directory_message ) >= 0 :
138- print_error ("%s is a directory" % file_path )
137+ print_error (f" { file_path } is a directory" )
139138 return stderr
140139
141140 api_version = get_device_android_api_version ()
@@ -148,22 +147,20 @@ def execute_file_related_adb_shell_command(
148147# Gets the package name given a file path.
149148# Eg. if the file is in /data/data/com.foo/.../file1 then package is com.foo
150149# Or if the file is in /data/user/0/com.foo/.../file1 then package is com.foo
151- def get_package (file_path ) -> typing . Optional [ str ] :
150+ def get_package (file_path ) -> str | None :
152151 if not file_path :
153152 return None
154153
155- if file_path .startswith (' /data/data/' ):
156- items = file_path .split ('/' )
154+ if file_path .startswith (" /data/data/" ):
155+ items = file_path .split ("/" )
157156 if len (items ) >= 4 :
158- run_as_package = items [3 ]
159- return run_as_package
157+ return items [3 ]
160158
161159 # Handles the new multi-user mode
162- if file_path .startswith (' /data/user/' ):
163- items = file_path .split ('/' )
160+ if file_path .startswith (" /data/user/" ):
161+ items = file_path .split ("/" )
164162 if len (items ) >= 5 :
165- run_as_package = items [4 ]
166- return run_as_package
163+ return items [4 ]
167164 return None
168165
169166
@@ -177,69 +174,63 @@ def get_device_android_api_version(device_serial=None) -> int:
177174
178175
179176def root_required_to_access_file (remote_file_path ) -> bool :
180- if not remote_file_path :
181- return False
182- elif remote_file_path .startswith ("/data/local/tmp" ):
183- return False
184- elif remote_file_path .startswith ("/sdcard" ):
185- return False
186- return True
177+ return not (not remote_file_path or remote_file_path .startswith (("/data/local/tmp" , "/sdcard" )))
187178
188179
189- def _check_for_adb_not_found_error (stderr_data ):
180+ def _check_for_adb_not_found_error (stderr_data ) -> None :
190181 if not stderr_data :
191182 return
192183 stderr_data = stderr_data .strip ()
193- if stderr_data .endswith ('%s : command not found' % _adb_prefix ):
194- message = ' ADB (Android debug bridge) command not found.\n '
195- message += ' Install ADB via https://developer.android.com/studio/releases/platform-tools.html'
184+ if stderr_data .endswith (f" { _adb_prefix } : command not found" ):
185+ message = " ADB (Android debug bridge) command not found.\n "
186+ message += " Install ADB via https://developer.android.com/studio/releases/platform-tools.html"
196187 print_error_and_exit (message )
197188
198189
199- def _check_for_more_than_one_device_error (stderr_data ):
190+ def _check_for_more_than_one_device_error (stderr_data ) -> None :
200191 if not stderr_data :
201192 return
202- for line in stderr_data .split (' \n ' ):
193+ for line in stderr_data .split (" \n " ):
203194 line = line .strip ()
204195 if line :
205196 print_verbose (line )
206- if line .find (' error: more than one' ) != - 1 :
207- message = ''
208- message += ' More than one device/emulator are connected.\n '
209- message += ' Please select a device by providing the serial ID (-s parameter).\n '
210- message += 'You can list all connected devices/emulators via \ " devices\ " subcommand.'
197+ if line .find (" error: more than one" ) != - 1 :
198+ message = ""
199+ message += " More than one device/emulator are connected.\n "
200+ message += " Please select a device by providing the serial ID (-s parameter).\n "
201+ message += 'You can list all connected devices/emulators via "devices" subcommand.'
211202 print_error_and_exit (message )
212203
213204
214- def _check_for_device_not_found_error (stderr_data ):
205+ def _check_for_device_not_found_error (stderr_data ) -> None :
215206 if not stderr_data :
216207 return
217- for line in stderr_data .split (' \n ' ):
208+ for line in stderr_data .split (" \n " ):
218209 line = line .strip ()
219210 if line :
220211 print_verbose (line )
221- if line .find (' error: device' ) > - 1 and line .find (' not found' ) > - 1 :
212+ if line .find (" error: device" ) > - 1 and line .find (" not found" ) > - 1 :
222213 print_error_and_exit (line )
223214
224215
225216def toggle_screen ():
226217 return execute_adb_shell_command2 ("input keyevent KEYCODE_POWER" )
227218
228219
229- def set_device_id (device_id ):
220+ def set_device_id (device_id ) -> None :
230221 """
231222 Make :param device_id: as main device to use
232223 Primary use-case: scripting
233224 Command line equivalent: "-s :param device_id:"
234225 """
235226 old_adb_prefix = get_adb_prefix ()
236- if '-s' in old_adb_prefix :
237- old_device = old_adb_prefix .split (' -s ' )[1 ]
238- if ' ' in old_device :
227+ if "-s" in old_adb_prefix :
228+ old_device = old_adb_prefix .split (" -s " )[1 ]
229+ if " " in old_device :
239230 # Case: device ID is not the last argument
240- old_device = old_adb_prefix .split ('-s' )[1 ].split (' ' )[0 ]
241- print_verbose (' Switching from %s to %s' % ( old_device , device_id ) )
231+ old_device = old_adb_prefix .split ("-s" )[1 ].split (" " )[0 ]
232+ print_verbose (f" Switching from { old_device } to { device_id } " )
242233 old_adb_prefix .replace (old_device , device_id )
243234
244- print_verbose (' Setting device ID to %s' % device_id )
245- set_adb_prefix ("%s -s %s" % ( old_adb_prefix , device_id ) )
235+ print_verbose (f" Setting device ID to { device_id } " )
236+ set_adb_prefix (f" { old_adb_prefix } -s { device_id } " )
0 commit comments