mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """ |
| 5 | The label module contains the objects and methods used to |
| 6 | manage labels in Autotest. |
| 7 | |
| 8 | The valid actions are: |
| 9 | add: adds label(s), or hosts to an LABEL |
| 10 | remove: deletes label(s), or hosts from an LABEL |
| 11 | list: lists label(s) |
| 12 | |
| 13 | The common options are: |
| 14 | --blist / -B: file containing a list of LABELs |
| 15 | |
| 16 | See topic_common.py for a High Level Design and Algorithm. |
| 17 | """ |
| 18 | |
| 19 | import os, sys |
| 20 | from autotest_lib.cli import topic_common, action_common |
| 21 | |
| 22 | |
| 23 | class label(topic_common.atest): |
| 24 | """Label class |
| 25 | atest label [create|delete|list|add|remove] <options>""" |
| 26 | usage_action = '[create|delete|list|add|remove]' |
| 27 | topic = msg_topic = 'label' |
| 28 | msg_items = '<labels>' |
| 29 | |
| 30 | def __init__(self): |
| 31 | """Add to the parser the options common to all the |
| 32 | label actions""" |
| 33 | super(label, self).__init__() |
| 34 | |
| 35 | self.parser.add_option('-B', '--blist', |
| 36 | help='File listing the labels', |
| 37 | type='string', |
| 38 | default=None, |
| 39 | metavar='LABEL_FLIST') |
| 40 | |
| 41 | |
| 42 | def parse(self, flists=None, req_items='labels'): |
| 43 | """Consume the common label options""" |
| 44 | if flists: |
| 45 | flists.append(('labels', 'blist', '', True)) |
| 46 | else: |
| 47 | flists = [('labels', 'blist', '', True)] |
| 48 | return self.parse_with_flist(flists, req_items) |
| 49 | |
| 50 | |
| 51 | def get_items(self): |
| 52 | return self.labels |
| 53 | |
| 54 | |
| 55 | class label_help(label): |
| 56 | """Just here to get the atest logic working. |
| 57 | Usage is set by its parent""" |
| 58 | pass |
| 59 | |
| 60 | |
| 61 | class label_list(action_common.atest_list, label): |
| 62 | """atest label list [--platform] [--all] |
| 63 | [--valid-only] [--machine <machine>] |
| 64 | [--blist <file>] [<labels>]""" |
| 65 | def __init__(self): |
| 66 | super(label_list, self).__init__() |
| 67 | |
| 68 | self.parser.add_option('-t', '--platform-only', |
| 69 | help='Display only platform labels', |
| 70 | action='store_true') |
| 71 | |
| 72 | self.parser.add_option('-d', '--valid-only', |
| 73 | help='Display only valid labels', |
| 74 | action='store_true') |
| 75 | |
| 76 | self.parser.add_option('-a', '--all', |
| 77 | help=('Display both normal & ' |
| 78 | 'platform labels'), |
| 79 | action='store_true') |
| 80 | |
| 81 | self.parser.add_option('-m', '--machine', |
| 82 | help='List LABELs of MACHINE', |
| 83 | type='string', |
| 84 | metavar='MACHINE') |
| 85 | |
| 86 | |
| 87 | def parse(self): |
| 88 | flists = [('hosts', '', 'machine', False)] |
| 89 | (options, leftover) = super(label_list, self).parse(flists, |
| 90 | req_items=None) |
| 91 | |
| 92 | if options.all and options.platform_only: |
| 93 | self.invalid_syntax('Only specify one of --all,' |
| 94 | '--platform') |
| 95 | |
| 96 | if len(self.hosts) > 1: |
| 97 | self.invalid_syntax(('Only one machine name allowed. ' |
| 98 | '''Use '%s host list %s' ''' |
| 99 | 'instead.') % |
| 100 | (sys.argv[0], ','.join(self.hosts))) |
| 101 | self.all = options.all |
| 102 | self.platform_only = options.platform_only |
| 103 | self.valid_only = options.valid_only |
| 104 | return (options, leftover) |
| 105 | |
| 106 | |
| 107 | def execute(self): |
| 108 | filters = {} |
| 109 | check_results = {} |
| 110 | if self.hosts: |
| 111 | filters['host__hostname__in'] = self.hosts |
| 112 | check_results['host__hostname__in'] = None |
| 113 | |
| 114 | if self.labels: |
| 115 | filters['name__in'] = self.labels |
| 116 | check_results['name__in'] = 'name' |
| 117 | |
| 118 | return super(label_list, self).execute(op='get_labels', |
| 119 | filters=filters, |
| 120 | check_results=check_results) |
| 121 | |
| 122 | |
| 123 | def output(self, results): |
| 124 | if self.valid_only: |
| 125 | results = [label for label in results |
| 126 | if not label['invalid']] |
| 127 | |
| 128 | if self.platform_only: |
| 129 | results = [label for label in results |
| 130 | if label['platform']] |
| 131 | keys = ['name', 'invalid'] |
| 132 | elif not self.all: |
| 133 | results = [label for label in results |
| 134 | if not label['platform']] |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 135 | keys = ['name', 'only_if_needed', 'invalid'] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 136 | else: |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 137 | keys = ['name', 'platform', 'only_if_needed', 'invalid'] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 138 | |
| 139 | super(label_list, self).output(results, keys) |
| 140 | |
| 141 | |
| 142 | class label_create(action_common.atest_create, label): |
| 143 | """atest label create <labels>|--blist <file> --platform""" |
| 144 | def __init__(self): |
| 145 | super(label_create, self).__init__() |
| 146 | self.parser.add_option('-t', '--platform', |
| 147 | help='To create this label as a platform', |
| 148 | default=False, |
| 149 | action='store_true') |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 150 | self.parser.add_option('-o', '--only_if_needed', |
| 151 | help='To mark the label as "only use if needed', |
| 152 | default=False, |
| 153 | action='store_true') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def parse(self): |
| 157 | (options, leftover) = super(label_create, self).parse() |
| 158 | self.data_item_key = 'name' |
| 159 | self.data['platform'] = options.platform |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 160 | self.data['only_if_needed'] = options.only_if_needed |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 161 | return (options, leftover) |
| 162 | |
| 163 | |
| 164 | class label_delete(action_common.atest_delete, label): |
| 165 | """atest label delete <labels>|--blist <file>""" |
| 166 | pass |
| 167 | |
| 168 | |
| 169 | |
| 170 | class label_add_or_remove(label): |
| 171 | def __init__(self): |
| 172 | super(label_add_or_remove, self).__init__() |
| 173 | lower_words = tuple(word.lower() for word in self.usage_words) |
| 174 | self.parser.add_option('-m', '--machine', |
| 175 | help=('%s MACHINE(s) %s the LABEL' % |
| 176 | self.usage_words), |
| 177 | type='string', |
| 178 | metavar='MACHINE') |
| 179 | self.parser.add_option('-M', '--mlist', |
| 180 | help='File containing machines to %s %s ' |
| 181 | 'the LABEL' % lower_words, |
| 182 | type='string', |
| 183 | metavar='MACHINE_FLIST') |
| 184 | |
| 185 | |
| 186 | def parse(self): |
| 187 | flists = [('hosts', 'mlist', 'machine', False)] |
| 188 | (options, leftover) = super(label_add_or_remove, self).parse(flists) |
| 189 | if not getattr(self, 'hosts', None): |
| 190 | self.invalid_syntax('%s %s requires at least one host' % |
| 191 | (self.msg_topic, |
| 192 | self.usage_action)) |
| 193 | return (options, leftover) |
| 194 | |
| 195 | |
| 196 | class label_add(action_common.atest_add, label_add_or_remove): |
| 197 | """atest label add <labels>|--blist <file> |
| 198 | --platform [--machine <machine>] [--mlist <file>]""" |
| 199 | pass |
| 200 | |
| 201 | |
| 202 | class label_remove(action_common.atest_remove, label_add_or_remove): |
| 203 | """atest label remove <labels>|--blist <file> |
| 204 | [--machine <machine>] [--mlist <file>]""" |
| 205 | pass |