Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 1 | # pylint: disable=g-bad-file-header |
| 2 | # Copyright 2017 The Bazel Authors. All rights reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http:#www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | """Creates the embedded_tools.zip that is part of the Bazel binary.""" |
| 16 | |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame^] | 17 | import contextlib |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 18 | import fnmatch |
| 19 | import os |
| 20 | import os.path |
| 21 | import re |
| 22 | import stat |
| 23 | import sys |
| 24 | import tarfile |
| 25 | import zipfile |
| 26 | |
| 27 | output_paths = [ |
| 28 | ('*tools/jdk/BUILD*', lambda x: 'tools/jdk/BUILD'), |
| 29 | ('*tools/platforms/platforms.BUILD', lambda x: 'platforms/BUILD'), |
| 30 | ('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)), |
| 31 | ('*JavaBuilder*_deploy.jar', lambda x: 'tools/jdk/' + os.path.basename(x)), |
| 32 | ('*JacocoCoverage*_deploy.jar', |
| 33 | lambda x: 'tools/jdk/JacocoCoverage_deploy.jar'), |
| 34 | ('*turbine_deploy.jar', lambda x: 'tools/jdk/turbine_deploy.jar'), |
| 35 | ('*javac-9-dev-r4023-2.jar', |
| 36 | lambda x: 'third_party/java/jdk/langtools/javac-9-dev-r4023-2.jar'), |
| 37 | ('*SingleJar_deploy.jar', |
| 38 | lambda x: 'tools/jdk/singlejar/SingleJar_deploy.jar'), |
| 39 | ('*GenClass_deploy.jar', lambda x: 'tools/jdk/GenClass_deploy.jar'), |
| 40 | ('*ExperimentalRunner_deploy.jar', |
| 41 | lambda x: 'tools/jdk/ExperimentalTestRunner_deploy.jar'), |
| 42 | ('*Runner_deploy.jar', lambda x: 'tools/jdk/TestRunner_deploy.jar'), |
| 43 | ('*singlejar', lambda x: 'tools/jdk/singlejar/singlejar'), |
| 44 | ('*launcher.exe', lambda x: 'tools/launcher/launcher.exe'), |
| 45 | ('*ijar.exe', lambda x: 'tools/jdk/ijar/ijar.exe'), |
| 46 | ('*ijar', lambda x: 'tools/jdk/ijar/ijar'), |
| 47 | ('*zipper.exe', lambda x: 'tools/zip/zipper/zipper.exe'), |
| 48 | ('*zipper', lambda x: 'tools/zip/zipper/zipper'), |
| 49 | ('*src/objc_tools/*', |
| 50 | lambda x: 'tools/objc/precomp_' + os.path.basename(x)), |
| 51 | ('*xcode*StdRedirect.dylib', lambda x: 'tools/objc/StdRedirect.dylib'), |
| 52 | ('*xcode*make_hashed_objlist.py', |
| 53 | lambda x: 'tools/objc/make_hashed_objlist.py'), |
| 54 | ('*xcode*realpath', lambda x: 'tools/objc/realpath'), |
| 55 | ('*xcode*xcode-locator', lambda x: 'tools/objc/xcode-locator'), |
| 56 | ('*src/tools/xcode/*.sh', lambda x: 'tools/objc/' + os.path.basename(x)), |
| 57 | ('*src/tools/xcode/*', |
| 58 | lambda x: 'tools/objc/' + os.path.basename(x) + '.sh'), |
| 59 | ('*external/openjdk_*/file/*.tar.gz', lambda x: 'jdk.tar.gz'), |
| 60 | ('*external/openjdk_*/file/*.zip', lambda x: 'jdk.zip'), |
| 61 | ('*', lambda x: re.sub(r'^.*bazel-out/[^/]*/bin/', '', x, count=1)), |
| 62 | ] |
| 63 | |
| 64 | |
| 65 | def get_output_path(path): |
| 66 | for pattern, transformer in output_paths: |
| 67 | if fnmatch.fnmatch(path.replace('\\', '/'), pattern): |
| 68 | # BUILD.tools are stored as BUILD files. |
| 69 | return transformer(path).replace('/BUILD.tools', '/BUILD') |
| 70 | |
| 71 | |
| 72 | def is_mode_executable(mode): |
| 73 | return mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) > 0 |
| 74 | |
| 75 | |
| 76 | def is_executable(path): |
| 77 | return is_mode_executable(os.stat(path)[stat.ST_MODE]) |
| 78 | |
| 79 | |
| 80 | def get_input_files(argsfile): |
| 81 | """Returns a sorted list of tuples (archive_file, input_file). |
| 82 | |
| 83 | This describes the files that should be put into the generated archive. |
| 84 | |
| 85 | Args: |
| 86 | argsfile: The file containing the list of input files. |
| 87 | """ |
| 88 | with open(argsfile, 'r') as f: |
| 89 | input_files = set(x.strip() for x in f.readlines()) |
| 90 | |
| 91 | result = {} |
| 92 | for input_file in input_files: |
| 93 | # If we have both a BUILD and a BUILD.tools file, take the latter only. |
| 94 | if (os.path.basename(input_file) == 'BUILD' and |
| 95 | input_file + '.tools' in input_files): |
| 96 | continue |
| 97 | |
| 98 | # This gives us the same behavior as the older bash version of this |
| 99 | # tool: If two input files map to the same output files, the one that |
| 100 | # comes last in the list of input files overrides all earlier ones. |
| 101 | result[get_output_path(input_file)] = input_file |
| 102 | |
| 103 | # By sorting the file list, the resulting ZIP file will not be reproducible |
| 104 | # and deterministic. |
| 105 | return sorted(result.items()) |
| 106 | |
| 107 | |
| 108 | def copy_jdk_into_archive(output_zip, archive_file, input_file): |
| 109 | # The JDK is special - it's extracted instead of copied. |
| 110 | if archive_file.endswith('.tar.gz'): |
| 111 | with tarfile.open(input_file, 'r', errorlevel=2) as jdk_tar: |
| 112 | while True: |
| 113 | jdk_tarinfo = jdk_tar.next() |
| 114 | if jdk_tarinfo is None: |
| 115 | break |
| 116 | # Rename the first folder to 'jdk', because Bazel looks for a |
| 117 | # bundled JDK in the embedded tools using that folder name. |
| 118 | filename = 'jdk/' + '/'.join(jdk_tarinfo.name.split('/')[1:]) |
| 119 | zipinfo = zipfile.ZipInfo(filename, (1980, 1, 1, 0, 0, 0)) |
| 120 | if jdk_tarinfo.isreg(): |
| 121 | if is_mode_executable(jdk_tarinfo.mode): |
| 122 | zipinfo.external_attr = 0o755 << 16 |
| 123 | else: |
| 124 | zipinfo.external_attr = 0o644 << 16 |
| 125 | zipinfo.compress_type = zipfile.ZIP_DEFLATED |
| 126 | output_zip.writestr(zipinfo, jdk_tar.extractfile(jdk_tarinfo).read()) |
| 127 | elif jdk_tarinfo.issym(): |
| 128 | # 0120000 originally comes from the definition of S_IFLNK and |
| 129 | # marks a symbolic link in the Zip file format. |
| 130 | zipinfo.external_attr = 0o120000 << 16 |
| 131 | output_zip.writestr(zipinfo, jdk_tarinfo.linkname) |
| 132 | else: |
| 133 | # Ignore directories, hard links, special files, ... |
| 134 | pass |
| 135 | elif archive_file.endswith('.zip'): |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame^] | 136 | # Adding contextlib.closing to be python 2.6 (for centos 6.7) compatible |
| 137 | with contextlib.closing(zipfile.ZipFile(input_file, 'r')) as jdk_zip: |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 138 | for jdk_zipinfo in jdk_zip.infolist(): |
| 139 | # Rename the first folder to 'jdk', because Bazel looks for a |
| 140 | # bundled JDK in the embedded tools using that folder name. |
| 141 | filename = 'jdk/' + '/'.join(jdk_zipinfo.filename.split('/')[1:]) |
| 142 | zipinfo = zipfile.ZipInfo(filename, (1980, 1, 1, 0, 0, 0)) |
| 143 | if is_mode_executable(jdk_zipinfo.external_attr >> 16 & 0xFFFF): |
| 144 | zipinfo.external_attr = 0o755 << 16 |
| 145 | else: |
| 146 | zipinfo.external_attr = 0o644 << 16 |
| 147 | zipinfo.compress_type = jdk_zipinfo.compress_type |
| 148 | output_zip.writestr(zipinfo, jdk_zip.read(jdk_zipinfo)) |
| 149 | |
| 150 | |
| 151 | def main(): |
| 152 | output_zip = os.path.join(os.getcwd(), sys.argv[1]) |
| 153 | input_files = get_input_files(sys.argv[2]) |
| 154 | |
| 155 | # Copy all the input_files into output_zip. |
hlopko | 5a661c7 | 2017-08-09 12:03:03 +0200 | [diff] [blame^] | 156 | # Adding contextlib.closing to be python 2.6 (for centos 6.7) compatible |
| 157 | with contextlib.closing( |
| 158 | zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED)) as output_zip: |
Philipp Wollermann | 4c55898 | 2017-07-27 18:01:12 +0200 | [diff] [blame] | 159 | zipinfo = zipfile.ZipInfo('WORKSPACE', (1980, 1, 1, 0, 0, 0)) |
| 160 | zipinfo.external_attr = 0o644 << 16 |
| 161 | output_zip.writestr(zipinfo, 'workspace(name = "bazel_tools")\n') |
| 162 | |
| 163 | zipinfo = zipfile.ZipInfo('tools/defaults/BUILD', (1980, 1, 1, 0, 0, 0)) |
| 164 | zipinfo.external_attr = 0o644 << 16 |
| 165 | output_zip.writestr(zipinfo, '') |
| 166 | |
| 167 | for archive_file, input_file in input_files: |
| 168 | if os.path.basename(archive_file) in ('jdk.tar.gz', 'jdk.zip'): |
| 169 | copy_jdk_into_archive(output_zip, archive_file, input_file) |
| 170 | else: |
| 171 | zipinfo = zipfile.ZipInfo(archive_file, (1980, 1, 1, 0, 0, 0)) |
| 172 | zipinfo.external_attr = 0o755 << 16 if is_executable( |
| 173 | input_file) else 0o644 << 16 |
| 174 | zipinfo.compress_type = zipfile.ZIP_DEFLATED |
| 175 | with open(input_file, 'rb') as f: |
| 176 | output_zip.writestr(zipinfo, f.read()) |
| 177 | |
| 178 | |
| 179 | if __name__ == '__main__': |
| 180 | main() |