OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
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. | |
8 | |
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 | |
11 call one of the instrument commands, or the copy command. | |
12 | |
13 Possible commands are: | |
14 - instrument_jar: Accepts a jar and instruments it using emma.jar. | |
15 - instrument_classes: Accepts a directory contains java classes and instruments | |
16 it using emma.jar. | |
17 - copy: Triggered instead of an instrumentation command when we don't have EMMA | |
18 coverage enabled. This allows us to make this a required step without | |
19 necessarily instrumenting on every build. | |
20 """ | |
21 | |
22 import collections | |
23 import json | |
24 import os | |
25 import shutil | |
26 import sys | |
27 | |
28 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | |
29 from pylib.utils import command_option_parser | |
30 | |
31 from util import build_utils | |
32 | |
33 | |
34 def _AddCommonOptions(option_parser): | |
35 """Adds common options to |option_parser|.""" | |
36 option_parser.add_option('--input-path', | |
37 help=('Path to input file(s). Either the classes ' | |
38 'directory, or the path to a jar.')) | |
39 option_parser.add_option('--output-path', | |
40 help=('Path to output final file(s) to. Either the ' | |
41 'final classes directory, or the directory in ' | |
42 'which to place the instrumented/copied jar.')) | |
43 option_parser.add_option('--stamp', help='Path to touch when done.') | |
44 | |
45 | |
46 def _AddInstrumentOptions(option_parser): | |
47 """Adds options related to instrumentation to |option_parser|.""" | |
48 _AddCommonOptions(option_parser) | |
49 option_parser.add_option('--coverage-file', | |
50 help='File to create with coverage metadata') | |
51 option_parser.add_option('--sources-file', | |
52 help='File to create with the list of sources.') | |
53 option_parser.add_option('--sources', | |
54 help='Space separated list of sources.') | |
55 option_parser.add_option('--src-root', | |
56 help='Root of the src repository.') | |
57 option_parser.add_option('--sdk-root', | |
58 help='Root of the Android SDK.') | |
59 option_parser.add_option('--intermediate-dir', | |
60 help=('Intermediate directory in which the ' | |
61 'instrumented jar will be produced in.')) | |
62 | |
63 | |
64 def _RunCopyCommand(command, options, args, option_parser): | |
65 """Just copies the jar from input to output locations. | |
66 | |
67 Args: | |
68 command: String indicating the command that was received to trigger | |
69 this function. | |
70 options: optparse options dictionary. | |
71 args: List of extra args from optparse. | |
72 option_parser: optparse.OptionParser object. | |
73 | |
74 Returns: | |
75 An exit code. | |
76 """ | |
77 if not (options.input_path and options.output_path): | |
78 option_parser.error('All arguments are required.') | |
79 | |
80 if os.path.isdir(options.input_path): | |
81 shutil.rmtree(options.output_path, ignore_errors=True) | |
82 shutil.copytree(options.input_path, options.output_path) | |
83 else: | |
84 shutil.copy(options.input_path, options.output_path) | |
85 | |
86 if options.stamp: | |
87 build_utils.Touch(options.stamp) | |
88 | |
89 | |
90 def _CreateSourcesFile(sources_string, sources_file, src_root): | |
91 """Adds all normalized source directories to |sources_file|. | |
92 | |
93 Args: | |
94 sources_string: String generated from gyp containing the list of sources. | |
95 sources_file: File into which to write the JSON list of sources. | |
96 src_root: Root which sources added to the file should be relative to. | |
97 | |
98 Returns: | |
99 An exit code. | |
100 """ | |
101 src_root = os.path.abspath(src_root) | |
102 sources = build_utils.ParseGypList(sources_string) | |
103 relative_sources = [] | |
104 for s in sources: | |
105 abs_source = os.path.abspath(s) | |
106 if abs_source[:len(src_root)] != src_root: | |
107 print ('Error: found source directory not under repository root: %s %s' | |
108 % (abs_source, src_root)) | |
109 return 1 | |
110 rel_source = os.path.relpath(abs_source, src_root) | |
111 | |
112 relative_sources.append(rel_source) | |
113 | |
114 with open(sources_file, 'w') as f: | |
115 json.dump(relative_sources, f) | |
116 | |
117 | |
118 def _RunInstrumentCommand(command, options, args, option_parser): | |
119 """Instruments the classes/jar files using EMMA. | |
120 | |
121 Args: | |
122 command: 'instrument_jar' or 'instrument_classes'. This distinguishes | |
123 whether we copy the output from the created lib/ directory, or classes/ | |
124 directory. | |
125 options: optparse options dictionary. | |
126 args: List of extra args from optparse. | |
127 option_parser: optparse.OptionParser object. | |
128 | |
129 Returns: | |
130 An exit code. | |
131 """ | |
132 if not (options.input_path and options.output_path and | |
133 options.intermediate_dir and options.coverage_file and | |
134 options.sources_file and options.sources and options.src_root and | |
135 options.sdk_root): | |
136 option_parser.error('All arguments are required.') | |
137 | |
138 # TODO(gkanwar): Add '-ix' option to filter out useless classes. | |
139 build_utils.CheckCallDie(['java', '-cp', | |
140 os.path.join(options.sdk_root, 'tools', | |
141 'lib', 'emma.jar'), | |
cjhopman
2013/08/21 17:37:48
Nit: It would be better if this path were construc
gkanwar1
2013/08/21 18:28:32
Done.
| |
142 'emma', 'instr', | |
143 '-ip', options.input_path, | |
144 '-d', options.intermediate_dir, | |
145 '-out', options.coverage_file, | |
146 '-m', 'fullcopy'], suppress_output=True) | |
147 | |
148 if command == 'instrument_jar': | |
cjhopman
2013/08/21 17:37:48
It looks like the intermediate dir here does not n
gkanwar1
2013/08/21 18:28:32
Done.
| |
149 for jar in os.listdir(os.path.join(options.intermediate_dir, 'lib')): | |
150 shutil.copy(os.path.join(options.intermediate_dir, 'lib', jar), | |
151 options.output_path) | |
152 else: # 'instrument_classes' | |
153 if os.path.isdir(options.output_path): | |
154 shutil.rmtree(options.output_path, ignore_errors=True) | |
155 shutil.copytree(os.path.join(options.intermediate_dir, 'classes'), | |
156 options.output_path) | |
157 | |
158 _CreateSourcesFile(options.sources, options.sources_file, options.src_root) | |
159 | |
160 if options.stamp: | |
161 build_utils.Touch(options.stamp) | |
162 | |
163 return 0 | |
164 | |
165 | |
166 CommandFunctionTuple = collections.namedtuple( | |
167 'CommandFunctionTuple', ['add_options_func', 'run_command_func']) | |
168 VALID_COMMANDS = { | |
169 'copy': CommandFunctionTuple(_AddCommonOptions, | |
170 _RunCopyCommand), | |
171 'instrument_jar': CommandFunctionTuple(_AddInstrumentOptions, | |
172 _RunInstrumentCommand), | |
173 'instrument_classes': CommandFunctionTuple(_AddInstrumentOptions, | |
174 _RunInstrumentCommand), | |
175 } | |
176 | |
177 | |
178 def main(argv): | |
179 option_parser = command_option_parser.CommandOptionParser( | |
180 commands_dict=VALID_COMMANDS) | |
181 command_option_parser.ParseAndExecute(option_parser) | |
182 | |
183 | |
184 if __name__ == '__main__': | |
185 sys.exit(main(sys.argv)) | |
OLD | NEW |