lsbrelease_utils.py revision 60cf6a9294dc53a9fe11dc9b28eaaea1ee69ca92
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 re
10
11import common
12from autotest_lib.client.cros import constants
13
14
15def _lsbrelease_search(regex, group_id=0):
16    """Searches /etc/lsb-release for a regex match.
17
18    @param regex: Regex to match.
19    @param group_id: The group in the regex we are searching for.
20                     Default is group 0.
21
22    @returns the string in the specified group if there is a match or None if
23             not found.
24
25    @raises IOError if /etc/lsb-release can not be accessed.
26    """
27    with open(constants.LSB_RELEASE) as lsb_release_file:
28        for line in lsb_release_file:
29            m = re.match(regex, line)
30            if m:
31                return m.group(group_id)
32    return None
33
34
35def get_current_board():
36    """Return the current board name.
37
38    @return current board name, e.g "lumpy", None on fail.
39    """
40    return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1)
41
42
43def get_chromeos_release_version():
44    """
45    @return chromeos version in device under test as string. None on fail.
46    """
47    return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1)
48
49
50def is_moblab():
51    """Return if we are running on a Moblab system or not.
52
53    @return the board string if this is a Moblab device or None if it is not.
54    """
55    try:
56        return _lsbrelease_search(r'.*moblab')
57    except IOError as e:
58        logging.error('Unable to determine if this is a moblab system: %s', e)