Adam Michael | 732907f | 2016-11-16 04:48:58 +0000 | [diff] [blame] | 1 | # 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 | |
| 17 | The input jar will be opened as a zip file, and its entries will be copied into |
| 18 | a new zip file, excepting any which are suspected not to be resources |
| 19 | (e.g., Java class files, etc). |
| 20 | |
| 21 | Usage: |
| 22 | python resource_extractor.py <input jar> <output zip> |
| 23 | """ |
| 24 | |
| 25 | import sys |
| 26 | import zipfile |
| 27 | |
| 28 | USAGE = """Error, invalid arguments. |
| 29 | Usage: resource_extractor.py <input jar> <output zip>""" |
| 30 | |
| 31 | EXCLUDED_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 | |
| 47 | EXCLUDED_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 | |
| 56 | EXCLUDED_DIRECTORIES = ( |
| 57 | 'cvs', # CVS repository files |
| 58 | '.svn', # SVN repository files |
| 59 | 'sccs', # SourceSafe repository files |
| 60 | 'meta-inf' # jar metadata |
| 61 | ) |
| 62 | |
| 63 | |
| 64 | def 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 | |
| 90 | def ExtractResources(input_jar, output_zip): |
| 91 | for path in input_jar.namelist(): |
| 92 | if IsValidPath(path): |
Adam Michael | 1fe8e81 | 2016-11-16 21:25:01 +0000 | [diff] [blame] | 93 | output_zip.writestr(input_jar.getinfo(path), input_jar.read(path)) |
Adam Michael | 732907f | 2016-11-16 04:48:58 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def 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 | |
| 105 | if __name__ == '__main__': |
| 106 | main(sys.argv) |