Home | History | Annotate | Download | only in contrib
      1 #!/bin/bash
      2 #
      3 # Copyright 2014 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 #
      7 # count_labels:  Print a summary of how many times a particular label
      8 # value occurs in the output of an `atest host list` command.
      9 #
     10 # To find the sizes of the pools assigned to a board:
     11 #     atest host list -b board:$BOARD | count_labels -p
     12 #
     13 # To find how many of each board is assigned to a pool:
     14 #     atest host list -b pool:$POOL | count_labels -b
     15 
     16 USAGE="usage: $(basename $0)"
     17 HELP="\
     18 $USAGE -p | -b | -v | -l <label>
     19 $USAGE -h
     20 
     21 Standard input to this command is the output of some variant of
     22 'atest host list'.  The command line option selects a particular
     23 category of label to be counted:
     24   -p: Count \"pool:\" label values.
     25   -b: Count \"board:\" label values.
     26   -m: Count \"model:\" label values.
     27   -v: Count \"variant:\" label values.
     28   -l <label>: Count values of labels named \"<label>:\"
     29 
     30 Exactly one label selection option must be supplied; there is no
     31 default, and multiple options aren't allowed.
     32 
     33 The comand reports the counts of the various values of the
     34 selected label.
     35 
     36 Example:
     37   \$ atest host list -b board:daisy_skate | count_labels -p
     38         9 bvt
     39        14 suites
     40         1 wificell
     41 "
     42 
     43 
     44 usage() {
     45     if [ $# -ne 0 ]; then
     46         echo "$@" >&2
     47         echo >&2
     48     fi
     49     echo "$HELP" >&2
     50     exit 1
     51 }
     52 
     53 COUNT=0
     54 ERR=0
     55 while getopts 'hpbmvl:' flag; do
     56     case $flag in
     57         p) LABEL=pool ;;
     58         b) LABEL=board ;;
     59         m) LABEL=model ;;
     60         v) LABEL=variant ;;
     61         l) LABEL=$OPTARG ;;
     62         h|\?) ERR=1 ;;
     63     esac
     64     COUNT=$(( COUNT + 1 ))
     65 done
     66 
     67 if [ $COUNT -ne 1 ]; then
     68     usage "Must have exactly one label-specifying option" >&2
     69 fi
     70 
     71 if [ $ERR -ne 0 ]; then
     72     usage
     73 fi
     74 
     75 sed -e "/$LABEL:/ !d" -e "s=.*$LABEL:\([^,]*\).*=\1=" | sort |
     76     uniq -c | awk '{sum += $1; print} END {printf "%7d total\n", sum}'
     77