xref: /aosp_15_r20/frameworks/base/ravenwood/scripts/extract-last-soong-commands.py (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1#!/usr/bin/env python3
2# Copyright (C) 2024 The Android Open Source Project
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
16
17# This script extracts all the commands executed in the last soong run,
18# and write them into a script file, and print the filename.
19#
20# All the commands are commented out. Uncomment what you want to execute as
21# needed before running it.
22
23import datetime
24import gzip
25import os
26import re
27import shlex
28import sys
29
30re_command = re.compile(r''' ^\[.*?\]  \s*  (.*) ''', re.X)
31
32HEADER = r'''#!/bin/bash
33
34set -e # Stop on a failed command
35
36cd "${ANDROID_BUILD_TOP:?}"
37
38'''
39
40OUT_SCRIPT_DIR = "/tmp/"
41OUT_SCRIPT_FORMAT = "soong-rerun-%Y-%m-%d_%H-%M-%S.sh"
42
43def main(args):
44    log = os.environ["ANDROID_BUILD_TOP"] + "/out/verbose.log.gz"
45    outdir = "/tmp/"
46    outfile = outdir + datetime.datetime.now().strftime(OUT_SCRIPT_FORMAT)
47
48    with open(outfile, "w") as out:
49        out.write(HEADER)
50
51        with gzip.open(log) as f:
52            for line in f:
53                s = line.decode("utf-8")
54
55                if s.startswith("verbose"):
56                    continue
57                if re.match('^\[.*bootstrap blueprint', s):
58                    continue
59
60                s = s.rstrip()
61
62                m = re_command.search(s)
63                if m:
64                    command = m.groups()[0]
65
66                    out.write('#========\n')
67
68                    # Show the full command line before executing it.
69                    out.write('#echo ' + shlex.quote(command) + '\n')
70                    out.write('\n')
71
72                    # Execute the command.
73                    out.write('#' + command + '\n')
74
75                    out.write('\n')
76
77                    continue
78
79                if s.startswith("FAILED:"):
80                    break
81
82    os.chmod(outfile, 0o755)
83    print(outfile)
84
85    return 0
86
87
88if __name__ == '__main__':
89    sys.exit(main(sys.argv[1:]))
90