Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Creates symlinks to native libraries for an APK. | 7 """Creates symlinks to native libraries for an APK. |
| 8 | 8 |
| 9 The native libraries should have previously been pushed to the device (in | 9 The native libraries should have previously been pushed to the device (in |
| 10 options.target_dir). This script then creates links in an apk's lib/ folder to | 10 options.target_dir). This script then creates links in an apk's lib/ folder to |
| 11 those native libraries. | 11 those native libraries. |
| 12 """ | 12 """ |
| 13 | 13 |
| 14 import json | 14 import json |
| 15 import optparse | 15 import optparse |
| 16 import os | 16 import os |
| 17 import sys | 17 import sys |
| 18 | 18 |
| 19 from util import build_utils | 19 from util import build_utils |
| 20 from util import md5_check | 20 from util import md5_check |
| 21 | 21 |
| 22 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | 22 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') |
| 23 sys.path.append(BUILD_ANDROID_DIR) | 23 sys.path.append(BUILD_ANDROID_DIR) |
| 24 | 24 |
| 25 from pylib import android_commands | 25 from pylib import android_commands |
| 26 from pylib.utils import apk_helper | 26 from pylib.utils import apk_helper |
| 27 | 27 |
| 28 def RunShellCommand(adb, cmd, expect_output=False): | |
| 29 output = adb.RunShellCommand(cmd) | |
| 28 | 30 |
| 29 def RunLinkCommand(adb, target, link): | 31 if not expect_output and output: |
| 30 cmd = ( | |
| 31 'rm ' + link + ' > /dev/null 2>&1 \n' | |
| 32 'ln -s ' + target + ' ' + link + '\n' | |
| 33 ) | |
| 34 result = adb.RunShellCommand(cmd) | |
| 35 | |
| 36 if result: | |
| 37 raise Exception( | 32 raise Exception( |
| 38 'Unexpected output creating links on device.\n' + | 33 'Unexpected output running command.\n' + |
| 39 '\n'.join(result)) | 34 '\n'.join(output)) |
| 40 | 35 |
| 41 | 36 |
| 42 def CreateLinks(options): | 37 def CreateSymlinkScript(options): |
| 43 libraries = build_utils.ReadJson(options.libraries_json) | 38 libraries = build_utils.ReadJson(options.libraries_json) |
| 39 | |
| 40 link_cmd = ( | |
| 41 'rm $APK_LIBRARIES_DIR/%(lib_basename)s > /dev/null 2>&1 \n' | |
| 42 'ln -s $STRIPPED_LIBRARIES_DIR/%(lib_basename)s ' | |
| 43 '$APK_LIBRARIES_DIR/%(lib_basename)s \n' | |
| 44 ) | |
| 45 | |
| 46 script = ('#!/bin/sh \n' | |
| 47 '# usage: script.sh target_libraries_dir apk_libraries_dir \n') | |
| 48 | |
| 49 for lib in libraries: | |
| 50 script += link_cmd % { 'lib_basename': lib } | |
| 51 | |
| 52 with open(options.script_host_path, 'w') as scriptfile: | |
| 53 scriptfile.write(script) | |
| 54 | |
| 55 | |
| 56 def TriggerSymlinkScript(options): | |
| 44 apk_package = apk_helper.GetPackageName(options.apk) | 57 apk_package = apk_helper.GetPackageName(options.apk) |
| 58 apk_libraries_dir = '/data/data/' + apk_package + '/lib' | |
|
Yaron
2013/04/18 18:06:25
'/data/data/%s/lib/' % apk_package
cjhopman
2013/04/18 21:05:23
Done.
| |
| 45 | 59 |
| 46 adb = android_commands.AndroidCommands() | 60 adb = android_commands.AndroidCommands() |
| 47 serial_number = adb.Adb().GetSerialNumber() | 61 mkdir_cmd = 'mkdir ' + os.path.dirname(options.script_device_path) |
| 48 for lib in libraries: | 62 RunShellCommand(adb, mkdir_cmd, expect_output=True) |
|
Yaron
2013/04/18 18:06:25
You're just trying to avoid the directory already
cjhopman
2013/04/18 21:05:23
Done. I like that much better.
| |
| 49 host_path = os.path.join(options.libraries_dir, lib) | 63 adb.PushIfNeeded(options.script_host_path, options.script_device_path) |
| 50 def CreateLink(): | |
| 51 link = '/data/data/' + apk_package + '/lib/' + lib | |
| 52 target = options.target_dir + '/' + lib | |
| 53 RunLinkCommand(adb, target, link) | |
| 54 | 64 |
| 55 record_path = '%s.%s.link.md5.stamp' % (host_path, serial_number) | 65 trigger_cmd = ( |
| 56 md5_check.CallAndRecordIfStale( | 66 'APK_LIBRARIES_DIR=%(apk_libraries_dir)s; ' |
| 57 CreateLink, | 67 'STRIPPED_LIBRARIES_DIR=%(target_dir)s; ' |
| 58 record_path=record_path, | 68 '. %(script_device_path)s' |
| 59 input_paths=[host_path]) | 69 ) % { |
| 70 'apk_libraries_dir': apk_libraries_dir, | |
| 71 'target_dir': options.target_dir, | |
| 72 'script_device_path': options.script_device_path | |
| 73 } | |
| 74 RunShellCommand(adb, trigger_cmd) | |
| 75 | |
| 60 | 76 |
| 61 | 77 |
| 62 def main(argv): | 78 def main(argv): |
| 63 parser = optparse.OptionParser() | 79 parser = optparse.OptionParser() |
| 64 parser.add_option('--apk', help='Path to the apk.') | 80 parser.add_option('--apk', help='Path to the apk.') |
| 81 parser.add_option('--script-host-path', | |
| 82 help='Path on the host for the symlink script.') | |
| 83 parser.add_option('--script-device-path', | |
| 84 help='Path on the device to push the created symlink script.') | |
| 65 parser.add_option('--libraries-json', | 85 parser.add_option('--libraries-json', |
| 66 help='Path to the json list of native libraries.') | 86 help='Path to the json list of native libraries.') |
| 67 parser.add_option('--target-dir', | 87 parser.add_option('--target-dir', |
| 68 help='Device directory that contains the target libraries for symlinks.') | 88 help='Device directory that contains the target libraries for symlinks.') |
| 69 parser.add_option('--libraries-dir', | |
| 70 help='Directory that contains stripped libraries ' | |
| 71 '(used to determine if a library has changed since last push).') | |
| 72 parser.add_option('--stamp', help='Path to touch on success.') | 89 parser.add_option('--stamp', help='Path to touch on success.') |
| 73 options, _ = parser.parse_args() | 90 options, _ = parser.parse_args() |
| 74 | 91 |
| 75 required_options = ['apk', 'libraries_json', 'target_dir', 'libraries_dir'] | 92 required_options = ['apk', 'target_dir'] |
|
Yaron
2013/04/18 18:06:25
Why is libraries_json not required?
cjhopman
2013/04/18 21:05:23
Done.
| |
| 76 build_utils.CheckOptions(options, parser, required=required_options) | 93 build_utils.CheckOptions(options, parser, required=required_options) |
| 77 | 94 |
| 78 CreateLinks(options) | 95 CreateSymlinkScript(options) |
| 96 TriggerSymlinkScript(options) | |
| 79 | 97 |
| 80 if options.stamp: | 98 if options.stamp: |
|
Yaron
2013/04/18 18:06:25
You're never passing in the stamp anymore
cjhopman
2013/04/18 21:05:23
In all these scripts we have had --stamp as option
| |
| 81 build_utils.Touch(options.stamp) | 99 build_utils.Touch(options.stamp) |
| 82 | 100 |
| 83 | 101 |
| 84 if __name__ == '__main__': | 102 if __name__ == '__main__': |
| 85 sys.exit(main(sys.argv)) | 103 sys.exit(main(sys.argv)) |
| OLD | NEW |