Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 1 | # 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 | """ |
| 6 | This module contains functions to get or update stable version for a given |
| 7 | board. |
| 8 | |
| 9 | The valid actions are: |
| 10 | list: Show version of a given board or list all boards and their stable |
| 11 | versions if --board option is not specified. |
| 12 | modify: Set the stable version of a given board to the given value. |
| 13 | delete: Delete the stable version of a given board. So its stable version will |
| 14 | use the value for board `DEFAULT`. |
| 15 | """ |
| 16 | |
| 17 | import common |
| 18 | |
| 19 | from autotest_lib.cli import topic_common |
| 20 | |
| 21 | |
| 22 | class 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 | |
| 68 | class 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 | |
| 74 | class 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 | """ |
J. Richard Barnette | 5ab3e83 | 2015-11-24 10:52:34 -0800 | [diff] [blame] | 93 | board_columns = max([len(s) for s in results.keys()]) |
| 94 | version_columns = max([len(s) for s in results.values()]) |
| 95 | total_columns = board_columns + version_columns + 3 |
| 96 | format = '%%-%ds | %%s' % board_columns |
| 97 | print '=' * total_columns |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 98 | print format % ('board', 'version') |
J. Richard Barnette | 5ab3e83 | 2015-11-24 10:52:34 -0800 | [diff] [blame] | 99 | print '-' * total_columns |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 100 | for board,version in results.iteritems(): |
| 101 | print format % (board, version) |
J. Richard Barnette | 5ab3e83 | 2015-11-24 10:52:34 -0800 | [diff] [blame] | 102 | print '=' * total_columns |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 103 | |
| 104 | |
| 105 | class stable_version_modify(stable_version): |
| 106 | """atest stable_version modify --board <board> --version <version> |
| 107 | |
| 108 | Change the stable version of a given board to the given value. |
| 109 | """ |
| 110 | |
| 111 | def __init__(self): |
| 112 | """Add to the parser the options common to all the |
| 113 | stable_version actions. |
| 114 | """ |
| 115 | super(stable_version_modify, self).__init__() |
| 116 | |
| 117 | self.parser.add_option('-i', '--version', |
| 118 | help='Stable version.', |
| 119 | type='string', |
| 120 | metavar='VERSION') |
| 121 | |
| 122 | self.topic_parse_info = topic_common.item_parse_info( |
| 123 | attribute_name='board', use_leftover=True) |
| 124 | |
| 125 | |
| 126 | def parse(self): |
| 127 | """Parse command arguments. |
| 128 | """ |
| 129 | options,leftover = super(stable_version_modify, self).parse() |
| 130 | |
| 131 | self.version = options.version |
| 132 | if not self.board or not self.version: |
| 133 | self.invalid_syntax('Both --board and --version arguments must be ' |
| 134 | 'specified.') |
| 135 | |
| 136 | |
| 137 | def execute(self): |
| 138 | """Execute delete stable version action. |
| 139 | """ |
| 140 | current_version = self.execute_rpc(op='get_stable_version', |
| 141 | board=self.board) |
| 142 | if current_version == self.version: |
| 143 | print ('Board %s already has stable version of %s.' % |
| 144 | (self.board, self.version)) |
| 145 | return |
| 146 | |
| 147 | self.execute_rpc(op='set_stable_version', board=self.board, |
| 148 | version=self.version) |
| 149 | print ('Stable version for board %s is changed from %s to %s.' % |
| 150 | (self.board, current_version, self.version)) |
| 151 | |
| 152 | |
| 153 | class stable_version_delete(stable_version): |
| 154 | """atest stable_version delete --board <board> |
| 155 | |
| 156 | Delete a stable version entry in afe_stable_versions table for a given |
| 157 | board, so default stable version will be used. |
| 158 | """ |
| 159 | |
| 160 | def parse(self): |
| 161 | """Parse command arguments. |
| 162 | """ |
| 163 | super(stable_version_delete, self).parse() |
| 164 | if not self.board: |
| 165 | self.invalid_syntax('`board` argument must be specified to delete ' |
| 166 | 'a stable version entry.') |
| 167 | if self.board == 'DEFAULT': |
| 168 | self.invalid_syntax('Stable version for board DEFAULT can not be ' |
| 169 | 'deleted.') |
| 170 | |
| 171 | |
| 172 | @topic_common.atest.require_confirmation( |
| 173 | 'Are you sure to delete stable version for the given board?') |
| 174 | def execute(self): |
| 175 | """Execute delete stable version action. |
| 176 | """ |
| 177 | self.execute_rpc(op='delete_stable_version', board=self.board) |
| 178 | print 'Stable version for board %s is deleted.' % self.board |
| 179 | default_stable_version = self.execute_rpc(op='get_stable_version') |
| 180 | print ('Stable version for board %s is default to %s' % |
| 181 | (self.board, default_stable_version)) |