From 855f44725e9c7df38911a40b0e34c56b40cf8301 Mon Sep 17 00:00:00 2001 From: Mohammad Abu-Garbeyyeh Date: Sun, 28 Jan 2018 10:41:29 +0200 Subject: [PATCH 1/5] Added app uninstallation feature --- adb/adb_commands.py | 17 +++++++++++++++++ adb/adb_debug.py | 1 + test/adb_test.py | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index fae84f9..8cd6ddd 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -132,6 +132,23 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None): return self.Shell('pm install -r "%s"' % destination_path, timeout_ms=timeout_ms) + def Uninstall(self, package_name, keep_data=False, timeout_ms=None): + """Removes a package from the device. + + Args: + package_name: Package name of target package. + keep_data: whether to keep the data and cache directories + timeout_ms: Expected timeout for pushing and installing. + + Returns: + The pm uninstall output. + """ + cmds = ['pm uninstall'] + if keep_data: + cmds.append('-k') + cmds.append('"%s"' % package_name) + return self.Shell(" ".join(cmds), timeout_ms=timeout_ms) + def Push(self, source_file, device_filename, mtime='0', timeout_ms=None): """Push a file or directory to the device. diff --git a/adb/adb_debug.py b/adb/adb_debug.py index f396bdb..b590bc6 100755 --- a/adb/adb_debug.py +++ b/adb/adb_debug.py @@ -123,6 +123,7 @@ def main(): common_cli.MakeSubparser( subparsers, parents, adb_commands.AdbCommands.Install) + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Uninstall) common_cli.MakeSubparser(subparsers, parents, List) common_cli.MakeSubparser(subparsers, parents, Logcat) common_cli.MakeSubparser( diff --git a/test/adb_test.py b/test/adb_test.py index 29bce3a..e59929c 100755 --- a/test/adb_test.py +++ b/test/adb_test.py @@ -116,6 +116,15 @@ def testBigResponseShell(self): self.assertEqual(b''.join(responses).decode('utf8'), adb_commands.Shell(command)) + def testUninstall(self): + package_name = "com.test.package" + response = b'Success' + + usb = self._ExpectCommand(b'shell', bytes('pm uninstall "%s"' % package_name), response) + + adb_commands = self._Connect(usb) + self.assertEquals(response, adb_commands.Uninstall(package_name)) + def testStreamingResponseShell(self): command = b'keepin it real big' # expect multiple lines From a874814324defb7878e864866afe851b21570ead Mon Sep 17 00:00:00 2001 From: Mohammad Abu-Garbeyyeh Date: Sun, 28 Jan 2018 10:42:00 +0200 Subject: [PATCH 2/5] Make -r optional in app installation --- adb/adb_commands.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 8cd6ddd..28c3295 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -109,7 +109,7 @@ def Devices(cls): def GetState(self): return self._device_state - def Install(self, apk_path, destination_dir='', timeout_ms=None): + def Install(self, apk_path, destination_dir='', replace_existing=True, timeout_ms=None): """Install an apk to the device. Doesn't support verifier file, instead allows destination directory to be @@ -119,6 +119,7 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None): apk_path: Local path to apk to install. destination_dir: Optional destination directory. Use /system/app/ for persistent applications. + replace_existing: whether to replace existing application timeout_ms: Expected timeout for pushing and installing. Returns: @@ -129,8 +130,13 @@ def Install(self, apk_path, destination_dir='', timeout_ms=None): basename = os.path.basename(apk_path) destination_path = destination_dir + basename self.Push(apk_path, destination_path, timeout_ms=timeout_ms) - return self.Shell('pm install -r "%s"' % destination_path, - timeout_ms=timeout_ms) + + cmds = ['pm install'] + if replace_existing: + cmds.append('-r') + cmds.append('"%s"' % destination_path) + + return self.Shell(" ".join(cmds), timeout_ms=timeout_ms) def Uninstall(self, package_name, keep_data=False, timeout_ms=None): """Removes a package from the device. From 4069ff605f85d0178098d7f1a575f97d34e7bf18 Mon Sep 17 00:00:00 2001 From: Mohammad Abu-Garbeyyeh Date: Sun, 28 Jan 2018 11:01:26 +0200 Subject: [PATCH 3/5] Fixed unit test --- test/adb_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/adb_test.py b/test/adb_test.py index e59929c..1b8800c 100755 --- a/test/adb_test.py +++ b/test/adb_test.py @@ -118,9 +118,9 @@ def testBigResponseShell(self): def testUninstall(self): package_name = "com.test.package" - response = b'Success' + response = 'Success' - usb = self._ExpectCommand(b'shell', bytes('pm uninstall "%s"' % package_name), response) + usb = self._ExpectCommand(b'shell', ('pm uninstall "%s"' % package_name).encode('utf8'), response) adb_commands = self._Connect(usb) self.assertEquals(response, adb_commands.Uninstall(package_name)) From 0d6e7fd07e54989b09b3157cf633b673cf18cf56 Mon Sep 17 00:00:00 2001 From: Mohammad Abu-Garbeyyeh Date: Tue, 30 Jan 2018 13:02:45 +0200 Subject: [PATCH 4/5] Changed variable name --- adb/adb_commands.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 28c3295..fd94edd 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -131,12 +131,12 @@ def Install(self, apk_path, destination_dir='', replace_existing=True, timeout_m destination_path = destination_dir + basename self.Push(apk_path, destination_path, timeout_ms=timeout_ms) - cmds = ['pm install'] + cmd = ['pm install'] if replace_existing: - cmds.append('-r') - cmds.append('"%s"' % destination_path) + cmd.append('-r') + cmd.append('"%s"' % destination_path) - return self.Shell(" ".join(cmds), timeout_ms=timeout_ms) + return self.Shell(" ".join(cmd), timeout_ms=timeout_ms) def Uninstall(self, package_name, keep_data=False, timeout_ms=None): """Removes a package from the device. @@ -149,11 +149,11 @@ def Uninstall(self, package_name, keep_data=False, timeout_ms=None): Returns: The pm uninstall output. """ - cmds = ['pm uninstall'] + cmd = ['pm uninstall'] if keep_data: - cmds.append('-k') - cmds.append('"%s"' % package_name) - return self.Shell(" ".join(cmds), timeout_ms=timeout_ms) + cmd.append('-k') + cmd.append('"%s"' % package_name) + return self.Shell(" ".join(cmd), timeout_ms=timeout_ms) def Push(self, source_file, device_filename, mtime='0', timeout_ms=None): """Push a file or directory to the device. From 67fc660218cef7d3661d32b04e6fc9a683ae8f09 Mon Sep 17 00:00:00 2001 From: Fahrzin Hemmati Date: Tue, 30 Jan 2018 03:12:00 -0800 Subject: [PATCH 5/5] Change quotes for consistency --- adb/adb_commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index fd94edd..e0fe15f 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -136,7 +136,7 @@ def Install(self, apk_path, destination_dir='', replace_existing=True, timeout_m cmd.append('-r') cmd.append('"%s"' % destination_path) - return self.Shell(" ".join(cmd), timeout_ms=timeout_ms) + return self.Shell(' '.join(cmd), timeout_ms=timeout_ms) def Uninstall(self, package_name, keep_data=False, timeout_ms=None): """Removes a package from the device. @@ -153,7 +153,7 @@ def Uninstall(self, package_name, keep_data=False, timeout_ms=None): if keep_data: cmd.append('-k') cmd.append('"%s"' % package_name) - return self.Shell(" ".join(cmd), timeout_ms=timeout_ms) + return self.Shell(' '.join(cmd), timeout_ms=timeout_ms) def Push(self, source_file, device_filename, mtime='0', timeout_ms=None): """Push a file or directory to the device.