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.
12 from util
import build_utils
13 from util
import md5_check
16 def Jar(class_files
, classes_dir
, jar_path
, manifest_file
=None):
17 jar_path
= os
.path
.abspath(jar_path
)
19 # 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
21 # options.classes_dir so the .class file paths in the jar are correct.
23 class_files_rel
= [os
.path
.relpath(f
, jar_cwd
) for f
in class_files
]
24 jar_cmd
= ['jar', 'cf0', jar_path
]
27 jar_cmd
.append(os
.path
.abspath(manifest_file
))
28 jar_cmd
.extend(class_files_rel
)
30 with build_utils
.TempDir() as temp_dir
:
31 empty_file
= os
.path
.join(temp_dir
, '.empty')
32 build_utils
.Touch(empty_file
)
33 jar_cmd
.append(os
.path
.relpath(empty_file
, jar_cwd
))
34 record_path
= '%s.md5.stamp' % jar_path
35 md5_check
.CallAndRecordIfStale(
36 lambda: build_utils
.CheckOutput(jar_cmd
, cwd
=jar_cwd
),
37 record_path
=record_path
,
38 input_paths
=class_files
,
39 input_strings
=jar_cmd
,
40 force
=not os
.path
.exists(jar_path
),
43 build_utils
.Touch(jar_path
, fail_if_missing
=True)
46 def JarDirectory(classes_dir
, excluded_classes
, jar_path
, manifest_file
=None):
47 class_files
= build_utils
.FindInDirectory(classes_dir
, '*.class')
48 for exclude
in excluded_classes
:
50 lambda f
: not fnmatch
.fnmatch(f
, exclude
), class_files
)
52 Jar(class_files
, classes_dir
, jar_path
, manifest_file
=manifest_file
)
56 parser
= optparse
.OptionParser()
57 parser
.add_option('--classes-dir', help='Directory containing .class files.')
58 parser
.add_option('--jar-path', help='Jar output path.')
59 parser
.add_option('--excluded-classes',
60 help='List of .class file patterns to exclude from the jar.')
61 parser
.add_option('--stamp', help='Path to touch on success.')
63 options
, _
= parser
.parse_args()
65 if options
.excluded_classes
:
66 excluded_classes
= build_utils
.ParseGypList(options
.excluded_classes
)
69 JarDirectory(options
.classes_dir
,
74 build_utils
.Touch(options
.stamp
)
77 if __name__
== '__main__':