Chromium Code Reviews| Index: build/android/gyp/instr.py |
| diff --git a/build/android/gyp/instr.py b/build/android/gyp/instr.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..c29e888bc6773ef352c92343b451fd1c4a76eaf8 |
| --- /dev/null |
| +++ b/build/android/gyp/instr.py |
| @@ -0,0 +1,163 @@ |
| +#!/usr/bin/env python |
|
frankf
2013/08/13 23:33:39
Can you pick a better module name than 'instr'?
gkanwar1
2013/08/15 19:49:20
Renamed to emma_instr.
|
| +# |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Instruments classes and jar files.""" |
|
frankf
2013/08/13 23:33:39
Expand this comment.
gkanwar1
2013/08/15 19:49:20
Done.
|
| + |
| +import collections |
| +import json |
| +import os |
| +import shutil |
| +import sys |
| + |
| +sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| +from pylib.utils import command_option_parser |
| + |
| +from util import build_utils |
| + |
| + |
| +def _AddCommonOptions(option_parser): |
| + """Adds common options to |option_parser|.""" |
| + option_parser.add_option('--input-path', help='Path to input file(s).') |
|
frankf
2013/08/13 23:33:39
Define what 'input' files are
gkanwar1
2013/08/15 19:49:20
Done.
|
| + option_parser.add_option('--output-path', |
| + help='Path to output final file(s) to.') |
|
frankf
2013/08/13 23:33:39
Same here
gkanwar1
2013/08/15 19:49:20
Done.
|
| + option_parser.add_option('--stamp', help='Path to touch when done.') |
| + |
| + |
| +def _AddInstrumentOptions(option_parser): |
| + """Adds options related to instrumentation to |option_parser|.""" |
| + option_parser.add_option('--coverage-file', |
| + help='File to create with coverage metadata') |
| + option_parser.add_option('--sources-file', |
| + help='File to create with the list of sources.') |
| + option_parser.add_option('--sources', |
| + help='Space separated list of sources.') |
| + option_parser.add_option('--src-root', |
| + help='Root of the src repository.') |
| + option_parser.add_option('--sdk-root', |
| + help='Root of the Android SDK.') |
| + |
| + |
| +def _AddInstrumentJarOptions(option_parser): |
| + """Adds options for the 'instrument_jar' command to |option_parser|.""" |
| + _AddCommonOptions(option_parser) |
| + _AddInstrumentOptions(option_parser) |
| + |
| + option_parser.add_option('--intermediate-dir', |
| + help=('Intermediate directory in which the ' |
| + 'instrumented jar will be produced in.')) |
| + |
| + |
| +def _AddInstrumentClassesOptions(option_parser): |
| + """Adds options for the 'instrument_classes' command to |option_parser|.""" |
| + _AddCommonOptions(option_parser) |
| + _AddInstrumentOptions(option_parser) |
| + |
| + |
| +def _RunCopyCommand(command, options, args, option_parser): |
| + """Just copies the jar from input to output locations.""" |
| + if not (options.input_path and options.output_path): |
| + option_parser.error('All arguments are required.') |
| + |
| + if os.path.isdir(options.input_path): |
| + shutil.rmtree(options.output_path, ignore_errors=True) |
| + shutil.copytree(options.input_path, options.output_path) |
| + else: |
| + shutil.copy(options.input_path, options.output_path) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| + |
| +def _CreateSourcesFile(sources_string, sources_file, src_root): |
| + """Adds all normalized source directories to |sources_file|.""" |
|
frankf
2013/08/13 23:33:39
Add Args section here and below
gkanwar1
2013/08/15 19:49:20
Done.
|
| + src_root = os.path.abspath(src_root) |
| + sources = build_utils.ParseGypList(sources_string) |
| + relative_sources = [] |
| + for s in sources: |
| + abs_source = os.path.abspath(s) |
| + if abs_source[:len(src_root)] != src_root: |
| + print ('Error: found source directory not under repository root: %s %s' |
| + % (abs_source, src_root)) |
| + return 1 |
| + rel_source = os.path.relpath(abs_source, src_root) |
| + |
| + relative_sources.append(rel_source) |
| + |
| + with open(sources_file, 'w') as f: |
| + json.dump(relative_sources, f) |
| + |
| + |
| +def _RunInstrumentJarCommand(command, options, args, option_parser): |
| + """Instruments the jar, producing the metadata and sources files.""" |
| + if not (options.input_path and options.output_path and |
| + options.intermediate_dir and options.coverage_file and |
| + options.sources_file and options.sources and options.src_root and |
| + options.sdk_root): |
| + option_parser.error('All arguments are required.') |
| + |
| + # TODO(gkanwar): Add '-ix' option to filter out useless classes. |
| + build_utils.CheckCallDie(['java', '-cp', |
| + os.path.join(options.sdk_root, 'tools', |
| + 'lib', 'emma.jar'), |
| + 'emma', 'instr', |
| + '-ip', options.input_path, |
| + '-d', options.intermediate_dir, |
| + '-out', options.coverage_file, |
| + '-m', 'fullcopy'], suppress_output=True) |
| + |
| + for jar in os.listdir(os.path.join(options.intermediate_dir, 'lib')): |
| + shutil.copy(os.path.join(options.intermediate_dir, 'lib', jar), |
| + options.output_path) |
| + |
| + _CreateSourcesFile(options.sources, options.sources_file, options.src_root) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| + |
| +def _RunInstrumentClassesCommand(command, options, args, option_parser): |
| + """Instruments the classes, producing the metadata and sources files.""" |
| + if not (options.input_path and options.output_path and |
| + options.coverage_file and options.sources_file and options.sources and |
| + options.src_root and options.sdk_root): |
| + option_parser.error('All arguments are required.') |
| + |
| + # TODO(gkanwar): Add '-ix' option to filter out useless classes. |
| + build_utils.CheckCallDie(['java', '-cp', |
| + os.path.join(options.sdk_root, 'tools', |
| + 'lib', 'emma.jar'), |
| + 'emma', 'instr', |
| + '-ip', options.input_path, |
| + '-d', options.output_path, |
| + '-out', options.coverage_file, |
| + '-m', 'copy'], suppress_output=True) |
| + |
| + _CreateSourcesFile(options.sources, options.sources_file, options.src_root) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| + |
| +CommandFunctionTuple = collections.namedtuple( |
| + 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) |
| +VALID_COMMANDS = { |
| + 'copy': CommandFunctionTuple(_AddCommonOptions, |
| + _RunCopyCommand), |
| + 'instrument_jar': CommandFunctionTuple(_AddInstrumentJarOptions, |
| + _RunInstrumentJarCommand), |
| + 'instrument_classes': CommandFunctionTuple(_AddInstrumentClassesOptions, |
| + _RunInstrumentClassesCommand), |
| +} |
| + |
| + |
| +def main(argv): |
| + option_parser = command_option_parser.CommandOptionParser( |
| + commands_dict=VALID_COMMANDS) |
| + command_option_parser.ParseAndExecute(option_parser) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |