1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import posixpath
7
8from devil import devil_env
9from devil.android import device_errors
10from devil.android.constants import file_system
11
12BIN_DIR = '%s/bin' % file_system.TEST_EXECUTABLE_DIR
13_FRAMEWORK_DIR = '%s/framework' % file_system.TEST_EXECUTABLE_DIR
14
15_COMMANDS = {
16  'unzip': 'org.chromium.android.commands.unzip.Unzip',
17}
18
19_SHELL_COMMAND_FORMAT = (
20"""#!/system/bin/sh
21base=%s
22export CLASSPATH=$base/framework/chromium_commands.jar
23exec app_process $base/bin %s $@
24""")
25
26
27def Installed(device):
28  paths = [posixpath.join(BIN_DIR, c) for c in _COMMANDS]
29  paths.append(posixpath.join(_FRAMEWORK_DIR, 'chromium_commands.jar'))
30  return device.PathExists(paths)
31
32
33def InstallCommands(device):
34  if device.IsUserBuild():
35    raise device_errors.CommandFailedError(
36        'chromium_commands currently requires a userdebug build.',
37        device_serial=device.adb.GetDeviceSerial())
38
39  chromium_commands_jar_path = devil_env.config.FetchPath('chromium_commands')
40  if not os.path.exists(chromium_commands_jar_path):
41    raise device_errors.CommandFailedError(
42        '%s not found. Please build chromium_commands.'
43        % chromium_commands_jar_path)
44
45  device.RunShellCommand(
46      ['mkdir', '-p', BIN_DIR, _FRAMEWORK_DIR], check_return=True)
47  for command, main_class in _COMMANDS.iteritems():
48    shell_command = _SHELL_COMMAND_FORMAT % (
49        file_system.TEST_EXECUTABLE_DIR, main_class)
50    shell_file = '%s/%s' % (BIN_DIR, command)
51    device.WriteFile(shell_file, shell_command)
52    device.RunShellCommand(
53        ['chmod', '755', shell_file], check_return=True)
54
55  device.adb.Push(
56      chromium_commands_jar_path,
57      '%s/chromium_commands.jar' % _FRAMEWORK_DIR)
58