blob: 80e7c76a1d826bc12b8072555c99a21db24c18dd [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
16import argparse
17import py_compile
18import os
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000019import sys
Cole Faust5c503d12023-01-24 11:48:08 -080020import shutil
21import tempfile
22import zipfile
23
24# This file needs to support both python 2 and 3.
25
26
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000027def process_one_file(info, infile, outzip):
28 if not info.filename.endswith('.py'):
29 outzip.writestr(info, infile.read())
Cole Faust5c503d12023-01-24 11:48:08 -080030 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 Sulaiman55b7ba02024-02-01 17:13:39 +000035 shutil.copyfileobj(infile, tmp)
Cole Faust5c503d12023-01-24 11:48:08 -080036 in_name = tmp.name
37 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
38 out_name = tmp.name
39 try:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000040 # 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 Faust5c503d12023-01-24 11:48:08 -080048 with open(out_name, 'rb') as f:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000049 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 Faust5c503d12023-01-24 11:48:08 -080052 finally:
53 os.remove(in_name)
54 os.remove(out_name)
55
56
57def 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 Sulaiman55b7ba02024-02-01 17:13:39 +000065 for info in inzip.infolist():
66 with inzip.open(info.filename, mode='r') as inzipf:
67 process_one_file(info, inzipf, outzip)
Cole Faust5c503d12023-01-24 11:48:08 -080068
69
70if __name__ == "__main__":
71 main()