@@ -1348,7 +1348,7 @@ async def read_tree_tar_flat(self, path, depth=1, check_exit_code=True,
1348
1348
# if it is a file and not a folder
1349
1349
if content_f :
1350
1350
content = content_f .read ()
1351
- if decode_unicode :
1351
+ if decode_unicode in ( True , None ) :
1352
1352
try :
1353
1353
content = content .decode ('utf-8' ).strip ()
1354
1354
if strip_null_chars :
@@ -1362,27 +1362,37 @@ async def read_tree_tar_flat(self, path, depth=1, check_exit_code=True,
1362
1362
return result
1363
1363
1364
1364
@asyn .asyncf
1365
- async def read_tree_values_flat (self , path , depth = 1 , check_exit_code = True ):
1365
+ async def read_tree_values_flat (self , path , depth = 1 , check_exit_code = True , decode = None ):
1366
1366
self .async_manager .track_access (
1367
1367
asyn .PathAccess (namespace = 'target' , path = path , mode = 'r' )
1368
1368
)
1369
1369
command = 'read_tree_values {} {}' .format (quote (path ), depth )
1370
1370
output = await self ._execute_util .asyn (command , as_root = self .is_rooted ,
1371
- check_exit_code = check_exit_code )
1372
-
1371
+ check_exit_code = check_exit_code , decode = False )
1373
1372
accumulator = defaultdict (list )
1374
- for entry in output .strip ().split ( ' \n ' ):
1375
- if ':' not in entry :
1373
+ for entry in output .strip ().splitlines ( ):
1374
+ if b ':' not in entry :
1376
1375
continue
1377
- path , value = entry .strip ().split (':' , 1 )
1376
+ path , value = entry .strip ().split (b ':' , 1 )
1378
1377
accumulator [path ].append (value )
1379
1378
1380
- result = {k : '\n ' .join (v ).strip () for k , v in accumulator .items ()}
1379
+ if decode is None :
1380
+ def do_decode (b ):
1381
+ try :
1382
+ return b .decode ()
1383
+ except UnicodeDecodeError :
1384
+ return b
1385
+ elif decode :
1386
+ do_decode = lambda b : b .decode ()
1387
+ else :
1388
+ do_decode = lambda b : b
1389
+
1390
+ result = {k .decode (): do_decode (b'\n ' .join (v ).strip ()) for k , v in accumulator .items ()}
1381
1391
return result
1382
1392
1383
1393
@asyn .asyncf
1384
1394
async def read_tree_values (self , path , depth = 1 , dictcls = dict ,
1385
- check_exit_code = True , tar = False , decode_unicode = True ,
1395
+ check_exit_code = True , tar = False , decode_unicode = None ,
1386
1396
strip_null_chars = True ):
1387
1397
"""
1388
1398
Reads the content of all files under a given tree
@@ -1400,7 +1410,7 @@ async def read_tree_values(self, path, depth=1, dictcls=dict,
1400
1410
:returns: a tree-like dict with the content of files as leafs
1401
1411
"""
1402
1412
if not tar :
1403
- value_map = await self .read_tree_values_flat .asyn (path , depth , check_exit_code )
1413
+ value_map = await self .read_tree_values_flat .asyn (path , depth , check_exit_code , decode = decode_unicode )
1404
1414
else :
1405
1415
value_map = await self .read_tree_tar_flat .asyn (path , depth , check_exit_code ,
1406
1416
decode_unicode ,
@@ -1436,14 +1446,15 @@ async def _setup_shutils(self):
1436
1446
1437
1447
@asyn .asyncf
1438
1448
@call_conn
1439
- async def _execute_util (self , command , timeout = None , check_exit_code = True , as_root = False ):
1449
+ async def _execute_util (self , command , timeout = None , check_exit_code = True , as_root = False , decode = True ):
1440
1450
command = '{} sh {} {}' .format (quote (self .busybox ), quote (self .shutils ), command )
1441
- return await self .execute .asyn (
1442
- command ,
1451
+ stdout , stderr = await self .execute_raw .asyn (
1452
+ command = command ,
1443
1453
timeout = timeout ,
1444
1454
check_exit_code = check_exit_code ,
1445
- as_root = as_root
1455
+ as_root = as_root ,
1446
1456
)
1457
+ return stdout .decode () if decode else stdout
1447
1458
1448
1459
async def _extract_archive (self , path , cmd , dest = None ):
1449
1460
cmd = '{} ' + cmd # busybox
0 commit comments