blob: aa1a5df531f9d0922a3d787454ec70663c425ab0 [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 Sulaimanc755fdb2024-02-06 19:09:44 +000027def process_one_file(name, infile, outzip):
28 # Create a ZipInfo instance with a fixed date to ensure a deterministic output.
29 # Date was chosen to be the same as
30 # https://cs.android.com/android/platform/superproject/main/+/main:build/soong/jar/jar.go;l=36;drc=2863e4535eb65e15f955dc8ed48fa99b1d2a1db5
31 info = zipfile.ZipInfo(filename=name, date_time=(2008, 1, 1, 0, 0, 0))
32
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000033 if not info.filename.endswith('.py'):
34 outzip.writestr(info, infile.read())
Cole Faust5c503d12023-01-24 11:48:08 -080035 return
36
37 # Unfortunately py_compile requires the input/output files to be written
38 # out to disk.
39 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000040 shutil.copyfileobj(infile, tmp)
Cole Faust5c503d12023-01-24 11:48:08 -080041 in_name = tmp.name
42 with tempfile.NamedTemporaryFile(prefix="Soong_precompile_", delete=False) as tmp:
43 out_name = tmp.name
44 try:
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000045 # Ensure a deterministic .pyc output by using the hash rather than the timestamp.
46 # Only works on Python 3.7+
47 # See https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000048 if sys.version_info >= (3, 7):
49 py_compile.compile(in_name, out_name, info.filename, doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
50 else:
51 py_compile.compile(in_name, out_name, info.filename, doraise=True)
Cole Faust5c503d12023-01-24 11:48:08 -080052 with open(out_name, 'rb') as f:
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000053 info.filename = info.filename + 'c'
Anas Sulaiman55b7ba02024-02-01 17:13:39 +000054 outzip.writestr(info, f.read())
Cole Faust5c503d12023-01-24 11:48:08 -080055 finally:
56 os.remove(in_name)
57 os.remove(out_name)
58
59
60def main():
61 parser = argparse.ArgumentParser()
62 parser.add_argument('src_zip')
63 parser.add_argument('dst_zip')
64 args = parser.parse_args()
65
66 with open(args.dst_zip, 'wb') as outf, open(args.src_zip, 'rb') as inf:
67 with zipfile.ZipFile(outf, mode='w') as outzip, zipfile.ZipFile(inf, mode='r') as inzip:
Anas Sulaimanc755fdb2024-02-06 19:09:44 +000068 for name in inzip.namelist():
69 with inzip.open(name, mode='r') as inzipf:
70 process_one_file(name, inzipf, outzip)
Cole Faust5c503d12023-01-24 11:48:08 -080071
72
73if __name__ == "__main__":
74 main()