summaryrefslogtreecommitdiff
path: root/cli/atomicgroup.py
blob: 8384d13525413d069959d43b02434b194a97fdec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

"""
The atomicgroup module contains the objects and methods used to
manage atomic groups in Autotest.

The valid actions are:
create:  Create a new atomic group.
delete:  Delete (invalidate) an existing atomic group.
list:    Lists atomic groups.
add:     Adds labels to an atomic group.
remove:  Removes labels from an atomic group.

See topic_common.py for a High Level Design and Algorithm.
"""

import os, sys
from autotest_lib.cli import topic_common, action_common

class atomicgroup(topic_common.atest):
    """
    Atomic group class

    atest atomicgroup [create|delete|list|add|remove] <options>
    """
    usage_action = '[create|delete|list|add|remove]'
    topic = 'atomic_group'
    msg_topic = 'atomicgroup'
    msg_items = '<atomicgroups>'


    def __init__(self):
        super(atomicgroup, self).__init__()
        self.parser.add_option('-G', '--glist',
                               help='File listing the ATOMIC GROUPs',
                               type='string', default=None,
                               metavar='ATOMIC_GROUP_FLIST')

        self.topic_parse_info = topic_common.item_parse_info(
            attribute_name='atomicgroups',
            filename_option='glist',
            use_leftover=True)


    def get_items(self):
        return self.atomicgroups


class atomicgroup_help(atomicgroup):
    """Just to get the atest logic working.  Usage is set by its parent."""
    pass


class atomicgroup_list(action_common.atest_list, atomicgroup):
    """atest atomicgroup list [--show-invalid]"""
    def __init__(self):
        super(atomicgroup_list, self).__init__()
        self.parser.add_option('-d', '--show-invalid',
                               help='Don\'t hide invalid atomic groups.',
                               action='store_true')


    def parse(self):
        options, leftover = super(atomicgroup_list, self).parse()
        self.show_invalid = options.show_invalid
        return options, leftover


    def execute(self):
        return super(atomicgroup_list, self).execute(op='get_atomic_groups')


    def output(self, results):
        if not self.show_invalid:
            results = [atomicgroup for atomicgroup in results
                       if not atomicgroup['invalid']]

        keys = ['name', 'description', 'max_number_of_machines', 'invalid']
        super(atomicgroup_list, self).output(results, keys)


class atomicgroup_create(action_common.atest_create, atomicgroup):
    def __init__(self):
        super(atomicgroup_create, self).__init__()
        self.parser.add_option('-n', '--max_number_of_machines',
                               help='Maximum # of machines for this group.',
                               type='int')
        self.parser.add_option('-d', '--description',
                               help='Description of this atomic group.',
                               type='string',
                               default='')


    def parse(self):
        options, leftover = super(atomicgroup_create, self).parse()
        self.data_item_key = 'name'
        self.data['description'] = options.description
        self.data['max_number_of_machines'] = options.max_number_of_machines
        return options, leftover


class atomicgroup_delete(action_common.atest_delete, atomicgroup):
    """atest atomicgroup delete <atomicgroup>"""
    pass


class atomicgroup_add_or_remove(atomicgroup):

    def __init__(self):
        super(atomicgroup_add_or_remove, self).__init__()
        # .add_remove_things is used by action_common.atest_add_or_remove.
        self.add_remove_things = {'labels': 'label'}
        lower_words = tuple(word.lower() for word in self.usage_words)
        self.parser.add_option('-l', '--label',
                               help=('%s LABELS(s) %s the ATOMIC GROUP.' %
                                     self.usage_words),
                               type='string',
                               metavar='LABEL')
        self.parser.add_option('-L', '--label_list',
                               help='File containing LABELs to %s %s '
                               'the ATOMIC GROUP.' % lower_words,
                               type='string',
                               metavar='LABEL_FLIST')


    def parse(self):
        label_info = topic_common.item_parse_info(attribute_name='labels',
                                                  inline_option='label',
                                                  filename_option='label_list')

        options, leftover = super(atomicgroup_add_or_remove,
                                  self).parse([label_info],
                                              req_items='atomicgroups')
        if not getattr(self, 'labels', None):
            self.invalid_syntax('%s %s requires at least one label' %
                                (self.msg_topic,
                                 self.usage_action))
        return options, leftover


class atomicgroup_add(action_common.atest_add, atomicgroup_add_or_remove):
    """atest atomicgroup add <atomicgroup>
     [--label <label>] [--label_list <file>]"""
    pass


class atomicgroup_remove(action_common.atest_remove, atomicgroup_add_or_remove):
    """atest atomicgroup remove <atomicgroup>
     [--label <label>] [--label_list <file>]"""
    pass