• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16USAGE="usage: $(basename $0)"
17HELP="\
18$USAGE -p | -b | -v | -l <label>
19$USAGE -h
20
21Standard input to this command is the output of some variant of
22'atest host list'.  The command line option selects a particular
23category 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
29Exactly one label selection option must be supplied; there is no
30default, and multiple options aren't allowed.
31
32The comand reports the counts of the various values of the
33selected label.
34
35Example:
36  \$ atest host list -b board:daisy_skate | count_labels -p
37        9 bvt
38       14 suites
39        1 wificell
40"
41
42
43usage() {
44    if [ $# -ne 0 ]; then
45        echo "$@" >&2
46        echo >&2
47    fi
48    echo "$HELP" >&2
49    exit 1
50}
51
52COUNT=0
53ERR=0
54while 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 ))
63done
64
65if [ $COUNT -ne 1 ]; then
66    usage "Must have exactly one label-specifying option" >&2
67fi
68
69if [ $ERR -ne 0 ]; then
70    usage
71fi
72
73sed -e "/$LABEL:/ !d" -e "s=.*$LABEL:\([^,]*\).*=\1=" | sort | uniq -c
74