• 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 logging
9import os
10import re
11
12import common
13from autotest_lib.client.cros import constants
14
15
16JETSTREAM_BOARDS = frozenset(['arkham', 'gale', 'whirlwind'])
17
18
19def _lsbrelease_search(regex, group_id=0, lsb_release_content=None):
20    """Searches /etc/lsb-release for a regex match.
21
22    @param regex: Regex to match.
23    @param group_id: The group in the regex we are searching for.
24                     Default is group 0.
25    @param lsb_release_content: A string represents the content of lsb-release.
26            If the caller is from drone, it can pass in the file content here.
27
28    @returns the string in the specified group if there is a match or None if
29             not found.
30
31    @raises IOError if /etc/lsb-release can not be accessed.
32    """
33    if lsb_release_content is None:
34        with open(constants.LSB_RELEASE) as lsb_release_file:
35            lsb_release_content = lsb_release_file.read()
36    for line in lsb_release_content.split('\n'):
37        m = re.match(regex, line)
38        if m:
39            return m.group(group_id)
40    return None
41
42
43def get_current_board(lsb_release_content=None):
44    """Return the current board name.
45
46    @param lsb_release_content: A string represents the content of lsb-release.
47            If the caller is from drone, it can pass in the file content here.
48
49    @return current board name, e.g "lumpy", None on fail.
50    """
51    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1,
52                              lsb_release_content=lsb_release_content)
53
54
55def get_chromeos_release_version(lsb_release_content=None):
56    """Get chromeos version in device under test as string. None on fail.
57
58    @param lsb_release_content: A string represents the content of lsb-release.
59            If the caller is from drone, it can pass in the file content here.
60
61    @return chromeos version in device under test as string. None on fail.
62    """
63    return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1,
64                              lsb_release_content=lsb_release_content)
65
66
67def get_chromeos_release_milestone(lsb_release_content=None):
68    """Get chromeos milestone in device under test as string. None on fail.
69
70    @param lsb_release_content: A string represents the content of lsb-release.
71            If the caller is from drone, it can pass in the file content here.
72
73    @return chromeos release milestone in device under test as string.
74            None on fail.
75    """
76    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
77                              group_id=1,
78                              lsb_release_content=lsb_release_content)
79
80
81def is_moblab(lsb_release_content=None):
82    """Return if we are running on a Moblab system or not.
83
84    @param lsb_release_content: A string represents the content of lsb-release.
85            If the caller is from drone, it can pass in the file content here.
86
87    @return the board string if this is a Moblab device or None if it is not.
88    """
89    if lsb_release_content is not None:
90        return _lsbrelease_search(r'.*moblab',
91                                  lsb_release_content=lsb_release_content)
92
93    if os.path.exists(constants.LSB_RELEASE):
94        return _lsbrelease_search(r'.*moblab')
95
96    try:
97        from chromite.lib import cros_build_lib
98        if cros_build_lib.IsInsideChroot():
99            return None
100    except ImportError as e:
101        logging.error('Unable to determine if this is a moblab system: %s', e)
102
103
104def is_jetstream(lsb_release_content=None):
105    """Parses lsb_contents to determine if the host is a Jetstream host.
106
107    @param lsb_release_content: The string contents of lsb-release.
108            If None, the local lsb-release is used.
109
110    @return True if the host is a Jetstream device, otherwise False.
111    """
112    board = get_current_board(lsb_release_content=lsb_release_content)
113    return board in JETSTREAM_BOARDS
114
115
116def get_chrome_milestone(lsb_release_content=None):
117    """Get the value for the Chrome milestone.
118
119    @param lsb_release_content: A string represents the content of lsb-release.
120            If the caller is from drone, it can pass in the file content here.
121
122    @return the value for the Chrome milestone
123    """
124    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
125                              group_id=1,
126                              lsb_release_content=lsb_release_content)
127
128
129def get_device_type(lsb_release_content=None):
130    """Get the device type string, e.g. "CHROMEBOOK" or "CHROMEBOX".
131
132    @param lsb_release_content: A string represents the content of lsb-release.
133            If the caller is from drone, it can pass in the file content here.
134
135    @return the DEVICETYPE value for this machine.
136    """
137    return _lsbrelease_search(r'^DEVICETYPE=(.+)$', group_id=1,
138                              lsb_release_content=lsb_release_content)
139
140
141def is_arc_available(lsb_release_content=None):
142    """Returns True if the device has ARC installed.
143
144    @param lsb_release_content: A string represents the content of lsb-release.
145            If the caller is from drone, it can pass in the file content here.
146
147    @return True if the device has ARC installed.
148    """
149    return (_lsbrelease_search(r'^CHROMEOS_ARC_VERSION',
150                               lsb_release_content=lsb_release_content)
151            is not None)
152