• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    for line in lsb_release_content.split('\n'):
35        m = re.match(regex, line)
36        if m:
37            return m.group(group_id)
38    return None
39
40
41def get_current_board(lsb_release_content=None):
42    """Return the current board name.
43
44    @param lsb_release_content: A string represents the content of lsb-release.
45            If the caller is from drone, it can pass in the file content here.
46
47    @return current board name, e.g "lumpy", None on fail.
48    """
49    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1,
50                              lsb_release_content=lsb_release_content)
51
52
53def get_chromeos_channel(lsb_release_content=None):
54    """Get chromeos channel in device under test as string. None on fail.
55
56    @param lsb_release_content: A string represents the content of lsb-release.
57            If the caller is from drone, it can pass in the file content here.
58
59    @return chromeos channel in device under test as string. None on fail.
60    """
61    return _lsbrelease_search(
62        r'^CHROMEOS_RELEASE_DESCRIPTION=.+ (.+)-channel.*$',
63        group_id=1, lsb_release_content=lsb_release_content)
64
65
66def get_chromeos_release_version(lsb_release_content=None):
67    """Get chromeos version in device under test as string. None on fail.
68
69    @param lsb_release_content: A string represents the content of lsb-release.
70            If the caller is from drone, it can pass in the file content here.
71
72    @return chromeos version in device under test as string. None on fail.
73    """
74    return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1,
75                              lsb_release_content=lsb_release_content)
76
77
78def get_chromeos_release_builder_path(lsb_release_content=None):
79    """Get chromeos builder path from device under test as string.
80
81    @param lsb_release_content: A string representing the content of
82            lsb-release. If the caller is from drone, it can pass in the file
83            content here.
84
85    @return chromeos builder path in device under test as string. None on fail.
86    """
87    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BUILDER_PATH=(.+)$',
88                              group_id=1,
89                              lsb_release_content=lsb_release_content)
90
91
92def get_chromeos_release_milestone(lsb_release_content=None):
93    """Get chromeos milestone in device under test as string. None on fail.
94
95    @param lsb_release_content: A string represents the content of lsb-release.
96            If the caller is from drone, it can pass in the file content here.
97
98    @return chromeos release milestone in device under test as string.
99            None on fail.
100    """
101    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
102                              group_id=1,
103                              lsb_release_content=lsb_release_content)
104
105
106def is_moblab(lsb_release_content=None):
107    """Return if we are running on a Moblab system or not.
108
109    @param lsb_release_content: A string represents the content of lsb-release.
110            If the caller is from drone, it can pass in the file content here.
111
112    @return the board string if this is a Moblab device or None if it is not.
113    """
114    if lsb_release_content is not None:
115        return _lsbrelease_search(r'.*moblab',
116                                  lsb_release_content=lsb_release_content)
117
118    if os.path.exists(constants.LSB_RELEASE):
119        return _lsbrelease_search(r'.*moblab')
120
121
122def is_jetstream(lsb_release_content=None):
123    """Parses lsb_contents to determine if the host is a Jetstream host.
124
125    @param lsb_release_content: The string contents of lsb-release.
126            If None, the local lsb-release is used.
127
128    @return True if the host is a Jetstream device, otherwise False.
129    """
130    board = get_current_board(lsb_release_content=lsb_release_content)
131    return board in JETSTREAM_BOARDS
132
133def is_gce_board(lsb_release_content=None):
134    """Parses lsb_contents to determine if host is a GCE board.
135
136    @param lsb_release_content: The string contents of lsb-release.
137            If None, the local lsb-release is used.
138
139    @return True if the host is a GCE board otherwise False.
140    """
141    return is_lakitu(lsb_release_content=lsb_release_content)
142
143def is_lakitu(lsb_release_content=None):
144    """Parses lsb_contents to determine if host is lakitu.
145
146    @param lsb_release_content: The string contents of lsb-release.
147            If None, the local lsb-release is used.
148
149    @return True if the host is lakitu otherwise False.
150    """
151    board = get_current_board(lsb_release_content=lsb_release_content)
152    if board is not None:
153        return board.startswith('lakitu')
154    return False
155
156def get_chrome_milestone(lsb_release_content=None):
157    """Get the value for the Chrome milestone.
158
159    @param lsb_release_content: A string represents the content of lsb-release.
160            If the caller is from drone, it can pass in the file content here.
161
162    @return the value for the Chrome milestone
163    """
164    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
165                              group_id=1,
166                              lsb_release_content=lsb_release_content)
167
168
169def get_device_type(lsb_release_content=None):
170    """Get the device type string, e.g. "CHROMEBOOK" or "CHROMEBOX".
171
172    @param lsb_release_content: A string represents the content of lsb-release.
173            If the caller is from drone, it can pass in the file content here.
174
175    @return the DEVICETYPE value for this machine.
176    """
177    return _lsbrelease_search(r'^DEVICETYPE=(.+)$', group_id=1,
178                              lsb_release_content=lsb_release_content)
179
180
181def is_arc_available(lsb_release_content=None):
182    """Returns True if the device has ARC installed.
183
184    @param lsb_release_content: A string represents the content of lsb-release.
185            If the caller is from drone, it can pass in the file content here.
186
187    @return True if the device has ARC installed.
188    """
189    return (_lsbrelease_search(r'^CHROMEOS_ARC_VERSION',
190                               lsb_release_content=lsb_release_content)
191            is not None)
192