blob: 07b8fe97bbe16bc87ca5a35ee3bab2edd042fa9a [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))
33
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000034 if not info.filename.endswith('.py'):
35 outzip.writestr(info, infile.read())
Cole Faust5c503d12023-01-24 11:48:08 -080036 return
37
38 # Unfortunately py_compile requires the input/output files to be written
39 # out to disk.
40 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000041 shutil.copyfileobj(infile, tmp)
Cole Faust5c503d12023-01-24 11:48:08 -080042 in_name = tmp.name
43 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
44 out_name = tmp.name
45 try:
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000046 # Ensure a deterministic .pyc output by using the hash rather than the timestamp.
47 # Only works on Python 3.7+
48 # See https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000049 if sys.version_info >= (3, 7):
50 py_compile.compile(in_name, out_name, info.filename, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
51 else:
52 py_compile.compile(in_name, out_name, info.filename, doraise=True)
Cole Faust5c503d12023-01-24 11:48:08 -080053 with open(out_name, 'rb') as f:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000054 info.filename = info.filename + 'c'
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000055 outzip.writestr(info, f.read())
Cole Faust5c503d12023-01-24 11:48:08 -080056 finally:
57 os.remove(in_name)
58 os.remove(out_name)
59
60
61def main():
62 parser = argparse.ArgumentParser()
63 parser.add_argument('src_zip')
64 parser.add_argument('dst_zip')
65 args = parser.parse_args()
66
Cole Faust5a8f5332024-03-28 17:07:26 -070067 errors = []
Cole Faust5c503d12023-01-24 11:48:08 -080068 with open(args.dst_zip, 'wb') as outf, open(args.src_zip, 'rb') as inf:
69 with zipfile.ZipFile(outf, mode='w') as outzip, zipfile.ZipFile(inf, mode='r') as inzip:
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000070 for name in inzip.namelist():
71 with inzip.open(name, mode='r') as inzipf:
Cole Faust5a8f5332024-03-28 17:07:26 -070072 try:
73 process_one_file(name, inzipf, outzip)
74 except py_compile.PyCompileError as e:
75 errors.append(e)
76
77 if errors:
78 for i, error in enumerate(errors):
79 # Print an empty line in between each error
80 if i > 0:
81 print(file=sys.stderr)
82 print(str(error).strip(), file=sys.stderr)
83 sys.exit(1)
Cole Faust5c503d12023-01-24 11:48:08 -080084
85
86if __name__ == "__main__":
87 main()