Makoto Onuki | a6a9a75 | 2024-11-27 11:19:59 -0800 | [diff] [blame] | 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 | |
| 23 | import datetime |
| 24 | import gzip |
| 25 | import os |
| 26 | import re |
| 27 | import shlex |
| 28 | import sys |
| 29 | |
| 30 | re_command = re.compile(r''' ^\[.*?\] \s* (.*) ''', re.X) |
| 31 | |
| 32 | HEADER = r'''#!/bin/bash |
| 33 | |
| 34 | set -e # Stop on a failed command |
| 35 | |
| 36 | cd "${ANDROID_BUILD_TOP:?}" |
| 37 | |
| 38 | ''' |
| 39 | |
| 40 | OUT_SCRIPT_DIR = "/tmp/" |
| 41 | OUT_SCRIPT_FORMAT = "soong-rerun-%Y-%m-%d_%H-%M-%S.sh" |
| 42 | |
| 43 | def 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 Onuki | 91928aa | 2024-12-09 09:42:47 -0800 | [diff] [blame] | 51 | count = 0 |
Makoto Onuki | a6a9a75 | 2024-11-27 11:19:59 -0800 | [diff] [blame] | 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 | |
Makoto Onuki | 91928aa | 2024-12-09 09:42:47 -0800 | [diff] [blame] | 67 | count += 1 |
| 68 | out.write(f'### Command {count} ========\n') |
Makoto Onuki | a6a9a75 | 2024-11-27 11:19:59 -0800 | [diff] [blame] | 69 | |
| 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 | |
| 90 | if __name__ == '__main__': |
| 91 | sys.exit(main(sys.argv[1:])) |