Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/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.
| |
| 2 # | |
| 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 | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 """Instruments classes and jar files.""" | |
|
frankf
2013/08/13 23:33:39
Expand this comment.
gkanwar1
2013/08/15 19:49:20
Done.
| |
| 8 | |
| 9 import collections | |
| 10 import json | |
| 11 import os | |
| 12 import shutil | |
| 13 import sys | |
| 14 | |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | |
| 16 from pylib.utils import command_option_parser | |
| 17 | |
| 18 from util import build_utils | |
| 19 | |
| 20 | |
| 21 def _AddCommonOptions(option_parser): | |
| 22 """Adds common options to |option_parser|.""" | |
| 23 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.
| |
| 24 option_parser.add_option('--output-path', | |
| 25 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.
| |
| 26 option_parser.add_option('--stamp', help='Path to touch when done.') | |
| 27 | |
| 28 | |
| 29 def _AddInstrumentOptions(option_parser): | |
| 30 """Adds options related to instrumentation to |option_parser|.""" | |
| 31 option_parser.add_option('--coverage-file', | |
| 32 help='File to create with coverage metadata') | |
| 33 option_parser.add_option('--sources-file', | |
| 34 help='File to create with the list of sources.') | |
| 35 option_parser.add_option('--sources', | |
| 36 help='Space separated list of sources.') | |
| 37 option_parser.add_option('--src-root', | |
| 38 help='Root of the src repository.') | |
| 39 option_parser.add_option('--sdk-root', | |
| 40 help='Root of the Android SDK.') | |
| 41 | |
| 42 | |
| 43 def _AddInstrumentJarOptions(option_parser): | |
| 44 """Adds options for the 'instrument_jar' command to |option_parser|.""" | |
| 45 _AddCommonOptions(option_parser) | |
| 46 _AddInstrumentOptions(option_parser) | |
| 47 | |
| 48 option_parser.add_option('--intermediate-dir', | |
| 49 help=('Intermediate directory in which the ' | |
| 50 'instrumented jar will be produced in.')) | |
| 51 | |
| 52 | |
| 53 def _AddInstrumentClassesOptions(option_parser): | |
| 54 """Adds options for the 'instrument_classes' command to |option_parser|.""" | |
| 55 _AddCommonOptions(option_parser) | |
| 56 _AddInstrumentOptions(option_parser) | |
| 57 | |
| 58 | |
| 59 def _RunCopyCommand(command, options, args, option_parser): | |
| 60 """Just copies the jar from input to output locations.""" | |
| 61 if not (options.input_path and options.output_path): | |
| 62 option_parser.error('All arguments are required.') | |
| 63 | |
| 64 if os.path.isdir(options.input_path): | |
| 65 shutil.rmtree(options.output_path, ignore_errors=True) | |
| 66 shutil.copytree(options.input_path, options.output_path) | |
| 67 else: | |
| 68 shutil.copy(options.input_path, options.output_path) | |
| 69 | |
| 70 if options.stamp: | |
| 71 build_utils.Touch(options.stamp) | |
| 72 | |
| 73 | |
| 74 def _CreateSourcesFile(sources_string, sources_file, src_root): | |
| 75 """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.
| |
| 76 src_root = os.path.abspath(src_root) | |
| 77 sources = build_utils.ParseGypList(sources_string) | |
| 78 relative_sources = [] | |
| 79 for s in sources: | |
| 80 abs_source = os.path.abspath(s) | |
| 81 if abs_source[:len(src_root)] != src_root: | |
| 82 print ('Error: found source directory not under repository root: %s %s' | |
| 83 % (abs_source, src_root)) | |
| 84 return 1 | |
| 85 rel_source = os.path.relpath(abs_source, src_root) | |
| 86 | |
| 87 relative_sources.append(rel_source) | |
| 88 | |
| 89 with open(sources_file, 'w') as f: | |
| 90 json.dump(relative_sources, f) | |
| 91 | |
| 92 | |
| 93 def _RunInstrumentJarCommand(command, options, args, option_parser): | |
| 94 """Instruments the jar, producing the metadata and sources files.""" | |
| 95 if not (options.input_path and options.output_path and | |
| 96 options.intermediate_dir and options.coverage_file and | |
| 97 options.sources_file and options.sources and options.src_root and | |
| 98 options.sdk_root): | |
| 99 option_parser.error('All arguments are required.') | |
| 100 | |
| 101 # TODO(gkanwar): Add '-ix' option to filter out useless classes. | |
| 102 build_utils.CheckCallDie(['java', '-cp', | |
| 103 os.path.join(options.sdk_root, 'tools', | |
| 104 'lib', 'emma.jar'), | |
| 105 'emma', 'instr', | |
| 106 '-ip', options.input_path, | |
| 107 '-d', options.intermediate_dir, | |
| 108 '-out', options.coverage_file, | |
| 109 '-m', 'fullcopy'], suppress_output=True) | |
| 110 | |
| 111 for jar in os.listdir(os.path.join(options.intermediate_dir, 'lib')): | |
| 112 shutil.copy(os.path.join(options.intermediate_dir, 'lib', jar), | |
| 113 options.output_path) | |
| 114 | |
| 115 _CreateSourcesFile(options.sources, options.sources_file, options.src_root) | |
| 116 | |
| 117 if options.stamp: | |
| 118 build_utils.Touch(options.stamp) | |
| 119 | |
| 120 | |
| 121 def _RunInstrumentClassesCommand(command, options, args, option_parser): | |
| 122 """Instruments the classes, producing the metadata and sources files.""" | |
| 123 if not (options.input_path and options.output_path and | |
| 124 options.coverage_file and options.sources_file and options.sources and | |
| 125 options.src_root and options.sdk_root): | |
| 126 option_parser.error('All arguments are required.') | |
| 127 | |
| 128 # TODO(gkanwar): Add '-ix' option to filter out useless classes. | |
| 129 build_utils.CheckCallDie(['java', '-cp', | |
| 130 os.path.join(options.sdk_root, 'tools', | |
| 131 'lib', 'emma.jar'), | |
| 132 'emma', 'instr', | |
| 133 '-ip', options.input_path, | |
| 134 '-d', options.output_path, | |
| 135 '-out', options.coverage_file, | |
| 136 '-m', 'copy'], suppress_output=True) | |
| 137 | |
| 138 _CreateSourcesFile(options.sources, options.sources_file, options.src_root) | |
| 139 | |
| 140 if options.stamp: | |
| 141 build_utils.Touch(options.stamp) | |
| 142 | |
| 143 | |
| 144 CommandFunctionTuple = collections.namedtuple( | |
| 145 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) | |
| 146 VALID_COMMANDS = { | |
| 147 'copy': CommandFunctionTuple(_AddCommonOptions, | |
| 148 _RunCopyCommand), | |
| 149 'instrument_jar': CommandFunctionTuple(_AddInstrumentJarOptions, | |
| 150 _RunInstrumentJarCommand), | |
| 151 'instrument_classes': CommandFunctionTuple(_AddInstrumentClassesOptions, | |
| 152 _RunInstrumentClassesCommand), | |
| 153 } | |
| 154 | |
| 155 | |
| 156 def main(argv): | |
| 157 option_parser = command_option_parser.CommandOptionParser( | |
| 158 commands_dict=VALID_COMMANDS) | |
| 159 command_option_parser.ParseAndExecute(option_parser) | |
| 160 | |
| 161 | |
| 162 if __name__ == '__main__': | |
| 163 sys.exit(main(sys.argv)) | |
| OLD | NEW |