• 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
16def _lsbrelease_search(regex, group_id=0, lsb_release_content=None):
17    """Searches /etc/lsb-release for a regex match.
18
19    @param regex: Regex to match.
20    @param group_id: The group in the regex we are searching for.
21                     Default is group 0.
22    @param lsb_release_content: A string represents the content of lsb-release.
23            If the caller is from drone, it can pass in the file content here.
24
25    @returns the string in the specified group if there is a match or None if
26             not found.
27
28    @raises IOError if /etc/lsb-release can not be accessed.
29    """
30    if not lsb_release_content:
31        with open(constants.LSB_RELEASE) as lsb_release_file:
32            lsb_release_content = lsb_release_file.read()
33    for line in lsb_release_content.split('\n'):
34        m = re.match(regex, line)
35        if m:
36            return m.group(group_id)
37    return None
38
39
40def get_current_board(lsb_release_content=None):
41    """Return the current board name.
42
43    @param lsb_release_content: A string represents the content of lsb-release.
44            If the caller is from drone, it can pass in the file content here.
45
46    @return current board name, e.g "lumpy", None on fail.
47    """
48    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1,
49                              lsb_release_content=lsb_release_content)
50
51
52def get_chromeos_release_version(lsb_release_content=None):
53    """Get chromeos version in device under test as string. None on fail.
54
55    @param lsb_release_content: A string represents the content of lsb-release.
56            If the caller is from drone, it can pass in the file content here.
57
58    @return chromeos version in device under test as string. None on fail.
59    """
60    return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1,
61                              lsb_release_content=lsb_release_content)
62
63
64def is_moblab(lsb_release_content=None):
65    """Return if we are running on a Moblab system or not.
66
67    @param lsb_release_content: A string represents the content of lsb-release.
68            If the caller is from drone, it can pass in the file content here.
69
70    @return the board string if this is a Moblab device or None if it is not.
71    """
72    if os.path.exists(constants.LSB_RELEASE):
73        return _lsbrelease_search(
74                r'.*moblab', lsb_release_content=lsb_release_content)
75    try:
76        from chromite.lib import cros_build_lib
77        if cros_build_lib.IsInsideChroot():
78            return None
79    except ImportError as e:
80        logging.error('Unable to determine if this is a moblab system: %s', e)
81
82
83def get_chrome_milestone(lsb_release_content=None):
84    """Get the value for the Chrome milestone.
85
86    @param lsb_release_content: A string represents the content of lsb-release.
87            If the caller is from drone, it can pass in the file content here.
88
89    @return the value for the Chrome milestone
90    """
91    return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$',
92                              group_id=1,
93                              lsb_release_content=lsb_release_content)
94