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 """Instruments classes and jar files. | 7 """Instruments classes and jar files. |
8 | 8 |
9 This script corresponds to the 'emma_instr' action in the java build process. | 9 This script corresponds to the 'emma_instr' action in the java build process. |
10 Depending on whether emma_instrument is set, the 'emma_instr' action will either | 10 Depending on whether emma_instrument is set, the 'emma_instr' action will either |
(...skipping 30 matching lines...) Expand all Loading... |
41 help=('Path to output final file(s) to. Either the ' | 41 help=('Path to output final file(s) to. Either the ' |
42 'final classes directory, or the directory in ' | 42 'final classes directory, or the directory in ' |
43 'which to place the instrumented/copied jar.')) | 43 'which to place the instrumented/copied jar.')) |
44 option_parser.add_option('--stamp', help='Path to touch when done.') | 44 option_parser.add_option('--stamp', help='Path to touch when done.') |
45 | 45 |
46 | 46 |
47 def _AddInstrumentOptions(option_parser): | 47 def _AddInstrumentOptions(option_parser): |
48 """Adds options related to instrumentation to |option_parser|.""" | 48 """Adds options related to instrumentation to |option_parser|.""" |
49 _AddCommonOptions(option_parser) | 49 _AddCommonOptions(option_parser) |
50 option_parser.add_option('--coverage-file', | 50 option_parser.add_option('--coverage-file', |
51 help='File to create with coverage metadata') | 51 help='File to create with coverage metadata.') |
52 option_parser.add_option('--sources-file', | 52 option_parser.add_option('--sources-file', |
53 help='File to create with the list of sources.') | 53 help='File to create with the list of sources.') |
54 option_parser.add_option('--sources', | 54 option_parser.add_option('--sources', |
55 help='Space separated list of sources.') | 55 help='Space separated list of sources.') |
56 option_parser.add_option('--src-root', | 56 option_parser.add_option('--src-root', |
57 help='Root of the src repository.') | 57 help='Root of the src repository.') |
58 option_parser.add_option('--emma-jar', | 58 option_parser.add_option('--emma-jar', |
59 help='Path to emma.jar.') | 59 help='Path to emma.jar.') |
60 | 60 |
61 | 61 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 option_parser: optparse.OptionParser object. | 125 option_parser: optparse.OptionParser object. |
126 | 126 |
127 Returns: | 127 Returns: |
128 An exit code. | 128 An exit code. |
129 """ | 129 """ |
130 if not (options.input_path and options.output_path and | 130 if not (options.input_path and options.output_path and |
131 options.coverage_file and options.sources_file and options.sources and | 131 options.coverage_file and options.sources_file and options.sources and |
132 options.src_root and options.emma_jar): | 132 options.src_root and options.emma_jar): |
133 option_parser.error('All arguments are required.') | 133 option_parser.error('All arguments are required.') |
134 | 134 |
| 135 coverage_file = os.path.join(os.path.dirname(options.output_path), |
| 136 options.coverage_file) |
| 137 sources_file = os.path.join(os.path.dirname(options.output_path), |
| 138 options.sources_file) |
135 temp_dir = tempfile.mkdtemp() | 139 temp_dir = tempfile.mkdtemp() |
136 try: | 140 try: |
137 # TODO(gkanwar): Add '-ix' option to filter out useless classes. | 141 # TODO(gkanwar): Add '-ix' option to filter out useless classes. |
138 build_utils.CheckCallDie(['java', '-cp', options.emma_jar, | 142 build_utils.CheckCallDie(['java', '-cp', options.emma_jar, |
139 'emma', 'instr', | 143 'emma', 'instr', |
140 '-ip', options.input_path, | 144 '-ip', options.input_path, |
141 '-d', temp_dir, | 145 '-d', temp_dir, |
142 '-out', options.coverage_file, | 146 '-out', coverage_file, |
143 '-m', 'fullcopy'], suppress_output=True) | 147 '-m', 'fullcopy'], suppress_output=True) |
144 | 148 |
145 if command == 'instrument_jar': | 149 if command == 'instrument_jar': |
146 for jar in os.listdir(os.path.join(temp_dir, 'lib')): | 150 for jar in os.listdir(os.path.join(temp_dir, 'lib')): |
147 shutil.copy(os.path.join(temp_dir, 'lib', jar), | 151 shutil.copy(os.path.join(temp_dir, 'lib', jar), |
148 options.output_path) | 152 options.output_path) |
149 else: # 'instrument_classes' | 153 else: # 'instrument_classes' |
150 if os.path.isdir(options.output_path): | 154 if os.path.isdir(options.output_path): |
151 shutil.rmtree(options.output_path, ignore_errors=True) | 155 shutil.rmtree(options.output_path, ignore_errors=True) |
152 shutil.copytree(os.path.join(temp_dir, 'classes'), | 156 shutil.copytree(os.path.join(temp_dir, 'classes'), |
153 options.output_path) | 157 options.output_path) |
154 finally: | 158 finally: |
155 shutil.rmtree(temp_dir) | 159 shutil.rmtree(temp_dir) |
156 | 160 |
157 _CreateSourcesFile(options.sources, options.sources_file, options.src_root) | 161 _CreateSourcesFile(options.sources, sources_file, options.src_root) |
158 | 162 |
159 if options.stamp: | 163 if options.stamp: |
160 build_utils.Touch(options.stamp) | 164 build_utils.Touch(options.stamp) |
161 | 165 |
162 return 0 | 166 return 0 |
163 | 167 |
164 | 168 |
165 CommandFunctionTuple = collections.namedtuple( | 169 CommandFunctionTuple = collections.namedtuple( |
166 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) | 170 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) |
167 VALID_COMMANDS = { | 171 VALID_COMMANDS = { |
168 'copy': CommandFunctionTuple(_AddCommonOptions, | 172 'copy': CommandFunctionTuple(_AddCommonOptions, |
169 _RunCopyCommand), | 173 _RunCopyCommand), |
170 'instrument_jar': CommandFunctionTuple(_AddInstrumentOptions, | 174 'instrument_jar': CommandFunctionTuple(_AddInstrumentOptions, |
171 _RunInstrumentCommand), | 175 _RunInstrumentCommand), |
172 'instrument_classes': CommandFunctionTuple(_AddInstrumentOptions, | 176 'instrument_classes': CommandFunctionTuple(_AddInstrumentOptions, |
173 _RunInstrumentCommand), | 177 _RunInstrumentCommand), |
174 } | 178 } |
175 | 179 |
176 | 180 |
177 def main(argv): | 181 def main(argv): |
178 option_parser = command_option_parser.CommandOptionParser( | 182 option_parser = command_option_parser.CommandOptionParser( |
179 commands_dict=VALID_COMMANDS) | 183 commands_dict=VALID_COMMANDS) |
180 command_option_parser.ParseAndExecute(option_parser) | 184 command_option_parser.ParseAndExecute(option_parser) |
181 | 185 |
182 | 186 |
183 if __name__ == '__main__': | 187 if __name__ == '__main__': |
184 sys.exit(main(sys.argv)) | 188 sys.exit(main(sys.argv)) |
OLD | NEW |