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