blob: d887fa3544c3ecb40986310cc01e8b3e544b0551 [file] [log] [blame]
Dan Shi6964fa52014-12-18 11:04:27 -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# This file contains utility functions to get and set stable versions for given
6# boards.
7
8import common
9import django.core.exceptions
10from autotest_lib.client.common_lib import global_config
11from autotest_lib.frontend import setup_django_environment
12from 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.
17DEFAULT = 'DEFAULT'
18
Dan Shi25e1fd42014-12-19 14:36:42 -080019# Type of metadata to store stable_version changes.
20_STABLE_VERSION_TYPE = 'stable_version'
21
Dan Shi6964fa52014-12-18 11:04:27 -080022def get_all():
23 """Get stable versions of all boards.
24
25 @return: A dictionary of boards and stable versions.
26 """
Dan Shi25e1fd42014-12-19 14:36:42 -080027 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 Shi6964fa52014-12-18 11:04:27 -080035
36
Prathmesh Prabhu5fc13142018-07-16 21:07:54 +000037def get(board=DEFAULT, android=False):
Dan Shi6964fa52014-12-18 11:04:27 -080038 """Get stable version for the given board.
39
40 @param board: Name of the board, default to value `DEFAULT`.
Prathmesh Prabhu5fc13142018-07-16 21:07:54 +000041 @param android: If True, indicates we are looking up a Android/Brillo-based
42 board. There is no default version that works for all
43 Android/Brillo boards. If False, we are looking up a Chrome
44 OS based board.
Simran Basibeb2bb22016-02-03 15:25:48 -080045
Dan Shi25e1fd42014-12-19 14:36:42 -080046 @return: Stable version of the given board. If the given board is not listed
47 in afe_stable_versions table, DEFAULT will be used.
48 Return global_config value of CROS.stable_cros_version if
49 afe_stable_versions table does not have entry of board DEFAULT.
Dan Shi6964fa52014-12-18 11:04:27 -080050 """
Prathmesh Prabhu5fc13142018-07-16 21:07:54 +000051 if board == DEFAULT and android:
52 return None
Dan Shi6964fa52014-12-18 11:04:27 -080053 try:
54 return models.StableVersion.objects.get(board=board).version
55 except django.core.exceptions.ObjectDoesNotExist:
56 if board == DEFAULT:
57 return global_config.global_config.get_config_value(
58 'CROS', 'stable_cros_version')
Prathmesh Prabhu5fc13142018-07-16 21:07:54 +000059 elif android:
60 return global_config.global_config.get_config_value(
61 'ANDROID', 'stable_version_%s' % board, default=None)
Dan Shi6964fa52014-12-18 11:04:27 -080062 else:
Dan Shi25e1fd42014-12-19 14:36:42 -080063 return get(board=DEFAULT)
64
65
66def set(version, board=DEFAULT):
67 """Set stable version for the given board.
68
69 @param version: The new value of stable version for given board.
70 @param board: Name of the board, default to value `DEFAULT`.
71 """
72 try:
73 stable_version = models.StableVersion.objects.get(board=board)
74 stable_version.version = version
75 stable_version.save()
76 except django.core.exceptions.ObjectDoesNotExist:
77 models.StableVersion.objects.create(board=board, version=version)
Dan Shi25e1fd42014-12-19 14:36:42 -080078
79
80def delete(board):
81 """Delete stable version record for the given board.
82
83 @param board: Name of the board.
84 """
85 stable_version = models.StableVersion.objects.get(board=board)
86 stable_version.delete()