• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
35set -x # Print command line before executing
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        count = 0
52        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
67                    count += 1
68                    out.write(f'### Command {count} ========\n\n')
69                    out.write('#' + command + '\n\n')
70
71                    continue
72
73                if s.startswith("FAILED:"):
74                    break
75
76    os.chmod(outfile, 0o755)
77    print(outfile)
78
79    return 0
80
81
82if __name__ == '__main__':
83    sys.exit(main(sys.argv[1:]))
84