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 fnmatch | 7 import fnmatch |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 | 11 |
12 from util import build_utils | 12 from util import build_utils |
13 from util import md5_check | 13 from util import md5_check |
14 | 14 |
15 | 15 def Jar(class_files, classes_dir, jar_path): |
16 def Jar(class_files, classes_dir, jar_path, manifest_file=None): | |
17 jar_path = os.path.abspath(jar_path) | 16 jar_path = os.path.abspath(jar_path) |
18 | 17 |
19 # The paths of the files in the jar will be the same as they are passed in to | 18 # The paths of the files in the jar will be the same as they are passed in to |
20 # the command. Because of this, the command should be run in | 19 # the command. Because of this, the command should be run in |
21 # options.classes_dir so the .class file paths in the jar are correct. | 20 # options.classes_dir so the .class file paths in the jar are correct. |
22 jar_cwd = classes_dir | 21 jar_cwd = classes_dir |
23 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] | 22 class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files] |
24 jar_cmd = ['jar', 'cf0', jar_path] | 23 jar_cmd = ['jar', 'cf0', jar_path] + class_files_rel |
25 if manifest_file: | |
26 jar_cmd[1] += 'm' | |
27 jar_cmd.append(os.path.abspath(manifest_file)) | |
28 jar_cmd.extend(class_files_rel) | |
29 | 24 |
30 record_path = '%s.md5.stamp' % jar_path | 25 record_path = '%s.md5.stamp' % jar_path |
31 md5_check.CallAndRecordIfStale( | 26 md5_check.CallAndRecordIfStale( |
32 lambda: build_utils.CheckOutput(jar_cmd, cwd=jar_cwd), | 27 lambda: build_utils.CheckOutput(jar_cmd, cwd=jar_cwd), |
33 record_path=record_path, | 28 record_path=record_path, |
34 input_paths=class_files, | 29 input_paths=class_files, |
35 input_strings=jar_cmd, | 30 input_strings=jar_cmd, |
36 force=not os.path.exists(jar_path), | 31 force=not os.path.exists(jar_path), |
37 ) | 32 ) |
38 | 33 |
39 build_utils.Touch(jar_path, fail_if_missing=True) | 34 build_utils.Touch(jar_path, fail_if_missing=True) |
40 | 35 |
41 | 36 |
42 def JarDirectory(classes_dir, excluded_classes, jar_path, manifest_file=None): | 37 def JarDirectory(classes_dir, excluded_classes, jar_path): |
43 class_files = build_utils.FindInDirectory(classes_dir, '*.class') | 38 class_files = build_utils.FindInDirectory(classes_dir, '*.class') |
44 for exclude in excluded_classes: | 39 for exclude in excluded_classes: |
45 class_files = filter( | 40 class_files = filter( |
46 lambda f: not fnmatch.fnmatch(f, exclude), class_files) | 41 lambda f: not fnmatch.fnmatch(f, exclude), class_files) |
47 | 42 |
48 Jar(class_files, classes_dir, jar_path, manifest_file=manifest_file) | 43 Jar(class_files, classes_dir, jar_path) |
49 | |
50 | 44 |
51 def main(): | 45 def main(): |
52 parser = optparse.OptionParser() | 46 parser = optparse.OptionParser() |
53 parser.add_option('--classes-dir', help='Directory containing .class files.') | 47 parser.add_option('--classes-dir', help='Directory containing .class files.') |
54 parser.add_option('--jar-path', help='Jar output path.') | 48 parser.add_option('--jar-path', help='Jar output path.') |
55 parser.add_option('--excluded-classes', | 49 parser.add_option('--excluded-classes', |
56 help='List of .class file patterns to exclude from the jar.') | 50 help='List of .class file patterns to exclude from the jar.') |
57 parser.add_option('--stamp', help='Path to touch on success.') | 51 parser.add_option('--stamp', help='Path to touch on success.') |
58 | 52 |
59 options, _ = parser.parse_args() | 53 options, _ = parser.parse_args() |
60 | 54 |
61 if options.excluded_classes: | |
62 excluded_classes = build_utils.ParseGypList(options.excluded_classes) | |
63 else: | |
64 excluded_classes = [] | |
65 JarDirectory(options.classes_dir, | 55 JarDirectory(options.classes_dir, |
66 excluded_classes, | 56 build_utils.ParseGypList(options.excluded_classes), |
67 options.jar_path) | 57 options.jar_path) |
68 | 58 |
69 if options.stamp: | 59 if options.stamp: |
70 build_utils.Touch(options.stamp) | 60 build_utils.Touch(options.stamp) |
71 | 61 |
72 | 62 |
73 if __name__ == '__main__': | 63 if __name__ == '__main__': |
74 sys.exit(main()) | 64 sys.exit(main()) |
75 | 65 |
OLD | NEW |