blob: f8932e7ac732820cc01b84a1186df0545aa55bfd [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#
2# Copyright 2008 Google Inc. All Rights Reserved.
3
4"""
5The label module contains the objects and methods used to
6manage labels in Autotest.
7
8The valid actions are:
9add: adds label(s), or hosts to an LABEL
10remove: deletes label(s), or hosts from an LABEL
11list: lists label(s)
12
13The common options are:
14--blist / -B: file containing a list of LABELs
15
16See topic_common.py for a High Level Design and Algorithm.
17"""
18
19import os, sys
20from autotest_lib.cli import topic_common, action_common
21
22
23class 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
55class label_help(label):
56 """Just here to get the atest logic working.
57 Usage is set by its parent"""
58 pass
59
60
61class 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']]
135 keys = ['name', 'invalid']
136 else:
137 keys = ['name', 'platform', 'invalid']
138
139 super(label_list, self).output(results, keys)
140
141
142class 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')
150
151
152 def parse(self):
153 (options, leftover) = super(label_create, self).parse()
154 self.data_item_key = 'name'
155 self.data['platform'] = options.platform
156 return (options, leftover)
157
158
159class label_delete(action_common.atest_delete, label):
160 """atest label delete <labels>|--blist <file>"""
161 pass
162
163
164
165class label_add_or_remove(label):
166 def __init__(self):
167 super(label_add_or_remove, self).__init__()
168 lower_words = tuple(word.lower() for word in self.usage_words)
169 self.parser.add_option('-m', '--machine',
170 help=('%s MACHINE(s) %s the LABEL' %
171 self.usage_words),
172 type='string',
173 metavar='MACHINE')
174 self.parser.add_option('-M', '--mlist',
175 help='File containing machines to %s %s '
176 'the LABEL' % lower_words,
177 type='string',
178 metavar='MACHINE_FLIST')
179
180
181 def parse(self):
182 flists = [('hosts', 'mlist', 'machine', False)]
183 (options, leftover) = super(label_add_or_remove, self).parse(flists)
184 if not getattr(self, 'hosts', None):
185 self.invalid_syntax('%s %s requires at least one host' %
186 (self.msg_topic,
187 self.usage_action))
188 return (options, leftover)
189
190
191class label_add(action_common.atest_add, label_add_or_remove):
192 """atest label add <labels>|--blist <file>
193 --platform [--machine <machine>] [--mlist <file>]"""
194 pass
195
196
197class label_remove(action_common.atest_remove, label_add_or_remove):
198 """atest label remove <labels>|--blist <file>
199 [--machine <machine>] [--mlist <file>]"""
200 pass