blob: b3cf950d947a41ce967985d559b34dc93fa5e5c4 [file] [log] [blame]
Cole Faust5c503d12023-01-24 11:48:08 -08001#!/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 Faust5a8f5332024-03-28 17:07:26 -070016from __future__ import print_function
Cole Faust5c503d12023-01-24 11:48:08 -080017import argparse
18import py_compile
19import os
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000020import sys
Cole Faust5c503d12023-01-24 11:48:08 -080021import shutil
22import tempfile
23import zipfile
24
25# This file needs to support both python 2 and 3.
26
27
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000028def 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 Faustbaae0832024-04-29 16:47:36 -070033 info.compress_type = zipfile.ZIP_DEFLATED
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000034
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000035 if not info.filename.endswith('.py'):
36 outzip.writestr(info, infile.read())
Cole Faust5c503d12023-01-24 11:48:08 -080037 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 Sulaiman55b7ba02024-02-01 17:13:39 +000042 shutil.copyfileobj(infile, tmp)
Cole Faust5c503d12023-01-24 11:48:08 -080043 in_name = tmp.name
44 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
45 out_name = tmp.name
46 try:
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000047 # 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 Sulaiman55b7ba02024-02-01 17:13:39 +000050 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 Faust5c503d12023-01-24 11:48:08 -080054 with open(out_name, 'rb') as f:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000055 info.filename = info.filename + 'c'
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000056 outzip.writestr(info, f.read())
Cole Faust5c503d12023-01-24 11:48:08 -080057 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
Cole Faust5a8f5332024-03-28 17:07:26 -070068 errors = []
Cole Faust5c503d12023-01-24 11:48:08 -080069 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 Sulaimanc755fdb2024-02-06 19:09:44 +000071 for name in inzip.namelist():
72 with inzip.open(name, mode='r') as inzipf:
Cole Faust5a8f5332024-03-28 17:07:26 -070073 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 Faust5c503d12023-01-24 11:48:08 -080085
86
87if __name__ == "__main__":
88 main()