xref: /aosp_15_r20/kernel/configs/tools/bump.py (revision 1d3556b860e096874ddca674556f5d20fb3a73b0)
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    # TODO(b/355580919): Remove the pattern '[0-9]+\\.next' by replacing the
34    # version placeholder with 'next'.
35    check_call("sed -i'' -E 's/\"kernel_config_{}_([0-9]+\\.[0-9]+|[0-9]+\\.next|next)\"/\"kernel_config_{}_\\1\"/g' {}"
36                .format(current_release, new_release, file_path), shell=True)
37
38class Bump(object):
39    def __init__(self, cmdline_args):
40        top = os.environ["ANDROID_BUILD_TOP"]
41        self.current_release = cmdline_args.current
42        self.new_release = cmdline_args.next
43        self.configs_dir = os.path.join(top, "kernel/configs")
44        self.current_release_dir = os.path.join(self.configs_dir, self.current_release)
45        self.new_release_dir = os.path.join(self.configs_dir, self.new_release)
46        self.versions = [e for e in os.listdir(self.current_release_dir) if e.startswith("android-")]
47
48    def run(self):
49        shutil.copytree(self.current_release_dir, self.new_release_dir)
50        for version in self.versions:
51            dst = os.path.join(self.new_release_dir, version)
52            for file_name in os.listdir(dst):
53                abs_path = os.path.join(dst, file_name)
54                if not os.path.isfile(abs_path):
55                    continue
56                year = datetime.datetime.now().year
57                check_call("sed -i'' -E 's/Copyright \\(C\\) [0-9]{{4,}}/Copyright (C) {}/g' {}".format(year, abs_path), shell=True)
58                replace_configs_module_name(self.current_release, self.new_release, abs_path)
59                if os.path.basename(abs_path) == "Android.bp":
60                    if shutil.which("bpfmt") is not None:
61                        check_call("bpfmt -w {}".format(abs_path), shell=True)
62                    else:
63                        print("bpfmt is not available so {} is not being formatted. Try `m bpfmt` first".format(abs_path))
64
65def main():
66    parser = argparse.ArgumentParser(description=__doc__)
67    parser.add_argument('current', type=str, help='name of the current version (e.g. v)')
68    parser.add_argument('next', type=str, help='name of the next version (e.g. w)')
69    cmdline_args = parser.parse_args()
70
71    Bump(cmdline_args).run()
72
73if __name__ == '__main__':
74    main()
75