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 16from __future__ import print_function 17import argparse 18import py_compile 19import os 20import sys 21import shutil 22import tempfile 23import zipfile 24 25# This file needs to support both python 2 and 3. 26 27 28def 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)) 33 info.compress_type = zipfile.ZIP_DEFLATED 34 35 if not info.filename.endswith('.py'): 36 outzip.writestr(info, infile.read()) 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: 42 shutil.copyfileobj(infile, tmp) 43 in_name = tmp.name 44 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp: 45 out_name = tmp.name 46 try: 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 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) 54 with open(out_name, 'rb') as f: 55 info.filename = info.filename + 'c' 56 outzip.writestr(info, f.read()) 57 finally: 58 os.remove(in_name) 59 os.remove(out_name) 60 61 62def main(): 63 parser = argparse.ArgumentParser() 64 parser.add_argument('src_zip') 65 parser.add_argument('dst_zip') 66 args = parser.parse_args() 67 68 errors = [] 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: 71 for name in inzip.namelist(): 72 with inzip.open(name, mode='r') as inzipf: 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) 85 86 87if __name__ == "__main__": 88 main() 89