Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous package install and uninstall #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion ppadb/device_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
except ImportError: # pragma: no cover
from asyncio import get_event_loop as get_running_loop # Python 3.6 compatibility

import re
import os
import re

from ppadb import InstallError
from ppadb.command.transport_async import TransportAsync
from ppadb.sync_async import SyncAsync
from ppadb.utils.logger import AdbLogging

try:
from shlex import quote as cmd_quote
except ImportError:
from pipes import quote as cmd_quote


logger = AdbLogging.get_logger(__name__)



def _get_src_info(src):
Expand Down Expand Up @@ -67,3 +78,60 @@ async def pull(self, src, dest):

async with sync_conn:
return await sync.pull(src, dest)

async def install(self, path,
forward_lock=False, # -l
reinstall=False, # -r
test=False, # -t
installer_package_name="", # -i {installer_package_name}
shared_mass_storage=False, # -s
internal_system_memory=False, # -f
downgrade=False, # -d
grand_all_permissions=False # -g
):
dest = SyncAsync.temp(path)

try:
await self.push(path, dest)
except Exception:
raise InstallError("file transfer to device failed")

parameters = []
if forward_lock: parameters.append("-l")
if reinstall: parameters.append("-r")
if test: parameters.append("-t")
if len(installer_package_name) > 0: parameters.append("-i {}".format(installer_package_name))
if shared_mass_storage: parameters.append("-s")
if internal_system_memory: parameters.append("-f")
if downgrade: parameters.append("-d")
if grand_all_permissions: parameters.append("-g")

try:
result = await self.shell(
"pm install {} {}".format(" ".join(parameters), cmd_quote(dest))
)
match = re.search(self.INSTALL_RESULT_PATTERN, result)

if match and match.group(1) == "Success":
return True
elif match:
groups = match.groups()
raise InstallError(dest, groups[1])
else:
raise InstallError(dest, result)
finally:
await self.shell("rm -f {}".format(dest))

async def uninstall(self, package):
result = await self.shell("pm uninstall {}".format(package))

m = re.search(self.UNINSTALL_RESULT_PATTERN, result)

if m and m.group(1) == "Success":
return True
elif m:
logger.error(m.group(1))
return False
else:
logger.error("There is no message after uninstalling")
return False
7 changes: 6 additions & 1 deletion ppadb/sync_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ def _get_src_info(src):
if not exists:
return exists, None, None

timestamp = os.stat(src).st_mtime
timestamp = int(os.stat(src).st_mtime)
total_size = os.path.getsize(src)

return exists, timestamp, total_size


class SyncAsync:
TEMP_PATH = '/data/local/tmp'
DATA_MAX_LENGTH = 65536

def __init__(self, connection):
self.connection = connection

@staticmethod
def temp(path):
return "{}/{}".format(SyncAsync.TEMP_PATH, os.path.basename(path))

async def push(self, src, dest, mode, progress=None):
"""Push from local path |src| to |dest| on device.
:param progress: callback, called with (filename, total_size, sent_size)
Expand Down