@@ -239,6 +239,7 @@ class AdbConnection(object):
239
239
active_connections = defaultdict (int )
240
240
default_timeout = 10
241
241
ls_command = 'ls'
242
+ su_cmd = 'su -c {}'
242
243
243
244
@property
244
245
def name (self ):
@@ -264,6 +265,7 @@ def _setup_ls(self):
264
265
self .ls_command = 'ls'
265
266
logger .debug ("ls command is set to {}" .format (self .ls_command ))
266
267
268
+
267
269
# pylint: disable=unused-argument
268
270
def __init__ (self , device = None , timeout = None , platform = None , adb_server = None ):
269
271
self .timeout = timeout if timeout is not None else self .default_timeout
@@ -274,6 +276,7 @@ def __init__(self, device=None, timeout=None, platform=None, adb_server=None):
274
276
adb_connect (self .device )
275
277
AdbConnection .active_connections [self .device ] += 1
276
278
self ._setup_ls ()
279
+ self ._setup_su ()
277
280
278
281
def push (self , source , dest , timeout = None ):
279
282
if timeout is None :
@@ -303,7 +306,7 @@ def execute(self, command, timeout=None, check_exit_code=False,
303
306
as_root = False , strip_colors = True , will_succeed = False ):
304
307
try :
305
308
return adb_shell (self .device , command , timeout , check_exit_code ,
306
- as_root , adb_server = self .adb_server )
309
+ as_root , adb_server = self .adb_server , su_cmd = self . su_cmd )
307
310
except TargetStableError as e :
308
311
if will_succeed :
309
312
raise TargetTransientError (e )
@@ -326,6 +329,18 @@ def cancel_running_command(self):
326
329
pass
327
330
328
331
332
+ def _setup_su (self ):
333
+ try :
334
+ # Try the new style of invoking `su`
335
+ self .execute ('ls' , timeout = self .timeout , as_root = True ,
336
+ check_exit_code = True )
337
+ # If failure assume either old style or unrooted. Here we will assume
338
+ # old style and root status will be verified later.
339
+ except (TargetStableError , TargetTransientError , TimeoutError ):
340
+ self .su_cmd = 'echo {} | su'
341
+ logger .debug ("su command is set to {}" .format (quote (self .su_cmd )))
342
+
343
+
329
344
def fastboot_command (command , timeout = None , device = None ):
330
345
_check_env ()
331
346
target = '-s {}' .format (quote (device )) if device else ''
@@ -423,15 +438,15 @@ def _ping(device):
423
438
424
439
# pylint: disable=too-many-locals
425
440
def adb_shell (device , command , timeout = None , check_exit_code = False ,
426
- as_root = False , adb_server = None ): # NOQA
441
+ as_root = False , adb_server = None , su_cmd = 'su -c {}' ): # NOQA
427
442
_check_env ()
428
443
parts = ['adb' ]
429
444
if adb_server is not None :
430
445
parts += ['-H' , adb_server ]
431
446
if device is not None :
432
447
parts += ['-s' , device ]
433
448
parts += ['shell' ,
434
- command if not as_root else 'su -c {}' .format (quote (command ))]
449
+ command if not as_root else su_cmd .format (quote (command ))]
435
450
436
451
logger .debug (' ' .join (quote (part ) for part in parts ))
437
452
# On older combinations of ADB/Android versions, the adb host command always
0 commit comments