Chromium Code Reviews| 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 """ Creates a TOC file from a Java jar. | |
|
nilesh
2013/04/12 01:09:52
New line after copyright.
cjhopman
2013/04/12 23:09:48
Done.
| |
| 7 | |
| 8 The TOC file contains the non-package API of the jar. This includes all | |
| 9 public/protected classes/functions/members and the values of static final | |
| 10 variables. Some other information (major/minor javac version) is also included. | |
| 11 | |
| 12 This TOC file then can be used to determine if a dependent library should be | |
| 13 rebuilt when this jar changes. I.e. any change to the jar that would require a | |
| 14 rebuild, will have a corresponding change in the TOC file. | |
| 15 """ | |
| 16 | |
| 17 import optparse | |
| 18 import os | |
| 19 import re | |
| 20 import sys | |
| 21 import zipfile | |
| 22 | |
| 23 from util import build_utils | |
| 24 from util import md5_check | |
| 25 | |
| 26 def CreateToc(jar_path, toc_path): | |
| 27 jar = zipfile.ZipFile(jar_path) | |
| 28 classes = [] | |
| 29 files = jar.namelist() | |
| 30 for f in files: | |
| 31 if f.endswith('.class'): | |
| 32 # f is of the form org/chromium/base/Class$Inner.class | |
| 33 classes.append(f.replace('/', '.')[:-6]) | |
| 34 | |
| 35 javap_cmd = [ | |
| 36 'javap', | |
| 37 '-public', | |
| 38 '-protected', | |
| 39 # -verbose is required to get constant values (which can be inlined in | |
| 40 # dependents). | |
| 41 '-verbose', | |
| 42 '-classpath', jar_path | |
| 43 ] + classes | |
| 44 output = build_utils.CheckCallDie(javap_cmd, suppress_output=True) | |
| 45 | |
| 46 # javap output is structured by indent (2-space) levels. | |
| 47 good_patterns = [ | |
| 48 '^[^ ]', # This includes all class/function/member signatures. | |
| 49 '^ SourceFile:', | |
| 50 '^ minor version:', | |
| 51 '^ major version:', | |
| 52 '^ Constant value:', | |
| 53 ] | |
| 54 bad_patterns = [ | |
| 55 '^const #', # Matches the constant pool (i.e. literals used in the class). | |
| 56 ] | |
| 57 def JavapFilter(line): | |
|
nilesh
2013/04/12 01:09:52
newline before def
cjhopman
2013/04/12 23:09:48
Done.
| |
| 58 return (re.match('|'.join(good_patterns), line) and | |
| 59 not re.match('|'.join(bad_patterns), line)) | |
| 60 filtered_output = filter(JavapFilter, output.split('\n')) | |
| 61 | |
| 62 with open(toc_path, 'w') as tocfile: | |
| 63 tocfile.write('\n'.join(filtered_output)) | |
| 64 | |
| 65 | |
| 66 def DoJarToc(options): | |
| 67 jar_path = options.jar_path | |
| 68 toc_path = jar_path + '.TOC' | |
| 69 md5_stamp = '%s.md5' % toc_path | |
|
nilesh
2013/04/12 01:09:52
md5_stamp_path
cjhopman
2013/04/12 23:09:48
Done.
| |
| 70 md5_checker = md5_check.Md5Checker(stamp=md5_stamp, inputs=[jar_path]) | |
| 71 if md5_checker.IsStale(): | |
|
nilesh
2013/04/12 01:09:52
Add comment:
# Update the toc file and timestamp.
cjhopman
2013/04/12 23:09:48
Done... kind of. Renamed function to UpdateToc(...
| |
| 72 CreateToc(jar_path, toc_path) | |
| 73 else: | |
| 74 build_utils.Touch(toc_path) | |
| 75 md5_checker.Write() | |
|
nilesh
2013/04/12 01:09:52
Do this only if isstale
cjhopman
2013/04/12 23:09:48
Done.
| |
| 76 | |
| 77 | |
| 78 def main(argv): | |
| 79 parser = optparse.OptionParser() | |
| 80 parser.add_option('--jar-path', help='Input .jar path.') | |
| 81 parser.add_option('--toc-path', help='Output .jar.TOC path.') | |
|
nilesh
2013/04/12 01:09:52
where is options.toc_path used? I see that you com
cjhopman
2013/04/12 23:09:48
It's now used, it should not have been computed fr
| |
| 82 parser.add_option('--stamp', help='Path to touch on success.') | |
| 83 | |
| 84 # TODO(newt): remove this once http://crbug.com/177552 is fixed in ninja. | |
| 85 parser.add_option('--ignore', help='Ignored.') | |
| 86 | |
| 87 options, _ = parser.parse_args() | |
| 88 | |
| 89 DoJarToc(options) | |
| 90 | |
| 91 if options.stamp: | |
| 92 build_utils.Touch(options.stamp) | |
| 93 | |
| 94 | |
| 95 if __name__ == '__main__': | |
| 96 sys.exit(main(sys.argv)) | |
| 97 | |
| OLD | NEW |