1#!/bin/bash
2set -e
3
4function usage() {
5  echo "Usage: $0 [--gc] [--target <target>] <buildId> [<buildId>...]"
6  echo
7  echo "Downloads logs from the given build Ids, and then"
8  echo "Updates the exemptions file (messages.ignore) to include a suppression for each of those messages."
9  echo
10  echo "  [--gc] Also remove any suppressions that don't match any existent messages"
11  echo "  [--target] Only download from logs from <target>, not from all targets"
12  exit 1
13}
14
15targets=""
16gc=false
17while true; do
18  if [ "$1" == "--gc" ]; then
19    gc=true
20    shift
21    continue
22  fi
23  if [ "$1" == "--target" ]; then
24    shift
25    targets="$1"
26    shift
27    continue
28  fi
29  break
30done
31
32if [ "$1" == "" ]; then
33  usage
34fi
35
36# fetch_artifact --bid <buildId> --target <target> <filepath>
37function fetch_artifact() {
38  fetch_script=/google/data/ro/projects/android/fetch_artifact
39  echo $fetch_script "$@"
40  $fetch_script "$@"
41}
42
43# fetch_log <target>
44function fetch_logs() {
45  build_id="$1"
46  target="$2"
47  newDir="${build_id}_${target}"
48  mkdir $newDir
49  cd $newDir
50  # download as many logs as exist
51  for i in 0 $(seq 20); do
52    if [ "$i" == "0" ]; then
53      logName="gradle.log"
54    else
55      logName="gradle.${i}.log"
56    fi
57    filepath="logs/$logName"
58    # incremental build uses a subdirectory
59    if [ "$target" == "androidx_incremental" ]; then
60      filepath="incremental/$filepath"
61    fi
62    if fetch_artifact --bid "$buildId" --target "$target" "$filepath"; then
63      echo "downloaded log ${i} in build $buildId target $target"
64    else
65      echo
66      echo "$logName does not exist in build $buildId target ${target}; continuing"
67      echo
68      break
69    fi
70  done
71  cd ..
72}
73
74function setup_temp_dir() {
75  tempDir="/tmp/build_log_gc"
76  rm -rf "$tempDir"
77  mkdir -p "$tempDir"
78  echo cd "$tempDir"
79  cd "$tempDir"
80}
81
82# get some paths
83SCRIPT_DIR="$(cd $(dirname $0) && pwd)"
84SUPPORT_ROOT="$(cd $SCRIPT_DIR/../.. && pwd)"
85BUILD_SCRIPTS_DIR="$SUPPORT_ROOT/busytown"
86
87# find the .sh files that enable build log validation
88target_scripts="$(cd $BUILD_SCRIPTS_DIR && find -maxdepth 1 -name "*.sh" -type f | grep -v androidx-studio-integration | sed 's|^./||')"
89
90# find the target names that enable build log validation
91if [ "$targets" == "" ]; then
92  targets="$(echo $target_scripts | sed 's/\.sh//g')"
93fi
94
95# download log for each target
96setup_temp_dir
97while [ "$1" != "" ]; do
98  buildId="$1"
99  for target in $targets; do
100    fetch_logs $buildId $target
101  done
102  shift
103done
104
105# process all of the logs
106logs="$(echo */*.log)"
107echo
108echo $SCRIPT_DIR/build_log_simplifier.py --update --gc $logs
109if [ "$gc" == "true" ]; then
110  gcArg="--gc"
111else
112  gcArg=""
113fi
114$SCRIPT_DIR/build_log_simplifier.py --update $gcArg $logs
115echo
116echo succeeded
117