Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -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 | # This file contains utility functions to get and set stable versions for given |
| 6 | # boards. |
| 7 | |
| 8 | import common |
| 9 | import django.core.exceptions |
| 10 | from autotest_lib.client.common_lib import global_config |
| 11 | from autotest_lib.frontend import setup_django_environment |
| 12 | from autotest_lib.frontend.afe import models |
| 13 | |
| 14 | |
| 15 | # Name of the default board. For boards that don't have stable version |
| 16 | # explicitly set, version for the default board will be used. |
| 17 | DEFAULT = 'DEFAULT' |
| 18 | |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 19 | # Type of metadata to store stable_version changes. |
| 20 | _STABLE_VERSION_TYPE = 'stable_version' |
| 21 | |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 22 | def get_all(): |
| 23 | """Get stable versions of all boards. |
| 24 | |
| 25 | @return: A dictionary of boards and stable versions. |
| 26 | """ |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 27 | versions = dict([(v.board, v.version) |
| 28 | for v in models.StableVersion.objects.all()]) |
| 29 | # Set default to the global config value of CROS.stable_cros_version if |
| 30 | # there is no entry in afe_stable_versions table. |
| 31 | if not versions: |
| 32 | versions = {DEFAULT: global_config.global_config.get_config_value( |
| 33 | 'CROS', 'stable_cros_version')} |
| 34 | return versions |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 35 | |
| 36 | |
Richard Barnette | e4f9399 | 2018-08-04 07:28:33 -0700 | [diff] [blame] | 37 | def get(board=DEFAULT): |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 38 | """Get stable version for the given board. |
| 39 | |
| 40 | @param board: Name of the board, default to value `DEFAULT`. |
Simran Basi | beb2bb2 | 2016-02-03 15:25:48 -0800 | [diff] [blame] | 41 | |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 42 | @return: Stable version of the given board. If the given board is not listed |
| 43 | in afe_stable_versions table, DEFAULT will be used. |
| 44 | Return global_config value of CROS.stable_cros_version if |
| 45 | afe_stable_versions table does not have entry of board DEFAULT. |
Dan Shi | 6964fa5 | 2014-12-18 11:04:27 -0800 | [diff] [blame] | 46 | """ |
| 47 | try: |
| 48 | return models.StableVersion.objects.get(board=board).version |
| 49 | except django.core.exceptions.ObjectDoesNotExist: |
| 50 | if board == DEFAULT: |
| 51 | return global_config.global_config.get_config_value( |
| 52 | 'CROS', 'stable_cros_version') |
| 53 | else: |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 54 | return get(board=DEFAULT) |
| 55 | |
| 56 | |
| 57 | def set(version, board=DEFAULT): |
| 58 | """Set stable version for the given board. |
| 59 | |
| 60 | @param version: The new value of stable version for given board. |
| 61 | @param board: Name of the board, default to value `DEFAULT`. |
| 62 | """ |
| 63 | try: |
| 64 | stable_version = models.StableVersion.objects.get(board=board) |
| 65 | stable_version.version = version |
| 66 | stable_version.save() |
| 67 | except django.core.exceptions.ObjectDoesNotExist: |
| 68 | models.StableVersion.objects.create(board=board, version=version) |
Dan Shi | 25e1fd4 | 2014-12-19 14:36:42 -0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def delete(board): |
| 72 | """Delete stable version record for the given board. |
| 73 | |
| 74 | @param board: Name of the board. |
| 75 | """ |
| 76 | stable_version = models.StableVersion.objects.get(board=board) |
| 77 | stable_version.delete() |