Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 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 import optparse | 7 import optparse |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import tempfile | 10 import tempfile |
| 11 | 11 |
| 12 REPOSITORY_ROOT = os.path.abspath(os.path.join( | 12 REPOSITORY_ROOT = os.path.abspath(os.path.join( |
| 13 os.path.dirname(__file__), '..', '..', '..')) | 13 os.path.dirname(__file__), '..', '..', '..')) |
| 14 | 14 |
| 15 sys.path.append(os.path.join(REPOSITORY_ROOT, 'build/android/gyp/util')) | 15 sys.path.append(os.path.join(REPOSITORY_ROOT, 'build/android/gyp/util')) |
| 16 import build_utils | 16 import build_utils |
| 17 | 17 |
| 18 | 18 |
| 19 def JarSources(src_dir, jar_path): | 19 def JarSources(src_dir, src_files, jar_path): |
| 20 # The paths of the files in the jar will be the same as they are passed in to | 20 # The paths of the files in the jar will be the same as they are passed in to |
| 21 # the command. Because of this, the command should be run in | 21 # the command. Because of this, the command should be run in |
| 22 # options.src_dir so the .java file paths in the jar are correct. | 22 # options.src_dir so the .java file paths in the jar are correct. |
| 23 jar_cwd = src_dir | 23 jar_cwd = src_dir |
| 24 jar_path = os.path.abspath(jar_path) | 24 jar_path = os.path.abspath(jar_path) |
| 25 if os.path.exists(jar_path): | 25 if os.path.exists(jar_path): |
| 26 jar_cmd = ['jar', 'uf', jar_path, '.'] | 26 jar_cmd = ['jar', 'uf', jar_path] |
| 27 else: | 27 else: |
| 28 jar_cmd = ['jar', 'cf', jar_path, '.'] | 28 jar_cmd = ['jar', 'cf', jar_path] |
| 29 | 29 |
| 30 jar_cmd.extend(src_files) | |
| 30 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) | 31 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) |
| 31 | 32 |
| 32 # Uncompress source jars so that they can be combined with other sources | 33 # Uncompress source jars so that they can be combined with other sources |
| 33 def UnzipSourceJar(jar, unzipped_jar_path): | 34 def UnzipSourceJar(jar, unzipped_jar_path): |
| 34 if os.path.exists(jar): | 35 if os.path.exists(jar): |
| 35 jar_cmd = ['jar', 'xf', os.path.abspath(jar)] | 36 jar_cmd = ['jar', 'xf', os.path.abspath(jar)] |
| 36 build_utils.CheckOutput(jar_cmd, cwd=unzipped_jar_path) | 37 build_utils.CheckOutput(jar_cmd, cwd=unzipped_jar_path) |
| 37 else: | 38 else: |
| 38 raise Exception('Jar file does not exist %s' % jar) | 39 raise Exception('Jar file does not exist %s' % jar) |
| 39 | 40 |
| 40 | 41 |
| 41 def main(): | 42 def main(): |
| 42 parser = optparse.OptionParser() | 43 parser = optparse.OptionParser() |
| 43 build_utils.AddDepfileOption(parser) | 44 build_utils.AddDepfileOption(parser) |
| 44 parser.add_option('--src-dir', action="append", | 45 parser.add_option('--src-search-dirs', action="append", |
| 45 help='Directory containing .java files.') | 46 help='A list of directories that should be searched ' |
| 47 'for the source files.') | |
| 48 parser.add_option('--src-files', action="append", | |
| 49 help='A list of source files to jar.') | |
| 46 parser.add_option('--src-jars', action="append", | 50 parser.add_option('--src-jars', action="append", |
| 47 help='A list of source jars to include in addition to source files.') | 51 help='A list of source jars to include in addition to source files.') |
| 48 parser.add_option('--jar-path', help='Jar output path.') | 52 parser.add_option('--jar-path', help='Jar output path.') |
| 49 parser.add_option('--stamp', help='Path to touch on success.') | 53 parser.add_option('--stamp', help='Path to touch on success.') |
| 50 | 54 |
| 51 options, _ = parser.parse_args() | 55 options, _ = parser.parse_args() |
| 52 | 56 |
| 53 # A temporary directory to put the output of jar files. | 57 # A temporary directory to put the output of jar files. |
| 54 unzipped_jar_path = None | 58 unzipped_jar_path = None |
| 55 generated_src_dirs = [] | |
| 56 if options.src_jars: | 59 if options.src_jars: |
| 57 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path)) | 60 unzipped_jar_path = tempfile.mkdtemp(dir=os.path.dirname(options.jar_path)) |
| 58 generated_src_dirs.append(unzipped_jar_path) | |
| 59 jar_list = [] | 61 jar_list = [] |
| 60 for gn_list in options.src_jars: | 62 for gn_list in options.src_jars: |
| 61 jar_list.extend(build_utils.ParseGnList(gn_list)) | 63 jar_list.extend(build_utils.ParseGnList(gn_list)) |
| 62 | 64 |
| 63 for jar in jar_list: | 65 for jar in jar_list: |
| 64 UnzipSourceJar(jar, unzipped_jar_path) | 66 UnzipSourceJar(jar, unzipped_jar_path) |
| 65 | 67 |
| 66 src_dirs = [] | 68 src_search_dirs = [] |
| 67 for src_dir in options.src_dir: | 69 for gn_src_search_dirs in options.src_search_dirs: |
| 68 src_dirs.extend(build_utils.ParseGnList(src_dir)) | 70 src_search_dirs.extend(build_utils.ParseGnList(gn_src_search_dirs)) |
| 69 | 71 |
| 70 for src_dir in src_dirs + generated_src_dirs: | 72 src_files = [] |
| 71 JarSources(src_dir, options.jar_path) | 73 for gn_src_files in options.src_files: |
| 74 src_files.extend(build_utils.ParseGnList(gn_src_files)) | |
| 75 | |
| 76 # Create a dictionary that maps every source directory | |
| 77 # to source files that it contains. | |
| 78 dir_to_files_map = {} | |
| 79 #Initialize the map | |
|
pauljensen
2016/09/16 18:38:39
space between "#" and "I"
xunjieli
2016/09/16 18:39:49
nit: leave a space after #.
kapishnikov
2016/09/16 20:28:27
Done.
kapishnikov
2016/09/16 20:28:27
Done.
| |
| 80 for src_search_dir in src_search_dirs: | |
| 81 dir_to_files_map[src_search_dir] = [] | |
| 82 # Fill the map | |
| 83 for src_file in src_files: | |
| 84 number_of_file_instances = 0 | |
| 85 for src_search_dir in src_search_dirs: | |
| 86 if os.path.isfile(os.path.join(src_search_dir, src_file)): | |
| 87 number_of_file_instances += 1 | |
| 88 dir_to_files_map[src_search_dir].append(src_file) | |
| 89 if (number_of_file_instances > 1): | |
| 90 raise Exception( | |
| 91 'There are more that one instance of file %s in %s' | |
| 92 % (src_file, src_search_dirs)) | |
| 93 if (number_of_file_instances < 1): | |
| 94 raise Exception( | |
| 95 'Unable to find file %s in %s' % (src_file, src_search_dirs)) | |
| 96 | |
| 97 # Jar the sources from every source search directory | |
| 98 for src_search_dir in src_search_dirs: | |
| 99 if len(dir_to_files_map[src_search_dir]) > 0: | |
| 100 JarSources(src_search_dir, dir_to_files_map[src_search_dir], | |
| 101 options.jar_path) | |
| 102 else: | |
| 103 raise Exception( | |
| 104 'Directory %s does not contain any files and can be' | |
| 105 'removes from the search path' % src_search_dir) | |
|
pauljensen
2016/09/16 18:38:39
removes->removed
search path->list of directories
kapishnikov
2016/09/16 20:28:27
Done.
| |
| 106 | |
| 107 # Jar additional src jars | |
| 108 if unzipped_jar_path: | |
| 109 JarSources(unzipped_jar_path, ['.'], options.jar_path) | |
| 72 | 110 |
| 73 if options.depfile: | 111 if options.depfile: |
| 74 deps = [] | 112 deps = [] |
| 75 for src_dir in src_dirs: | 113 for src_dir in src_search_dirs: |
| 76 for root, _, filenames in os.walk(src_dir): | 114 for root, _, filenames in os.walk(src_dir): |
| 77 deps.extend(os.path.join(root, f) for f in filenames) | 115 deps.extend(os.path.join(root, f) for f in filenames) |
| 78 # Srcjar deps already captured in GN rules (no need to list them here). | 116 # Srcjar deps already captured in GN rules (no need to list them here). |
| 79 build_utils.WriteDepfile(options.depfile, options.jar_path, deps) | 117 build_utils.WriteDepfile(options.depfile, options.jar_path, deps) |
| 80 | 118 |
| 81 # Clean up temporary output directory. | 119 # Clean up temporary output directory. |
| 82 if unzipped_jar_path: | 120 if unzipped_jar_path: |
| 83 build_utils.DeleteDirectory(unzipped_jar_path) | 121 build_utils.DeleteDirectory(unzipped_jar_path) |
| 84 | 122 |
| 85 if options.stamp: | 123 if options.stamp: |
| 86 build_utils.Touch(options.stamp) | 124 build_utils.Touch(options.stamp) |
| 87 | 125 |
| 88 if __name__ == '__main__': | 126 if __name__ == '__main__': |
| 89 sys.exit(main()) | 127 sys.exit(main()) |
| 90 | 128 |
| OLD | NEW |