blob: c08d4aa799a5426083bd10a73e98513f8e1334a6 [file] [log] [blame]
Makoto Onukia6a9a752024-11-27 11:19:59 -08001#!/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
Makoto Onuki91928aa2024-12-09 09:42:47 -080051 count = 0
Makoto Onukia6a9a752024-11-27 11:19:59 -080052 with gzip.open(log) as f:
53 for line in f:
54 s = line.decode("utf-8")
55
56 if s.startswith("verbose"):
57 continue
58 if re.match('^\[.*bootstrap blueprint', s):
59 continue
60
61 s = s.rstrip()
62
63 m = re_command.search(s)
64 if m:
65 command = m.groups()[0]
66
Makoto Onuki91928aa2024-12-09 09:42:47 -080067 count += 1
68 out.write(f'### Command {count} ========\n')
Makoto Onukia6a9a752024-11-27 11:19:59 -080069
70 # Show the full command line before executing it.
71 out.write('#echo ' + shlex.quote(command) + '\n')
72 out.write('\n')
73
74 # Execute the command.
75 out.write('#' + command + '\n')
76
77 out.write('\n')
78
79 continue
80
81 if s.startswith("FAILED:"):
82 break
83
84 os.chmod(outfile, 0o755)
85 print(outfile)
86
87 return 0
88
89
90if __name__ == '__main__':
91 sys.exit(main(sys.argv[1:]))