xref: /aosp_15_r20/external/autotest/client/common_lib/lsbrelease_utils.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright 2015 The Chromium 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 module provides helper method to parse /etc/lsb-release file to extract
6# various information.
7
8import os
9import re
10
11import common
12from autotest_lib.client.cros import constants
13
14
15JETSTREAM_BOARDS = frozenset(['arkham', 'gale', 'mistral', 'whirlwind'])
16
17def _lsbrelease_search(regex, group_id=0, lsb_release_content=None):
18    """Searches /etc/lsb-release for a regex match.
19
20    @param regex: Regex to match.
21    @param group_id: The group in the regex we are searching for.
22                     Default is group 0.
23    @param lsb_release_content: A string represents the content of lsb-release.
24            If the caller is from drone, it can pass in the file content here.
25
26    @returns the string in the specified group if there is a match or None if
27             not found.
28
29    @raises IOError if /etc/lsb-release can not be accessed.
30    """
31    if lsb_release_content is None:
32        with open(constants.LSB_RELEASE) as lsb_release_file:
33            lsb_release_content = lsb_release_file.read()
34
35    if type(lsb_release_content) == type(b' '):
36        lsb_release_content = lsb_release_content.decode("utf-8")
37    for line in lsb_release_content.split('\n'):
38        m = re.match(regex, line)
39        if m:
40            return m.group(group_id)
41    return None
42
43
44def get_current_board(lsb_release_content=None):
45    """Return the current board name.
46
47    @param lsb_release_content: A string represents the content of lsb-release.
48            If the caller is from drone, it can pass in the file content here.
49
50    @return current board name, e.g "lumpy", None on fail.
51    """
52    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1,
53                              lsb_release_content=lsb_release_content)
54
55
56def get_chromeos_channel(lsb_release_content=None):
57    """Get chromeos channel in device under test as string. None on fail.
58
59    @param lsb_release_content: A string represents the content of lsb-release.
60            If the caller is from drone, it can pass in the file content here.
61
62    @return chromeos channel in device under test as string. None on fail.
63    """
64    return _lsbrelease_search(
65        r'^CHROMEOS_RELEASE_DESCRIPTION=.+ (.+)-channel.*$',
66        group_id=1, lsb_release_content=lsb_release_content)
67
68
69def get_chromeos_release_version(lsb_release_content=None):
70    """Get chromeos version in device under test as string. None on fail.
71
72    @param lsb_release_content: A string represents the content of lsb-release.
73            If the caller is from drone, it can pass in the file content here.
74
75    @return chromeos version in device under test as string. None on fail.
76    """
77    return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1,
78                              lsb_release_content=lsb_release_content)
79
80
81def get_chromeos_release_builder_path(lsb_release_content=None):
82    """Get chromeos builder path from device under test as string.
83
84    @param lsb_release_content: A string representing the content of
85            lsb-release. If the caller is from drone, it can pass in the file
86            content here.
87
88    @return chromeos builder path in device under test as string. None on fail.
89    """
90    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BUILDER_PATH=(.+)$',
91                              group_id=1,
92                              lsb_release_content=lsb_release_content)
93
94
95def get_chromeos_release_milestone(lsb_release_content=None):
96    """Get chromeos milestone in device under test as string. None on fail.
97
98    @param lsb_release_content: A string represents the content of lsb-release.
99            If the caller is from drone, it can pass in the file content here.
100
101    @return chromeos release milestone in device under test as string.
102            None on fail.
103    """
104    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
105                              group_id=1,
106                              lsb_release_content=lsb_release_content)
107
108
109def is_moblab(lsb_release_content=None):
110    """Return if we are running on a Moblab system or not.
111
112    @param lsb_release_content: A string represents the content of lsb-release.
113            If the caller is from drone, it can pass in the file content here.
114
115    @return the board string if this is a Moblab device or None if it is not.
116    """
117    return 'MOBLAB' in os.environ
118
119
120def is_jetstream(lsb_release_content=None):
121    """Parses lsb_contents to determine if the host is a Jetstream host.
122
123    @param lsb_release_content: The string contents of lsb-release.
124            If None, the local lsb-release is used.
125
126    @return True if the host is a Jetstream device, otherwise False.
127    """
128    board = get_current_board(lsb_release_content=lsb_release_content)
129    return board in JETSTREAM_BOARDS
130
131def is_gce_board(lsb_release_content=None):
132    """Parses lsb_contents to determine if host is a GCE board.
133
134    @param lsb_release_content: The string contents of lsb-release.
135            If None, the local lsb-release is used.
136
137    @return True if the host is a GCE board otherwise False.
138    """
139    return is_lakitu(lsb_release_content=lsb_release_content)
140
141def is_lakitu(lsb_release_content=None):
142    """Parses lsb_contents to determine if host is lakitu.
143
144    @param lsb_release_content: The string contents of lsb-release.
145            If None, the local lsb-release is used.
146
147    @return True if the host is lakitu otherwise False.
148    """
149    board = get_current_board(lsb_release_content=lsb_release_content)
150    if board is not None:
151        return board.startswith('lakitu')
152    return False
153
154def get_chrome_milestone(lsb_release_content=None):
155    """Get the value for the Chrome milestone.
156
157    @param lsb_release_content: A string represents the content of lsb-release.
158            If the caller is from drone, it can pass in the file content here.
159
160    @return the value for the Chrome milestone
161    """
162    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
163                              group_id=1,
164                              lsb_release_content=lsb_release_content)
165
166
167def get_device_type(lsb_release_content=None):
168    """Get the device type string, e.g. "CHROMEBOOK" or "CHROMEBOX".
169
170    @param lsb_release_content: A string represents the content of lsb-release.
171            If the caller is from drone, it can pass in the file content here.
172
173    @return the DEVICETYPE value for this machine.
174    """
175    return _lsbrelease_search(r'^DEVICETYPE=(.+)$', group_id=1,
176                              lsb_release_content=lsb_release_content)
177
178
179def is_arc_available(lsb_release_content=None):
180    """Returns True if the device has ARC installed.
181
182    @param lsb_release_content: A string represents the content of lsb-release.
183            If the caller is from drone, it can pass in the file content here.
184
185    @return True if the device has ARC installed.
186    """
187    return (_lsbrelease_search(r'^CHROMEOS_ARC_VERSION',
188                               lsb_release_content=lsb_release_content)
189            is not None)
190