8cb3749cb3bd1f6e37ac01803104a87f581a06ab
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / gyp / jar.py
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 # pylint: disable=F0401
7
8 import fnmatch
9 import optparse
10 import os
11 import sys
12
13 from util import build_utils
14 from util import md5_check
15
16
17 def DoJar(options):
18   class_files = build_utils.FindInDirectory(options.classes_dir, '*.class')
19   for exclude in options.excluded_classes.split():
20     class_files = [f for f in class_files if not fnmatch.fnmatch(f, exclude)]
21
22   jar_path = os.path.abspath(options.jar_path)
23
24   # The paths of the files in the jar will be the same as they are passed in to
25   # the command. Because of this, the command should be run in
26   # options.classes_dir so the .class file paths in the jar are correct.
27   jar_cwd = options.classes_dir
28   class_files_rel = [os.path.relpath(f, jar_cwd) for f in class_files]
29   jar_cmd = ['jar', 'cf0', jar_path] + class_files_rel
30
31   record_path = '%s.md5.stamp' % options.jar_path
32   md5_check.CallAndRecordIfStale(
33       lambda: build_utils.CheckCallDie(jar_cmd, cwd=jar_cwd),
34       record_path=record_path,
35       input_paths=class_files,
36       input_strings=jar_cmd)
37
38   build_utils.Touch(options.jar_path)
39
40
41 def main():
42   parser = optparse.OptionParser()
43   parser.add_option('--classes-dir', help='Directory containing .class files.')
44   parser.add_option('--jar-path', help='Jar output path.')
45   parser.add_option('--excluded-classes',
46       help='List of .class file patterns to exclude from the jar.')
47   parser.add_option('--stamp', help='Path to touch on success.')
48
49   options, _ = parser.parse_args()
50
51   DoJar(options)
52
53   if options.stamp:
54     build_utils.Touch(options.stamp)
55
56
57 if __name__ == '__main__':
58   sys.exit(main())
59