blob: 2cd11d907c2809d0b49d76d4b189a6f9852c8be4 [file] [log] [blame]
Adam Michael732907f2016-11-16 04:48:58 +00001# Copyright 2016 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Script to extract resources from a jar and put them into a separate zip file.
16
17The input jar will be opened as a zip file, and its entries will be copied into
18a new zip file, excepting any which are suspected not to be resources
19 (e.g., Java class files, etc).
20
21Usage:
22 python resource_extractor.py <input jar> <output zip>
23"""
24
25import sys
26import zipfile
27
28USAGE = """Error, invalid arguments.
29Usage: resource_extractor.py <input jar> <output zip>"""
30
31EXCLUDED_EXTENSIONS = (
32 '.aidl', # Android interface definition files
33 '.rs', # RenderScript files
34 '.fs', # FilterScript files
35 '.rsh', # RenderScript header files
36 '.d', # Dependency files
37 '.java', # Java source files
38 '.scala', # Scala source files
39 '.class', # Java class files
40 '.scc', # Visual SourceSafe
41 '.swp', # vi swap file
42 '.gwt.xml', # Google Web Toolkit modules
43 '~', # backup files
44 '/', # empty directory entries
45)
46
47EXCLUDED_FILENAMES = (
48 'thumbs.db', # image index file
49 'picasa.ini', # image index file
50 'package.html', # Javadoc
51 'overview.html', # Javadoc
52 'protobuf.meta', # protocol buffer metadata
53 'flags.xml', # Google flags metadata
54)
55
56EXCLUDED_DIRECTORIES = (
57 'cvs', # CVS repository files
58 '.svn', # SVN repository files
59 'sccs', # SourceSafe repository files
60 'meta-inf' # jar metadata
61)
62
63
64def IsValidPath(path):
65 """Checks if the provided path describes a resource.
66
67 Args:
68 path: the path to check
69
70 Returns:
71 True if the path is a resource.
72 """
73 path = path.lower()
74 if any(path.endswith(extension) for extension in EXCLUDED_EXTENSIONS):
75 return False
76
77 segments = path.split('/')
78 filename = segments[-1]
79 if filename.startswith('.') or filename in EXCLUDED_FILENAMES:
80 return False
81
82 dirs = segments[:-1]
83 # allow META-INF/services at the root to support ServiceLoader
84 if dirs[:2] == ['meta-inf', 'services']:
85 return True
86
87 return not any(dir in EXCLUDED_DIRECTORIES for dir in dirs)
88
89
90def ExtractResources(input_jar, output_zip):
91 for path in input_jar.namelist():
92 if IsValidPath(path):
Adam Michael1fe8e812016-11-16 21:25:01 +000093 output_zip.writestr(input_jar.getinfo(path), input_jar.read(path))
Adam Michael732907f2016-11-16 04:48:58 +000094
95
96def main(argv):
97 if len(argv) != 3:
98 print USAGE
99 sys.exit(1)
100 with zipfile.ZipFile(argv[1], 'r') as input_jar:
101 with zipfile.ZipFile(argv[2], 'w') as output_zip:
102 ExtractResources(input_jar, output_zip)
103
104
105if __name__ == '__main__':
106 main(sys.argv)