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 import optparse | 7 import optparse |
8 import os | 8 import os |
9 import shutil | 9 import shutil |
10 import sys | 10 import sys |
11 | 11 |
12 from util import build_utils | 12 from util import build_utils |
13 | 13 |
14 | 14 |
15 _RESOURCE_CLASSES = [ | 15 _RESOURCE_CLASSES = [ |
16 "R.class", | 16 "R.class", |
17 "R##*.class", | 17 "R##*.class", |
18 "Manifest.class", | 18 "Manifest.class", |
19 "Manifest##*.class", | 19 "Manifest##*.class", |
20 ] | 20 ] |
21 | 21 |
22 | 22 |
23 def Jar(class_files, classes_dir, jar_path, manifest_file=None, | 23 def Jar(class_files, classes_dir, jar_path, manifest_file=None, |
24 provider_configurations=None): | 24 provider_configurations=None, additional_files=None): |
25 jar_path = os.path.abspath(jar_path) | 25 jar_path = os.path.abspath(jar_path) |
26 | 26 |
27 # The paths of the files in the jar will be the same as they are passed in to | 27 # The paths of the files in the jar will be the same as they are passed in to |
28 # the command. Because of this, the command should be run in | 28 # the command. Because of this, the command should be run in |
29 # options.classes_dir so the .class file paths in the jar are correct. | 29 # options.classes_dir so the .class file paths in the jar are correct. |
30 jar_cwd = classes_dir | 30 jar_cwd = classes_dir |
31 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] | 31 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] |
32 jar_cmd = ['jar', 'cf0', jar_path] | 32 jar_cmd = ['jar', 'cf0', jar_path] |
33 if manifest_file: | 33 if manifest_file: |
34 jar_cmd[1] += 'm' | 34 jar_cmd[1] += 'm' |
35 jar_cmd.append(os.path.abspath(manifest_file)) | 35 jar_cmd.append(os.path.abspath(manifest_file)) |
36 jar_cmd.extend(class_files_rel) | 36 jar_cmd.extend(class_files_rel) |
37 | 37 |
38 if additional_files: | |
jbudorick
2016/07/21 00:12:40
optional nit: you could eliminate this by doing
mikecase (-- gone --)
2016/07/21 16:32:37
oooh, I like this style. Done
| |
39 for filepath, jar_filepath in additional_files: | |
40 full_jar_filepath = os.path.join(jar_cwd, jar_filepath) | |
41 jar_dir = os.path.dirname(full_jar_filepath) | |
42 if not os.path.exists(jar_dir): | |
43 os.makedirs(jar_dir) | |
44 shutil.copy(filepath, full_jar_filepath) | |
45 jar_cmd.append(jar_filepath) | |
46 | |
38 if provider_configurations: | 47 if provider_configurations: |
39 service_dir = os.path.join(jar_cwd, 'META-INF', 'services') | 48 service_dir = os.path.join(jar_cwd, 'META-INF', 'services') |
40 if not os.path.exists(service_dir): | 49 if not os.path.exists(service_dir): |
41 os.makedirs(service_dir) | 50 os.makedirs(service_dir) |
42 for config in provider_configurations: | 51 for config in provider_configurations: |
43 config_jar_path = os.path.join(service_dir, os.path.basename(config)) | 52 config_jar_path = os.path.join(service_dir, os.path.basename(config)) |
44 shutil.copy(config, config_jar_path) | 53 shutil.copy(config, config_jar_path) |
45 jar_cmd.append(os.path.relpath(config_jar_path, jar_cwd)) | 54 jar_cmd.append(os.path.relpath(config_jar_path, jar_cwd)) |
46 | 55 |
47 if not class_files_rel: | 56 if not class_files_rel: |
48 empty_file = os.path.join(classes_dir, '.empty') | 57 empty_file = os.path.join(classes_dir, '.empty') |
49 build_utils.Touch(empty_file) | 58 build_utils.Touch(empty_file) |
50 jar_cmd.append(os.path.relpath(empty_file, jar_cwd)) | 59 jar_cmd.append(os.path.relpath(empty_file, jar_cwd)) |
51 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) | 60 build_utils.CheckOutput(jar_cmd, cwd=jar_cwd) |
52 build_utils.Touch(jar_path, fail_if_missing=True) | 61 build_utils.Touch(jar_path, fail_if_missing=True) |
53 | 62 |
54 | 63 |
55 def JarDirectory(classes_dir, jar_path, manifest_file=None, predicate=None, | 64 def JarDirectory(classes_dir, jar_path, manifest_file=None, predicate=None, |
56 provider_configurations=None): | 65 provider_configurations=None, additional_files=None): |
57 class_files = build_utils.FindInDirectory(classes_dir, '*.class') | 66 class_files = build_utils.FindInDirectory(classes_dir, '*.class') |
58 if predicate: | 67 if predicate: |
59 class_files = [f for f in class_files if predicate(f)] | 68 class_files = [f for f in class_files if predicate(f)] |
60 | 69 |
61 Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file, | 70 Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file, |
62 provider_configurations=provider_configurations) | 71 provider_configurations=provider_configurations, |
72 additional_files=additional_files) | |
63 | 73 |
64 | 74 |
65 def main(): | 75 def main(): |
66 parser = optparse.OptionParser() | 76 parser = optparse.OptionParser() |
67 parser.add_option('--classes-dir', help='Directory containing .class files.') | 77 parser.add_option('--classes-dir', help='Directory containing .class files.') |
68 parser.add_option('--input-jar', help='Jar to include .class files from') | 78 parser.add_option('--input-jar', help='Jar to include .class files from') |
69 parser.add_option('--jar-path', help='Jar output path.') | 79 parser.add_option('--jar-path', help='Jar output path.') |
70 parser.add_option('--excluded-classes', | 80 parser.add_option('--excluded-classes', |
71 help='GYP list of .class file patterns to exclude from the jar.') | 81 help='GYP list of .class file patterns to exclude from the jar.') |
72 parser.add_option('--strip-resource-classes-for', | 82 parser.add_option('--strip-resource-classes-for', |
(...skipping 25 matching lines...) Expand all Loading... | |
98 classes_dir = temp_dir | 108 classes_dir = temp_dir |
99 JarDirectory(classes_dir, options.jar_path, predicate=predicate) | 109 JarDirectory(classes_dir, options.jar_path, predicate=predicate) |
100 | 110 |
101 if options.stamp: | 111 if options.stamp: |
102 build_utils.Touch(options.stamp) | 112 build_utils.Touch(options.stamp) |
103 | 113 |
104 | 114 |
105 if __name__ == '__main__': | 115 if __name__ == '__main__': |
106 sys.exit(main()) | 116 sys.exit(main()) |
107 | 117 |
OLD | NEW |