blob: 11b57261826b7e320fd28e5bdc3578cb30baf03c [file] [log] [blame]
Dan Shi25e1fd42014-12-19 14:36:42 -08001# Copyright 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""
6This module contains functions to get or update stable version for a given
7board.
8
9The valid actions are:
10list: Show version of a given board or list all boards and their stable
11 versions if --board option is not specified.
12modify: Set the stable version of a given board to the given value.
13delete: Delete the stable version of a given board. So its stable version will
14 use the value for board `DEFAULT`.
15"""
16
17import common
18
19from autotest_lib.cli import topic_common
20
21
22class stable_version(topic_common.atest):
23 """stable_version class
24
25 atest stable_version [list|delete|modify] <options>
26 """
27 usage_action = '[list|delete|modify]'
28 topic = msg_topic = 'stable_version'
29 msg_items = '<stable_version>'
30
31 def __init__(self):
32 """Add to the parser the options common to all the
33 stable_version actions.
34 """
35 super(stable_version, self).__init__()
36
37 self.parser.add_option('-b', '--board',
38 help='Name of the board',
39 type='string',
40 default=None,
41 metavar='BOARD')
42
43 self.topic_parse_info = topic_common.item_parse_info(
44 attribute_name='board', use_leftover=True)
45
46
47 def parse(self):
48 """Parse command arguments.
49 """
50 board_info = topic_common.item_parse_info(attribute_name='board')
51 (options, leftover) = super(stable_version, self).parse([board_info])
52
53 self.board = options.board
54 return (options, leftover)
55
56
57 def output(self, results):
58 """Display output.
59
60 For most actions, the return is a string message, no formating needed.
61
62 @param results: return of the execute call.
63 """
64 if results:
65 print results
66
67
68class stable_version_help(stable_version):
69 """Just here to get the atest logic working. Usage is set by its parent.
70 """
71 pass
72
73
74class stable_version_list(stable_version):
75 """atest stable_version list [--board <board>]"""
76
77 def execute(self):
78 """Execute list stable version action.
79 """
80 if self.board:
81 version = self.execute_rpc(op='get_stable_version',
82 board=self.board)
83 return {self.board: version}
84 else:
85 return self.execute_rpc(op='get_all_stable_versions')
86
87
88 def output(self, results):
89 """Display output.
90
91 @param results: A dictionary of board:version.
92 """
93 format = '%-12s| %-20s'
94 print '='*30
95 print format % ('board', 'version')
96 print '-'*30
97 for board,version in results.iteritems():
98 print format % (board, version)
99 print '='*30
100
101
102class stable_version_modify(stable_version):
103 """atest stable_version modify --board <board> --version <version>
104
105 Change the stable version of a given board to the given value.
106 """
107
108 def __init__(self):
109 """Add to the parser the options common to all the
110 stable_version actions.
111 """
112 super(stable_version_modify, self).__init__()
113
114 self.parser.add_option('-i', '--version',
115 help='Stable version.',
116 type='string',
117 metavar='VERSION')
118
119 self.topic_parse_info = topic_common.item_parse_info(
120 attribute_name='board', use_leftover=True)
121
122
123 def parse(self):
124 """Parse command arguments.
125 """
126 options,leftover = super(stable_version_modify, self).parse()
127
128 self.version = options.version
129 if not self.board or not self.version:
130 self.invalid_syntax('Both --board and --version arguments must be '
131 'specified.')
132
133
134 def execute(self):
135 """Execute delete stable version action.
136 """
137 current_version = self.execute_rpc(op='get_stable_version',
138 board=self.board)
139 if current_version == self.version:
140 print ('Board %s already has stable version of %s.' %
141 (self.board, self.version))
142 return
143
144 self.execute_rpc(op='set_stable_version', board=self.board,
145 version=self.version)
146 print ('Stable version for board %s is changed from %s to %s.' %
147 (self.board, current_version, self.version))
148
149
150class stable_version_delete(stable_version):
151 """atest stable_version delete --board <board>
152
153 Delete a stable version entry in afe_stable_versions table for a given
154 board, so default stable version will be used.
155 """
156
157 def parse(self):
158 """Parse command arguments.
159 """
160 super(stable_version_delete, self).parse()
161 if not self.board:
162 self.invalid_syntax('`board` argument must be specified to delete '
163 'a stable version entry.')
164 if self.board == 'DEFAULT':
165 self.invalid_syntax('Stable version for board DEFAULT can not be '
166 'deleted.')
167
168
169 @topic_common.atest.require_confirmation(
170 'Are you sure to delete stable version for the given board?')
171 def execute(self):
172 """Execute delete stable version action.
173 """
174 self.execute_rpc(op='delete_stable_version', board=self.board)
175 print 'Stable version for board %s is deleted.' % self.board
176 default_stable_version = self.execute_rpc(op='get_stable_version')
177 print ('Stable version for board %s is default to %s' %
178 (self.board, default_stable_version))