Skip to content

Commit 0684e3a

Browse files
committed
Fix recursive directory push for DeviceAsync class
1 parent b136202 commit 0684e3a

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

ppadb/device.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
from pipes import quote as cmd_quote
3131

3232

33+
def _get_src_info(src):
34+
"""Get information about the contents of a folder; used in :meth:`Device.push`."""
35+
exists = os.path.exists(src)
36+
isfile = os.path.isfile(src)
37+
isdir = os.path.isdir(src)
38+
basename = os.path.basename(src)
39+
walk = None if not isdir else list(os.walk(src))
40+
41+
return exists, isfile, isdir, basename, walk
42+
43+
3344
class Device(Transport, Serial, Input, Utils, WM, Traffic, CPUStat, BatteryStats):
3445
INSTALL_RESULT_PATTERN = "(Success|Failure|Error)\s?(.*)"
3546
UNINSTALL_RESULT_PATTERN = "(Success|Failure.*|.*Unknown package:.*)"
@@ -55,24 +66,22 @@ def _push(self, src, dest, mode, progress):
5566
sync.push(src, dest, mode, progress)
5667

5768
def push(self, src, dest, mode=0o644, progress=None):
58-
if not os.path.exists(src):
69+
exists, isfile, isdir, basename, walk = _get_src_info(src)
70+
if not exists:
5971
raise FileNotFoundError("Cannot find {}".format(src))
60-
elif os.path.isfile(src):
61-
self._push(src, dest, mode, progress)
62-
elif os.path.isdir(src):
6372

64-
basename = os.path.basename(src)
73+
if isfile:
74+
self._push(src, dest, mode, progress)
6575

66-
for root, dirs, files in os.walk(src):
67-
subdir = root.replace(src, "")
68-
if subdir.startswith("/"):
69-
subdir = subdir[1:]
70-
root_dir_path = os.path.join(basename, subdir)
76+
elif isdir:
77+
for root, dirs, files in walk:
78+
subdir = os.path.relpath(root, src)
79+
root_dir_path = os.path.normpath(os.path.join(basename, subdir))
7180

72-
self.shell("mkdir -p {}/{}".format(dest, root_dir_path))
81+
self.shell('mkdir -p "{}"'.format(os.path.normpath(os.path.join(dest, root_dir_path))))
7382

7483
for item in files:
75-
self._push(os.path.join(root, item), os.path.join(dest, root_dir_path, item), mode, progress)
84+
self._push(os.path.normpath(os.path.join(root, item)), os.path.normpath(os.path.join(dest, root_dir_path, item)), mode, progress)
7685

7786
def pull(self, src, dest):
7887
sync_conn = self.sync()

ppadb/device_async.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,10 @@
77
import os
88

99
from ppadb.command.transport_async import TransportAsync
10+
from ppadb.device import _get_src_info
1011
from ppadb.sync_async import SyncAsync
1112

1213

13-
def _get_src_info(src):
14-
exists = os.path.exists(src)
15-
isfile = os.path.isfile(src)
16-
isdir = os.path.isdir(src)
17-
basename = os.path.basename(src)
18-
walk = None if not isdir else list(os.walk(src))
19-
20-
return exists, isfile, isdir, basename, walk
21-
22-
2314
class DeviceAsync(TransportAsync):
2415
INSTALL_RESULT_PATTERN = "(Success|Failure|Error)\s?(.*)"
2516
UNINSTALL_RESULT_PATTERN = "(Success|Failure.*|.*Unknown package:.*)"
@@ -54,12 +45,13 @@ async def push(self, src, dest, mode=0o644, progress=None):
5445

5546
elif isdir:
5647
for root, dirs, files in walk:
57-
root_dir_path = os.path.join(basename, root.replace(src, ""))
48+
subdir = os.path.relpath(root, src)
49+
root_dir_path = os.path.normpath(os.path.join(basename, subdir))
5850

59-
await self.shell("mkdir -p {}/{}".format(dest, root_dir_path))
51+
await self.shell('mkdir -p "{}"'.format(os.path.normpath(os.path.join(dest, root_dir_path))))
6052

6153
for item in files:
62-
await self._push(os.path.join(root, item), os.path.join(dest, root_dir_path, item), mode, progress)
54+
await self._push(os.path.normpath(os.path.join(root, item)), os.path.normpath(os.path.join(dest, root_dir_path, item)), mode, progress)
6355

6456
async def pull(self, src, dest):
6557
sync_conn = await self.sync()

0 commit comments

Comments
 (0)