blob: 7c21f7107fe3ac4266f9dfda83edb5366e4679d1 [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
Akira Baruah408ea372017-12-18 10:09:44 -080025from __future__ import print_function
26
Adam Michael732907f2016-11-16 04:48:58 +000027import sys
28import zipfile
29
30USAGE = """Error, invalid arguments.
31Usage: resource_extractor.py <input jar> <output zip>"""
32
33EXCLUDED_EXTENSIONS = (
34 '.aidl', # Android interface definition files
35 '.rs', # RenderScript files
36 '.fs', # FilterScript files
37 '.rsh', # RenderScript header files
38 '.d', # Dependency files
39 '.java', # Java source files
40 '.scala', # Scala source files
41 '.class', # Java class files
42 '.scc', # Visual SourceSafe
43 '.swp', # vi swap file
44 '.gwt.xml', # Google Web Toolkit modules
45 '~', # backup files
46 '/', # empty directory entries
47)
48
49EXCLUDED_FILENAMES = (
50 'thumbs.db', # image index file
51 'picasa.ini', # image index file
52 'package.html', # Javadoc
53 'overview.html', # Javadoc
54 'protobuf.meta', # protocol buffer metadata
55 'flags.xml', # Google flags metadata
56)
57
58EXCLUDED_DIRECTORIES = (
59 'cvs', # CVS repository files
60 '.svn', # SVN repository files
61 'sccs', # SourceSafe repository files
62 'meta-inf' # jar metadata
63)
64
65
66def IsValidPath(path):
67 """Checks if the provided path describes a resource.
68
69 Args:
70 path: the path to check
71
72 Returns:
73 True if the path is a resource.
74 """
75 path = path.lower()
76 if any(path.endswith(extension) for extension in EXCLUDED_EXTENSIONS):
77 return False
78
79 segments = path.split('/')
80 filename = segments[-1]
81 if filename.startswith('.') or filename in EXCLUDED_FILENAMES:
82 return False
83
84 dirs = segments[:-1]
85 # allow META-INF/services at the root to support ServiceLoader
86 if dirs[:2] == ['meta-inf', 'services']:
87 return True
88
89 return not any(dir in EXCLUDED_DIRECTORIES for dir in dirs)
90
91
92def ExtractResources(input_jar, output_zip):
93 for path in input_jar.namelist():
94 if IsValidPath(path):
Adam Michael1fe8e812016-11-16 21:25:01 +000095 output_zip.writestr(input_jar.getinfo(path), input_jar.read(path))
Adam Michael732907f2016-11-16 04:48:58 +000096
97
98def main(argv):
99 if len(argv) != 3:
Akira Baruah408ea372017-12-18 10:09:44 -0800100 print(USAGE)
Adam Michael732907f2016-11-16 04:48:58 +0000101 sys.exit(1)
102 with zipfile.ZipFile(argv[1], 'r') as input_jar:
103 with zipfile.ZipFile(argv[2], 'w') as output_zip:
104 ExtractResources(input_jar, output_zip)
105
106
107if __name__ == '__main__':
108 main(sys.argv)