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..a0082a4a2b1de1b99eec54ac5da1a781a6e9fc28 |
| --- /dev/null |
| +++ b/build/android/gyp/instr.py |
| @@ -0,0 +1,151 @@ |
| +#!/usr/bin/env python |
| +# |
| +# 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.""" |
| + |
| +import collections |
| +import glob |
| +import os |
| +import sys |
| + |
| +sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| +from pylib import cmd_helper |
|
cjhopman
2013/08/09 18:54:37
we try to minimize the use of build/android/pylib
gkanwar1
2013/08/09 23:32:53
Removed references to cmd_helper and constants. co
cjhopman
2013/08/10 00:11:47
That's fine.
|
| +from pylib import constants |
|
cjhopman
2013/08/09 18:54:37
This looks like it's just used for paths. Pass tho
gkanwar1
2013/08/09 23:32:53
Done.
|
| +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).') |
| + option_parser.add_option('--output-path', |
| + help='Path to output final file(s) to.') |
| + 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.') |
| + |
| + |
| +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.') |
| + |
| + cmd_helper.RunCmd(['cp', '-R', options.input_path, options.output_path]) |
|
cjhopman
2013/08/09 18:54:37
shutil.copy(options.input_path, options.output_pat
gkanwar1
2013/08/09 23:32:53
Done.
|
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| + |
| +def _CreateSourcesFile(sources_string, sources_file): |
| + """Adds all normalized source directories to |sources_file|.""" |
| + sources = sources_string.split() |
| + relative_sources = [] |
| + for s in sources: |
| + if s.startswith('"') and s.endswith('"'): |
| + s = s[1:-1] |
|
cjhopman
2013/08/09 18:54:37
sources = build_utils.ParseGypList(sources_string)
gkanwar1
2013/08/09 23:32:53
Done.
|
| + abs_source = os.path.abspath(s) |
| + if abs_source[:len(constants.DIR_SOURCE_ROOT)] != constants.DIR_SOURCE_ROOT: |
| + print 'Error: found source directory not under repository root.' |
| + return constants.ERROR_EXIT_CODE |
| + rel_source = abs_source[len(constants.DIR_SOURCE_ROOT)+1:] |
| + relative_sources.append(rel_source) |
| + |
| + with open(sources_file, 'w') as f: |
| + f.write(' '.join(relative_sources)) |
|
cjhopman
2013/08/09 18:54:37
I would write this json formatted, but not require
gkanwar1
2013/08/09 23:32:53
Sounds like a good idea. Done.
|
| + |
| + |
| +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): |
| + option_parser.error('All arguments are required.') |
| + |
| + emma_jar = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', |
|
cjhopman
2013/08/09 18:54:37
Same as below.
gkanwar1
2013/08/09 23:32:53
Done.
|
| + 'emma.jar') |
| + |
| + # TODO(gkanwar): Add '-ix' option to filter out useless classes. |
| + build_utils.CheckCallDie(['java', '-cp', emma_jar, 'emma', 'instr', |
| + '-ip', options.input_path, |
| + '-d', options.intermediate_dir, |
| + '-out', options.coverage_file, |
| + '-m', 'fullcopy'], suppress_output=True) |
| + cmd_helper.RunCmd(['cp'] + glob.glob(os.path.join( |
|
cjhopman
2013/08/09 18:54:37
Do this copying in python. Can possibly just shuti
gkanwar1
2013/08/09 23:32:53
Done.
|
| + options.intermediate_dir, 'lib', '*')) + [options.output_path]) |
| + |
| + _CreateSourcesFile(options.sources, options.sources_file) |
| + |
| + 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): |
| + option_parser.error('All arguments are required.') |
| + |
| + emma_jar = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', |
|
cjhopman
2013/08/09 18:54:37
Don't calculate the path here, pass it in as an ar
gkanwar1
2013/08/09 23:32:53
Fixed here and in generate_emma_html.py
|
| + 'emma.jar') |
| + |
| + # TODO(gkanwar): Add '-ix' option to filter out useless classes. |
| + build_utils.CheckCallDie(['java', '-cp', 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) |
| + |
| + 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)) |