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.
11 from util
import build_utils
14 def Jar(class_files
, classes_dir
, jar_path
, manifest_file
=None):
15 jar_path
= os
.path
.abspath(jar_path
)
17 # The paths of the files in the jar will be the same as they are passed in to
18 # the command. Because of this, the command should be run in
19 # options.classes_dir so the .class file paths in the jar are correct.
21 class_files_rel
= [os
.path
.relpath(f
, jar_cwd
) for f
in class_files
]
22 jar_cmd
= ['jar', 'cf0', jar_path
]
25 jar_cmd
.append(os
.path
.abspath(manifest_file
))
26 jar_cmd
.extend(class_files_rel
)
28 with build_utils
.TempDir() as temp_dir
:
29 empty_file
= os
.path
.join(temp_dir
, '.empty')
30 build_utils
.Touch(empty_file
)
31 jar_cmd
.append(os
.path
.relpath(empty_file
, jar_cwd
))
32 build_utils
.CheckOutput(jar_cmd
, cwd
=jar_cwd
)
33 build_utils
.Touch(jar_path
, fail_if_missing
=True)
36 def JarDirectory(classes_dir
, excluded_classes
, jar_path
, manifest_file
=None):
37 class_files
= build_utils
.FindInDirectory(classes_dir
, '*.class')
38 class_files
= [f
for f
in class_files
39 if not build_utils
.MatchesGlob(f
, excluded_classes
)]
41 Jar(class_files
, classes_dir
, jar_path
, manifest_file
=manifest_file
)
45 parser
= optparse
.OptionParser()
46 parser
.add_option('--classes-dir', help='Directory containing .class files.')
47 parser
.add_option('--jar-path', help='Jar output path.')
48 parser
.add_option('--excluded-classes',
49 help='List of .class file patterns to exclude from the jar.')
50 parser
.add_option('--stamp', help='Path to touch on success.')
52 options
, _
= parser
.parse_args()
54 if options
.excluded_classes
:
55 excluded_classes
= build_utils
.ParseGypList(options
.excluded_classes
)
58 JarDirectory(options
.classes_dir
,
63 build_utils
.Touch(options
.stamp
)
66 if __name__
== '__main__':