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