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 | |
| 16 | import argparse |
| 17 | import py_compile |
| 18 | import os |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 19 | import sys |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 20 | import shutil |
| 21 | import tempfile |
| 22 | import zipfile |
| 23 | |
| 24 | # This file needs to support both python 2 and 3. |
| 25 | |
| 26 | |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 27 | def process_one_file(info, infile, outzip): |
| 28 | if not info.filename.endswith('.py'): |
| 29 | outzip.writestr(info, infile.read()) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 30 | return |
| 31 | |
| 32 | # Unfortunately py_compile requires the input/output files to be written |
| 33 | # out to disk. |
| 34 | with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 35 | shutil.copyfileobj(infile, tmp) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 36 | in_name = tmp.name |
| 37 | with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp: |
| 38 | out_name = tmp.name |
| 39 | try: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 40 | # Ensure deterministic pyc by using the hash rather than timestamp. |
| 41 | # This is required to improve caching in accelerated builds. |
| 42 | # Only works on Python 3.7+ (see https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode) |
| 43 | # which should cover most updated branches and developer machines. |
| 44 | if sys.version_info >= (3, 7): |
| 45 | py_compile.compile(in_name, out_name, info.filename, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH) |
| 46 | else: |
| 47 | py_compile.compile(in_name, out_name, info.filename, doraise=True) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 48 | with open(out_name, 'rb') as f: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 49 | info.filename = info.filename + 'c' |
| 50 | # Use ZipInfo rather than str to reuse timestamps for deterministic zip files. |
| 51 | outzip.writestr(info, f.read()) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 52 | finally: |
| 53 | os.remove(in_name) |
| 54 | os.remove(out_name) |
| 55 | |
| 56 | |
| 57 | def main(): |
| 58 | parser = argparse.ArgumentParser() |
| 59 | parser.add_argument('src_zip') |
| 60 | parser.add_argument('dst_zip') |
| 61 | args = parser.parse_args() |
| 62 | |
| 63 | with open(args.dst_zip, 'wb') as outf, open(args.src_zip, 'rb') as inf: |
| 64 | with zipfile.ZipFile(outf, mode='w') as outzip, zipfile.ZipFile(inf, mode='r') as inzip: |
Anas Sulaiman | 55b7ba0 | 2024-02-01 17:13:39 +0000 | [diff] [blame^] | 65 | for info in inzip.infolist(): |
| 66 | with inzip.open(info.filename, mode='r') as inzipf: |
| 67 | process_one_file(info, inzipf, outzip) |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | main() |