|
| 1 | +try: |
| 2 | + from asyncio import get_running_loop |
| 3 | +except ImportError: # pragma: no cover |
| 4 | + from asyncio import get_event_loop as get_running_loop # Python 3.6 compatibility |
| 5 | + |
| 6 | +import struct |
| 7 | +import os |
| 8 | + |
| 9 | +from ppadb.protocol import Protocol |
| 10 | +from ppadb.sync.stats import S_IFREG |
| 11 | +from ppadb.utils.logger import AdbLogging |
| 12 | + |
| 13 | +import aiofiles |
| 14 | + |
| 15 | +logger = AdbLogging.get_logger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +def _get_src_info(src): |
| 19 | + exists = os.path.exists(src) |
| 20 | + if not exists: |
| 21 | + return exists, None, None |
| 22 | + |
| 23 | + timestamp = os.stat(src).st_mtime |
| 24 | + total_size = os.path.getsize(src) |
| 25 | + |
| 26 | + return exists, timestamp, total_size |
| 27 | + |
| 28 | + |
| 29 | +class SyncAsync: |
| 30 | + DATA_MAX_LENGTH = 65536 |
| 31 | + |
| 32 | + def __init__(self, connection): |
| 33 | + self.connection = connection |
| 34 | + |
| 35 | + async def push(self, src, dest, mode, progress=None): |
| 36 | + """Push from local path |src| to |dest| on device. |
| 37 | + :param progress: callback, called with (filename, total_size, sent_size) |
| 38 | + """ |
| 39 | + exists, timestamp, total_size = await get_running_loop().run_in_executor(None, _get_src_info, src) |
| 40 | + |
| 41 | + if not exists: |
| 42 | + raise FileNotFoundError("Can't find the source file {}".format(src)) |
| 43 | + |
| 44 | + sent_size = 0 |
| 45 | + |
| 46 | + # SEND |
| 47 | + mode = mode | S_IFREG |
| 48 | + args = "{dest},{mode}".format(dest=dest, mode=mode) |
| 49 | + await self._send_str(Protocol.SEND, args) |
| 50 | + |
| 51 | + # DATA |
| 52 | + async with aiofiles.open(src, 'rb') as stream: |
| 53 | + while True: |
| 54 | + chunk = await stream.read(self.DATA_MAX_LENGTH) |
| 55 | + if not chunk: |
| 56 | + break |
| 57 | + |
| 58 | + sent_size += len(chunk) |
| 59 | + await self._send_length(Protocol.DATA, len(chunk)) |
| 60 | + await self.connection.write(chunk) |
| 61 | + |
| 62 | + if progress is not None: |
| 63 | + progress(src, total_size, sent_size) |
| 64 | + |
| 65 | + # DONE |
| 66 | + await self._send_length(Protocol.DONE, timestamp) |
| 67 | + await self.connection._check_status() |
| 68 | + |
| 69 | + async def pull(self, src, dest): |
| 70 | + # RECV |
| 71 | + await self._send_str(Protocol.RECV, src) |
| 72 | + |
| 73 | + # DATA |
| 74 | + async with aiofiles.open(dest, 'wb') as stream: |
| 75 | + while True: |
| 76 | + flag = (await self.connection.read(4)).decode('utf-8') |
| 77 | + |
| 78 | + if flag == Protocol.DATA: |
| 79 | + data = await self._read_data() |
| 80 | + await stream.write(data) |
| 81 | + continue |
| 82 | + |
| 83 | + if flag == Protocol.DONE: |
| 84 | + await self.connection.read(4) |
| 85 | + return |
| 86 | + |
| 87 | + if flag == Protocol.FAIL: |
| 88 | + return (await self._read_data()).decode('utf-8') |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def _integer(little_endian): |
| 92 | + return struct.unpack("<I", little_endian) |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def _little_endian(n): |
| 96 | + return struct.pack('<I', n) |
| 97 | + |
| 98 | + async def _read_data(self): |
| 99 | + length = self._integer(await self.connection.read(4))[0] |
| 100 | + data = bytearray() |
| 101 | + while len(data) < length: |
| 102 | + data += await self.connection.read(length - len(data)) |
| 103 | + return data |
| 104 | + |
| 105 | + async def _send_length(self, cmd, length): |
| 106 | + le_len = self._little_endian(length) |
| 107 | + data = cmd.encode() + le_len |
| 108 | + |
| 109 | + logger.debug("Send length: {}".format(data)) |
| 110 | + await self.connection.write(data) |
| 111 | + |
| 112 | + async def _send_str(self, cmd, args): |
| 113 | + """ |
| 114 | + Format: |
| 115 | + {Command}{args length(little endian)}{str} |
| 116 | + Length: |
| 117 | + {4}{4}{str length} |
| 118 | + """ |
| 119 | + logger.debug("{} {}".format(cmd, args)) |
| 120 | + args = args.encode('utf-8') |
| 121 | + |
| 122 | + le_args_len = self._little_endian(len(args)) |
| 123 | + data = cmd.encode() + le_args_len + args |
| 124 | + logger.debug("Send string: {}".format(data)) |
| 125 | + await self.connection.write(data) |
0 commit comments