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   -v: Count \"variant:\" label values.
     27   -l <label>: Count values of labels named \"<label>:\"
     28 
     29 Exactly one label selection option must be supplied; there is no
     30 default, and multiple options aren't allowed.
     31 
     32 The comand reports the counts of the various values of the
     33 selected label.
     34 
     35 Example:
     36   \$ atest host list -b board:daisy_skate | count_labels -p
     37         9 bvt
     38        14 suites
     39         1 wificell
     40 "
     41 
     42 
     43 usage() {
     44     if [ $# -ne 0 ]; then
     45         echo "$@" >&2
     46         echo >&2
     47     fi
     48     echo "$HELP" >&2
     49     exit 1
     50 }
     51 
     52 COUNT=0
     53 ERR=0
     54 while getopts 'hpbvl:' flag; do
     55     case $flag in
     56         p) LABEL=pool ;;
     57         b) LABEL=board ;;
     58         v) LABEL=variant ;;
     59         l) LABEL=$OPTARG ;;
     60         h|\?) ERR=1 ;;
     61     esac
     62     COUNT=$(( COUNT + 1 ))
     63 done
     64 
     65 if [ $COUNT -ne 1 ]; then
     66     usage "Must have exactly one label-specifying option" >&2
     67 fi
     68 
     69 if [ $ERR -ne 0 ]; then
     70     usage
     71 fi
     72 
     73 sed -e "/$LABEL:/ !d" -e "s=.*$LABEL:\([^,]*\).*=\1=" | sort | uniq -c
     74