| 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 |
| 11 call one of the instrument commands, or the copy command. | 11 call one of the instrument commands, or the copy command. |
| 12 | 12 |
| 13 Possible commands are: | 13 Possible commands are: |
| 14 - instrument_jar: Accepts a jar and instruments it using emma.jar. | 14 - instrument_jar: Accepts a jar and instruments it using emma.jar. |
| 15 - instrument_classes: Accepts a directory contains java classes and instruments | 15 - instrument_classes: Accepts a directory containing java classes and |
| 16 it using emma.jar. | 16 instruments it using emma.jar. |
| 17 - copy: Triggered instead of an instrumentation command when we don't have EMMA | 17 - copy: Called when EMMA coverage is not enabled. This allows us to make |
| 18 coverage enabled. This allows us to make this a required step without | 18 this a required step without necessarily instrumenting on every build. |
| 19 necessarily instrumenting on every build. | 19 Also removes any stale coverage files. |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 import collections | 22 import collections |
| 23 import json | 23 import json |
| 24 import os | 24 import os |
| 25 import shutil | 25 import shutil |
| 26 import sys | 26 import sys |
| 27 import tempfile | 27 import tempfile |
| 28 | 28 |
| 29 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | 29 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 30 from pylib.utils import command_option_parser | 30 from pylib.utils import command_option_parser |
| 31 | 31 |
| 32 from util import build_utils | 32 from util import build_utils |
| 33 | 33 |
| 34 | 34 |
| 35 def _AddCommonOptions(option_parser): | 35 def _AddCommonOptions(option_parser): |
| 36 """Adds common options to |option_parser|.""" | 36 """Adds common options to |option_parser|.""" |
| 37 option_parser.add_option('--input-path', | 37 option_parser.add_option('--input-path', |
| 38 help=('Path to input file(s). Either the classes ' | 38 help=('Path to input file(s). Either the classes ' |
| 39 'directory, or the path to a jar.')) | 39 'directory, or the path to a jar.')) |
| 40 option_parser.add_option('--output-path', | 40 option_parser.add_option('--output-path', |
| 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 option_parser.add_option('--coverage-file', |
| 46 help='File to create with coverage metadata.') |
| 47 option_parser.add_option('--sources-file', |
| 48 help='File to create with the list of sources.') |
| 45 | 49 |
| 46 | 50 |
| 47 def _AddInstrumentOptions(option_parser): | 51 def _AddInstrumentOptions(option_parser): |
| 48 """Adds options related to instrumentation to |option_parser|.""" | 52 """Adds options related to instrumentation to |option_parser|.""" |
| 49 _AddCommonOptions(option_parser) | 53 _AddCommonOptions(option_parser) |
| 50 option_parser.add_option('--coverage-file', | |
| 51 help='File to create with coverage metadata.') | |
| 52 option_parser.add_option('--sources-file', | |
| 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 |
| 62 def _RunCopyCommand(command, options, args, option_parser): | 62 def _RunCopyCommand(command, options, args, option_parser): |
| 63 """Just copies the jar from input to output locations. | 63 """Copies the jar from input to output locations. |
| 64 |
| 65 Also removes any old coverage/sources file. |
| 64 | 66 |
| 65 Args: | 67 Args: |
| 66 command: String indicating the command that was received to trigger | 68 command: String indicating the command that was received to trigger |
| 67 this function. | 69 this function. |
| 68 options: optparse options dictionary. | 70 options: optparse options dictionary. |
| 69 args: List of extra args from optparse. | 71 args: List of extra args from optparse. |
| 70 option_parser: optparse.OptionParser object. | 72 option_parser: optparse.OptionParser object. |
| 71 | 73 |
| 72 Returns: | 74 Returns: |
| 73 An exit code. | 75 An exit code. |
| 74 """ | 76 """ |
| 75 if not (options.input_path and options.output_path): | 77 if not (options.input_path and options.output_path and |
| 78 options.coverage_file and options.sources_file): |
| 76 option_parser.error('All arguments are required.') | 79 option_parser.error('All arguments are required.') |
| 77 | 80 |
| 81 coverage_file = os.path.join(os.path.dirname(options.output_path), |
| 82 options.coverage_file) |
| 83 sources_file = os.path.join(os.path.dirname(options.output_path), |
| 84 options.sources_file) |
| 85 if os.path.exists(coverage_file): |
| 86 os.remove(coverage_file) |
| 87 if os.path.exists(sources_file): |
| 88 os.remove(sources_file) |
| 89 |
| 78 if os.path.isdir(options.input_path): | 90 if os.path.isdir(options.input_path): |
| 79 shutil.rmtree(options.output_path, ignore_errors=True) | 91 shutil.rmtree(options.output_path, ignore_errors=True) |
| 80 shutil.copytree(options.input_path, options.output_path) | 92 shutil.copytree(options.input_path, options.output_path) |
| 81 else: | 93 else: |
| 82 shutil.copy(options.input_path, options.output_path) | 94 shutil.copy(options.input_path, options.output_path) |
| 83 | 95 |
| 84 if options.stamp: | 96 if options.stamp: |
| 85 build_utils.Touch(options.stamp) | 97 build_utils.Touch(options.stamp) |
| 86 | 98 |
| 87 | 99 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 191 |
| 180 | 192 |
| 181 def main(argv): | 193 def main(argv): |
| 182 option_parser = command_option_parser.CommandOptionParser( | 194 option_parser = command_option_parser.CommandOptionParser( |
| 183 commands_dict=VALID_COMMANDS) | 195 commands_dict=VALID_COMMANDS) |
| 184 command_option_parser.ParseAndExecute(option_parser) | 196 command_option_parser.ParseAndExecute(option_parser) |
| 185 | 197 |
| 186 | 198 |
| 187 if __name__ == '__main__': | 199 if __name__ == '__main__': |
| 188 sys.exit(main(sys.argv)) | 200 sys.exit(main(sys.argv)) |
| OLD | NEW |