Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2023 Google Inc. 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 | |
Cole Faust | 5a8f533 | 2024-03-28 17:07:26 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 17 | import argparse |
| 18 | import py_compile |
| 19 | import os |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 20 | import sys |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 21 | import shutil |
| 22 | import tempfile |
| 23 | import zipfile |
| 24 | |
| 25 | # This file needs to support both python 2 and 3. |
| 26 | |
| 27 | |
Anas Sulaiman | c755fdb | 2024-02-06 19:09:44 +0000 | [diff] [blame] | 28 | def process_one_file(name, infile, outzip): |
| 29 | # Create a ZipInfo instance with a fixed date to ensure a deterministic output. |
| 30 | # Date was chosen to be the same as |
| 31 | # https://cs.android.com/android/platform/superproject/main/+/main:build/soong/jar/jar.go;l=36;drc=2863e4535eb65e15f955dc8ed48fa99b1d2a1db5 |
| 32 | info = zipfile.ZipInfo(filename=name, date_time=(2008, 1, 1, 0, 0, 0)) |
Cole Faust | baae083 | 2024-04-29 16:47:36 -0700 | [diff] [blame] | 33 | info.compress_type = zipfile.ZIP_DEFLATED |
Anas Sulaiman | c755fdb | 2024-02-06 19:09:44 +0000 | [diff] [blame] | 34 | |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 35 | if not info.filename.endswith('.py'): |
| 36 | outzip.writestr(info, infile.read()) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 37 | return |
| 38 | |
| 39 | # Unfortunately py_compile requires the input/output files to be written |
| 40 | # out to disk. |
| 41 | with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 42 | shutil.copyfileobj(infile, tmp) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 43 | in_name = tmp.name |
| 44 | with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp: |
| 45 | out_name = tmp.name |
| 46 | try: |
Anas Sulaiman | c755fdb | 2024-02-06 19:09:44 +0000 | [diff] [blame] | 47 | # Ensure a deterministic .pyc output by using the hash rather than the timestamp. |
| 48 | # Only works on Python 3.7+ |
| 49 | # See https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 50 | if sys.version_info >= (3, 7): |
| 51 | py_compile.compile(in_name, out_name, info.filename, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH) |
| 52 | else: |
| 53 | py_compile.compile(in_name, out_name, info.filename, doraise=True) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 54 | with open(out_name, 'rb') as f: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 55 | info.filename = info.filename + 'c' |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame] | 56 | outzip.writestr(info, f.read()) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 57 | finally: |
| 58 | os.remove(in_name) |
| 59 | os.remove(out_name) |
| 60 | |
| 61 | |
| 62 | def main(): |
| 63 | parser = argparse.ArgumentParser() |
| 64 | parser.add_argument('src_zip') |
| 65 | parser.add_argument('dst_zip') |
| 66 | args = parser.parse_args() |
| 67 | |
Cole Faust | 5a8f533 | 2024-03-28 17:07:26 -0700 | [diff] [blame] | 68 | errors = [] |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 69 | with open(args.dst_zip, 'wb') as outf, open(args.src_zip, 'rb') as inf: |
| 70 | with zipfile.ZipFile(outf, mode='w') as outzip, zipfile.ZipFile(inf, mode='r') as inzip: |
Anas Sulaiman | c755fdb | 2024-02-06 19:09:44 +0000 | [diff] [blame] | 71 | for name in inzip.namelist(): |
| 72 | with inzip.open(name, mode='r') as inzipf: |
Cole Faust | 5a8f533 | 2024-03-28 17:07:26 -0700 | [diff] [blame] | 73 | try: |
| 74 | process_one_file(name, inzipf, outzip) |
| 75 | except py_compile.PyCompileError as e: |
| 76 | errors.append(e) |
| 77 | |
| 78 | if errors: |
| 79 | for i, error in enumerate(errors): |
| 80 | # Print an empty line in between each error |
| 81 | if i > 0: |
| 82 | print(file=sys.stderr) |
| 83 | print(str(error).strip(), file=sys.stderr) |
| 84 | sys.exit(1) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | main() |