1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Freeze kernel configs for a compatibility matrix release.
20"""
21
22import argparse
23import datetime
24import os
25import shutil
26
27import subprocess
28
29def check_call(*args, **kwargs):
30    print(args[0])
31    subprocess.check_call(*args, **kwargs)
32
33def replace_configs_module_name(new_release, file_path):
34    check_call("sed -i'' -E 's/\"kernel_config_current_([0-9.]*)\"/\"kernel_config_{}_\\1\"/g' {}"
35                .format(new_release, file_path), shell=True)
36
37class Freeze(object):
38    def __init__(self, cmdline_args):
39        top = os.environ["ANDROID_BUILD_TOP"]
40        self.new_release = cmdline_args.name
41        self.configs_dir = os.path.join(top, "kernel/configs")
42        self.new_release_dir = os.path.join(self.configs_dir, self.new_release)
43        self.interfaces_dir = os.path.join(top, "hardware/interfaces")
44        self.versions = [e for e in os.listdir(self.configs_dir) if e.startswith("android-")]
45        self.bugline = "Bug: {}\n".format(cmdline_args.bug) if cmdline_args.bug else ""
46
47    def run(self):
48        self.move_configs()
49        self.freeze_configs_in_matrices()
50        self.create_current()
51        self.print_summary()
52
53    def move_configs(self):
54        os.makedirs(self.new_release_dir, exist_ok=False)
55        for version in self.versions:
56            src = os.path.join(self.configs_dir, version)
57            dst = os.path.join(self.new_release_dir, version)
58            shutil.move(src, dst)
59            for file_name in os.listdir(dst):
60                abs_path = os.path.join(dst, file_name)
61                if not os.path.isfile(abs_path):
62                    continue
63                year = datetime.datetime.now().year
64                check_call("sed -i'' -E 's/Copyright \\(C\\) [0-9]{{4,}}/Copyright (C) {}/g' {}".format(year, abs_path), shell=True)
65                replace_configs_module_name(self.new_release, abs_path)
66
67        check_call('git -C {} add android-* {}'.format(self.configs_dir, self.new_release), shell=True)
68        check_call('git -C {} commit -m "Freeze kernel configs for {}.\n\n{}Test: builds"'.format(self.configs_dir, self.new_release, self.bugline), shell=True)
69
70    def freeze_configs_in_matrices(self):
71        matrices_soong = "compatibility_matrices/Android.bp"
72        matrices_soong_abs_path = os.path.join(self.interfaces_dir, matrices_soong)
73        replace_configs_module_name(self.new_release, matrices_soong_abs_path)
74
75        check_call('git -C {} add {}'.format(self.interfaces_dir, matrices_soong), shell=True)
76        check_call('git -C {} commit -m "Freeze kernel configs for {}.\n\n{}Test: builds"'.format(self.interfaces_dir, self.new_release, self.bugline), shell=True)
77
78    def create_current(self):
79        check_call('git -C {} checkout HEAD~ -- android-*'.format(self.configs_dir), shell=True)
80        check_call('git -C {} add android-*'.format(self.configs_dir), shell=True)
81        check_call('git -C {} commit -m "Create kernel configs for current.\n\n{}Test: builds"'.format(self.configs_dir, self.bugline), shell=True)
82
83    def print_summary(self):
84        print("*** Please submit these changes to {} branch: ***".format(self.new_release))
85        check_call('git -C {} show -s --format=%B HEAD~1'.format(self.configs_dir), shell=True)
86        check_call('git -C {} show -s --format=%B HEAD'.format(self.interfaces_dir), shell=True)
87        print("*** Please submit these changes to master branch: ***")
88        check_call('git -C {} show -s --format=%B HEAD'.format(self.configs_dir), shell=True)
89
90def main():
91    parser = argparse.ArgumentParser(description=__doc__)
92    parser.add_argument('name', type=str, help='name of the directory (e.g. r)')
93    parser.add_argument('--bug', type=str, nargs='?', help='Bug number')
94    cmdline_args = parser.parse_args()
95
96    Freeze(cmdline_args).run()
97
98if __name__ == '__main__':
99    main()
100