xref: /aosp_15_r20/external/autotest/site_utils/stable_version_utils.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1*9c5db199SXin Li# Copyright 2014 The Chromium OS Authors. All rights reserved.
2*9c5db199SXin Li# Use of this source code is governed by a BSD-style license that can be
3*9c5db199SXin Li# found in the LICENSE file.
4*9c5db199SXin Li
5*9c5db199SXin Li# This file contains utility functions to get and set stable versions for given
6*9c5db199SXin Li# boards.
7*9c5db199SXin Li
8*9c5db199SXin Liimport common
9*9c5db199SXin Liimport logging
10*9c5db199SXin Liimport django.core.exceptions
11*9c5db199SXin Lifrom autotest_lib.client.common_lib import global_config
12*9c5db199SXin Lifrom autotest_lib.frontend import setup_django_environment
13*9c5db199SXin Lifrom autotest_lib.frontend.afe import models
14*9c5db199SXin Li
15*9c5db199SXin Li
16*9c5db199SXin Li# Name of the default board. For boards that don't have stable version
17*9c5db199SXin Li# explicitly set, version for the default board will be used.
18*9c5db199SXin LiDEFAULT = 'DEFAULT'
19*9c5db199SXin Li
20*9c5db199SXin Li# Type of metadata to store stable_version changes.
21*9c5db199SXin Li_STABLE_VERSION_TYPE = 'stable_version'
22*9c5db199SXin Li
23*9c5db199SXin Lidef get_all():
24*9c5db199SXin Li    """Get stable versions of all boards.
25*9c5db199SXin Li
26*9c5db199SXin Li    @return: A dictionary of boards and stable versions.
27*9c5db199SXin Li    """
28*9c5db199SXin Li    versions = dict([(v.board, v.version)
29*9c5db199SXin Li                     for v in models.StableVersion.objects.all()])
30*9c5db199SXin Li    # Set default to the global config value of CROS.stable_cros_version if
31*9c5db199SXin Li    # there is no entry in afe_stable_versions table.
32*9c5db199SXin Li    if not versions:
33*9c5db199SXin Li        versions = {DEFAULT: global_config.global_config.get_config_value(
34*9c5db199SXin Li                            'CROS', 'stable_cros_version')}
35*9c5db199SXin Li    return versions
36*9c5db199SXin Li
37*9c5db199SXin Li
38*9c5db199SXin Lidef get(board=DEFAULT):
39*9c5db199SXin Li    """Get stable version for the given board.
40*9c5db199SXin Li
41*9c5db199SXin Li    @param board: Name of the board, default to value `DEFAULT`.
42*9c5db199SXin Li
43*9c5db199SXin Li    @return: Stable version of the given board. If the given board is not listed
44*9c5db199SXin Li             in afe_stable_versions table, DEFAULT will be used.
45*9c5db199SXin Li             Return global_config value of CROS.stable_cros_version if
46*9c5db199SXin Li             afe_stable_versions table does not have entry of board DEFAULT.
47*9c5db199SXin Li    """
48*9c5db199SXin Li    try:
49*9c5db199SXin Li        return models.StableVersion.objects.get(board=board).version
50*9c5db199SXin Li    except django.core.exceptions.ObjectDoesNotExist:
51*9c5db199SXin Li        if board == DEFAULT:
52*9c5db199SXin Li            return global_config.global_config.get_config_value(
53*9c5db199SXin Li                    'CROS', 'stable_cros_version')
54*9c5db199SXin Li        else:
55*9c5db199SXin Li            return get(board=DEFAULT)
56*9c5db199SXin Li
57*9c5db199SXin Li
58*9c5db199SXin Lidef set(version, board=DEFAULT):
59*9c5db199SXin Li    """Set stable version for the given board.
60*9c5db199SXin Li
61*9c5db199SXin Li    @param version: The new value of stable version for given board.
62*9c5db199SXin Li    @param board: Name of the board, default to value `DEFAULT`.
63*9c5db199SXin Li    """
64*9c5db199SXin Li
65*9c5db199SXin Li    logging.warning("stable_version_utils::set: attmpted to set stable version. setting the stable version is not permitted")
66*9c5db199SXin Li    return None
67*9c5db199SXin Li
68*9c5db199SXin Li
69*9c5db199SXin Lidef delete(board):
70*9c5db199SXin Li    """Delete stable version record for the given board.
71*9c5db199SXin Li
72*9c5db199SXin Li    @param board: Name of the board.
73*9c5db199SXin Li    """
74*9c5db199SXin Li    logging.warning("stable_version_utils::set: attmpted to delete stable version. deleting the stable version is not permitted")
75*9c5db199SXin Li    return None
76