From 4431932e0db66028dcd2ec84207f21d6a602cff3 Mon Sep 17 00:00:00 2001
From: Douglas Raillard <douglas.raillard@arm.com>
Date: Wed, 20 Nov 2024 16:56:21 +0000
Subject: [PATCH] target: Reduce the number of commands involved in push/pull

* Combine cp and chmod for pull
* Make both push and pull use concurrent async code
---
 devlib/target.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/devlib/target.py b/devlib/target.py
index 69a55d6..f1005d3 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -819,10 +819,11 @@ class Target(object):
 
         if as_root:
             for sources, dest in mapping.items():
-                for source in sources:
+                async def f(source):
                     async with self._xfer_cache_path(source) as device_tempfile:
                         do_push([source], device_tempfile)
                         await self.execute.asyn("mv -f -- {} {}".format(quote(device_tempfile), quote(dest)), as_root=True)
+                await self.async_manager.map_concurrently(f, sources)
         else:
             for sources, dest in mapping.items():
                 do_push(sources, dest)
@@ -897,11 +898,13 @@ class Target(object):
 
         if via_temp:
             for sources, dest in mapping.items():
-                for source in sources:
+                async def f(source):
                     async with self._xfer_cache_path(source) as device_tempfile:
-                        await self.execute.asyn(f"{quote(self.busybox)} cp -rL -- {quote(source)} {quote(device_tempfile)}", as_root=as_root)
-                        await self.execute.asyn(f"{quote(self.busybox)} chmod 0644 -- {quote(device_tempfile)}", as_root=as_root)
+                        cp_cmd = f"{quote(self.busybox)} cp -rL -- {quote(source)} {quote(device_tempfile)}"
+                        chmod_cmd = f"{quote(self.busybox)} chmod 0644 -- {quote(device_tempfile)}"
+                        await self.execute.asyn(f"{cp_cmd} && {chmod_cmd}", as_root=as_root)
                         do_pull([device_tempfile], dest)
+                await self.async_manager.map_concurrently(f, sources)
         else:
             for sources, dest in mapping.items():
                 do_pull(sources, dest)