showard | fb64e6a | 2009-04-22 21:01:18 +0000 | [diff] [blame^] | 1 | |
| 2 | """ |
| 3 | The atomicgroup module contains the objects and methods used to |
| 4 | manage atomic groups in Autotest. |
| 5 | |
| 6 | The valid actions are: |
| 7 | create: Create a new atomic group. |
| 8 | delete: Delete (invalidate) an existing atomic group. |
| 9 | list: Lists atomic groups. |
| 10 | add: Adds labels to an atomic group. |
| 11 | remove: Removes labels from an atomic group. |
| 12 | |
| 13 | See topic_common.py for a High Level Design and Algorithm. |
| 14 | """ |
| 15 | |
| 16 | import os, sys |
| 17 | from autotest_lib.cli import topic_common, action_common |
| 18 | |
| 19 | class atomicgroup(topic_common.atest): |
| 20 | """ |
| 21 | Atomic group class |
| 22 | |
| 23 | atest atomicgroup [create|delete|list|add|remove] <options> |
| 24 | """ |
| 25 | usage_action = '[create|delete|list|add|remove]' |
| 26 | topic = 'atomic_group' |
| 27 | msg_topic = 'atomicgroup' |
| 28 | msg_items = '<atomicgroups>' |
| 29 | |
| 30 | |
| 31 | def __init__(self): |
| 32 | super(atomicgroup, self).__init__() |
| 33 | self.parser.add_option('-G', '--glist', |
| 34 | help='File listing the ATOMIC GROUPs', |
| 35 | type='string', default=None, |
| 36 | metavar='ATOMIC_GROUP_FLIST') |
| 37 | |
| 38 | |
| 39 | def parse(self, flists=None, req_items='atomicgroups'): |
| 40 | if not flists: |
| 41 | flists = [] |
| 42 | flists.append(('atomicgroups', 'glist', '', True)) |
| 43 | return self.parse_with_flist(flists, req_items) |
| 44 | |
| 45 | |
| 46 | def get_items(self): |
| 47 | return self.atomicgroups |
| 48 | |
| 49 | |
| 50 | class atomicgroup_help(atomicgroup): |
| 51 | """Just to get the atest logic working. Usage is set by its parent.""" |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | class atomicgroup_list(action_common.atest_list, atomicgroup): |
| 56 | """atest atomicgroup list [--show-invalid]""" |
| 57 | def __init__(self): |
| 58 | super(atomicgroup_list, self).__init__() |
| 59 | self.parser.add_option('-d', '--show-invalid', |
| 60 | help='Don\'t hide invalid atomic groups.', |
| 61 | action='store_true') |
| 62 | |
| 63 | |
| 64 | def parse(self): |
| 65 | options, leftover = super(atomicgroup_list, self).parse(req_items=None) |
| 66 | self.show_invalid = options.show_invalid |
| 67 | return options, leftover |
| 68 | |
| 69 | |
| 70 | def execute(self): |
| 71 | return super(atomicgroup_list, self).execute(op='get_atomic_groups') |
| 72 | |
| 73 | |
| 74 | def output(self, results): |
| 75 | if not self.show_invalid: |
| 76 | results = [atomicgroup for atomicgroup in results |
| 77 | if not atomicgroup['invalid']] |
| 78 | |
| 79 | keys = ['name', 'description', 'max_number_of_machines', 'invalid'] |
| 80 | super(atomicgroup_list, self).output(results, keys) |
| 81 | |
| 82 | |
| 83 | class atomicgroup_create(action_common.atest_create, atomicgroup): |
| 84 | def __init__(self): |
| 85 | super(atomicgroup_create, self).__init__() |
| 86 | self.parser.add_option('-n', '--max_number_of_machines', |
| 87 | help='Maximum # of machines for this group.', |
| 88 | type='int') |
| 89 | self.parser.add_option('-d', '--description', |
| 90 | help='Description of this atomic group.', |
| 91 | type='string', |
| 92 | default='') |
| 93 | |
| 94 | |
| 95 | def parse(self): |
| 96 | options, leftover = super(atomicgroup_create, self).parse() |
| 97 | self.data_item_key = 'name' |
| 98 | self.data['description'] = options.description |
| 99 | self.data['max_number_of_machines'] = options.max_number_of_machines |
| 100 | return options, leftover |
| 101 | |
| 102 | |
| 103 | class atomicgroup_delete(action_common.atest_delete, atomicgroup): |
| 104 | """atest atomicgroup delete <atomicgroup>""" |
| 105 | pass |
| 106 | |
| 107 | |
| 108 | class atomicgroup_add_or_remove(atomicgroup): |
| 109 | |
| 110 | def __init__(self): |
| 111 | super(atomicgroup_add_or_remove, self).__init__() |
| 112 | # .add_remove_things is used by action_common.atest_add_or_remove. |
| 113 | self.add_remove_things = {'labels': 'label'} |
| 114 | lower_words = tuple(word.lower() for word in self.usage_words) |
| 115 | self.parser.add_option('-l', '--label', |
| 116 | help=('%s LABELS(s) %s the ATOMIC GROUP.' % |
| 117 | self.usage_words), |
| 118 | type='string', |
| 119 | metavar='LABEL') |
| 120 | self.parser.add_option('-L', '--label_list', |
| 121 | help='File containing LABELs to %s %s ' |
| 122 | 'the ATOMIC GROUP.' % lower_words, |
| 123 | type='string', |
| 124 | metavar='LABEL_FLIST') |
| 125 | |
| 126 | |
| 127 | def parse(self): |
| 128 | flists = [('labels', 'label_list', 'label', False)] |
| 129 | options, leftover = super(atomicgroup_add_or_remove, self).parse(flists) |
| 130 | if not getattr(self, 'labels', None): |
| 131 | self.invalid_syntax('%s %s requires at least one label' % |
| 132 | (self.msg_topic, |
| 133 | self.usage_action)) |
| 134 | return options, leftover |
| 135 | |
| 136 | |
| 137 | class atomicgroup_add(action_common.atest_add, atomicgroup_add_or_remove): |
| 138 | """atest atomicgroup add <atomicgroup> |
| 139 | [--label <label>] [--label_list <file>]""" |
| 140 | pass |
| 141 | |
| 142 | |
| 143 | class atomicgroup_remove(action_common.atest_remove, atomicgroup_add_or_remove): |
| 144 | """atest atomicgroup remove <atomicgroup> |
| 145 | [--label <label>] [--label_list <file>]""" |
| 146 | pass |