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"""
19Creates new kernel configs for the next compatibility matrix.
20"""
21
22import argparse
23import datetime
24import os
25import shutil
26import subprocess
27
28def check_call(*args, **kwargs):
29    print(args[0])
30    subprocess.check_call(*args, **kwargs)
31
32def replace_configs_module_name(current_release, new_release, file_path):
33    check_call("sed -i'' -E 's/\"kernel_config_{}_([0-9.]*)\"/\"kernel_config_{}_\\1\"/g' {}"
34                .format(current_release, new_release, file_path), shell=True)
35
36class Bump(object):
37    def __init__(self, cmdline_args):
38        top = os.environ["ANDROID_BUILD_TOP"]
39        self.current_release = cmdline_args.current
40        self.new_release = cmdline_args.next
41        self.configs_dir = os.path.join(top, "kernel/configs")
42        self.current_release_dir = os.path.join(self.configs_dir, self.current_release)
43        self.new_release_dir = os.path.join(self.configs_dir, self.new_release)
44        self.versions = [e for e in os.listdir(self.current_release_dir) if e.startswith("android-")]
45
46    def run(self):
47        shutil.copytree(self.current_release_dir, self.new_release_dir)
48        for version in self.versions:
49            dst = os.path.join(self.new_release_dir, version)
50            for file_name in os.listdir(dst):
51                abs_path = os.path.join(dst, file_name)
52                if not os.path.isfile(abs_path):
53                    continue
54                year = datetime.datetime.now().year
55                check_call("sed -i'' -E 's/Copyright \\(C\\) [0-9]{{4,}}/Copyright (C) {}/g' {}".format(year, abs_path), shell=True)
56                replace_configs_module_name(self.current_release, self.new_release, abs_path)
57                if os.path.basename(abs_path) == "Android.bp":
58                    if shutil.which("bpfmt") is not None:
59                        check_call("bpfmt -w {}".format(abs_path), shell=True)
60                    else:
61                        print("bpfmt is not available so {} is not being formatted. Try `m bpfmt` first".format(abs_path))
62
63def main():
64    parser = argparse.ArgumentParser(description=__doc__)
65    parser.add_argument('current', type=str, help='name of the current version (e.g. v)')
66    parser.add_argument('next', type=str, help='name of the next version (e.g. w)')
67    cmdline_args = parser.parse_args()
68
69    Bump(cmdline_args).run()
70
71if __name__ == '__main__':
72    main()
73