1*9c5db199SXin Li# Copyright 2015 The Chromium Authors. All rights reserved. 2*9c5db199SXin Li# Use of this source code is governed by a BSD-style license that can be 3*9c5db199SXin Li# found in the LICENSE file. 4*9c5db199SXin Li 5*9c5db199SXin Li# This module provides helper method to parse /etc/lsb-release file to extract 6*9c5db199SXin Li# various information. 7*9c5db199SXin Li 8*9c5db199SXin Liimport os 9*9c5db199SXin Liimport re 10*9c5db199SXin Li 11*9c5db199SXin Liimport common 12*9c5db199SXin Lifrom autotest_lib.client.cros import constants 13*9c5db199SXin Li 14*9c5db199SXin Li 15*9c5db199SXin LiJETSTREAM_BOARDS = frozenset(['arkham', 'gale', 'mistral', 'whirlwind']) 16*9c5db199SXin Li 17*9c5db199SXin Lidef _lsbrelease_search(regex, group_id=0, lsb_release_content=None): 18*9c5db199SXin Li """Searches /etc/lsb-release for a regex match. 19*9c5db199SXin Li 20*9c5db199SXin Li @param regex: Regex to match. 21*9c5db199SXin Li @param group_id: The group in the regex we are searching for. 22*9c5db199SXin Li Default is group 0. 23*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 24*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 25*9c5db199SXin Li 26*9c5db199SXin Li @returns the string in the specified group if there is a match or None if 27*9c5db199SXin Li not found. 28*9c5db199SXin Li 29*9c5db199SXin Li @raises IOError if /etc/lsb-release can not be accessed. 30*9c5db199SXin Li """ 31*9c5db199SXin Li if lsb_release_content is None: 32*9c5db199SXin Li with open(constants.LSB_RELEASE) as lsb_release_file: 33*9c5db199SXin Li lsb_release_content = lsb_release_file.read() 34*9c5db199SXin Li 35*9c5db199SXin Li if type(lsb_release_content) == type(b' '): 36*9c5db199SXin Li lsb_release_content = lsb_release_content.decode("utf-8") 37*9c5db199SXin Li for line in lsb_release_content.split('\n'): 38*9c5db199SXin Li m = re.match(regex, line) 39*9c5db199SXin Li if m: 40*9c5db199SXin Li return m.group(group_id) 41*9c5db199SXin Li return None 42*9c5db199SXin Li 43*9c5db199SXin Li 44*9c5db199SXin Lidef get_current_board(lsb_release_content=None): 45*9c5db199SXin Li """Return the current board name. 46*9c5db199SXin Li 47*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 48*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 49*9c5db199SXin Li 50*9c5db199SXin Li @return current board name, e.g "lumpy", None on fail. 51*9c5db199SXin Li """ 52*9c5db199SXin Li return _lsbrelease_search(r'^CHROMEOS_RELEASE_BOARD=(.+)$', group_id=1, 53*9c5db199SXin Li lsb_release_content=lsb_release_content) 54*9c5db199SXin Li 55*9c5db199SXin Li 56*9c5db199SXin Lidef get_chromeos_channel(lsb_release_content=None): 57*9c5db199SXin Li """Get chromeos channel in device under test as string. None on fail. 58*9c5db199SXin Li 59*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 60*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 61*9c5db199SXin Li 62*9c5db199SXin Li @return chromeos channel in device under test as string. None on fail. 63*9c5db199SXin Li """ 64*9c5db199SXin Li return _lsbrelease_search( 65*9c5db199SXin Li r'^CHROMEOS_RELEASE_DESCRIPTION=.+ (.+)-channel.*$', 66*9c5db199SXin Li group_id=1, lsb_release_content=lsb_release_content) 67*9c5db199SXin Li 68*9c5db199SXin Li 69*9c5db199SXin Lidef get_chromeos_release_version(lsb_release_content=None): 70*9c5db199SXin Li """Get chromeos version in device under test as string. None on fail. 71*9c5db199SXin Li 72*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 73*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 74*9c5db199SXin Li 75*9c5db199SXin Li @return chromeos version in device under test as string. None on fail. 76*9c5db199SXin Li """ 77*9c5db199SXin Li return _lsbrelease_search(r'^CHROMEOS_RELEASE_VERSION=(.+)$', group_id=1, 78*9c5db199SXin Li lsb_release_content=lsb_release_content) 79*9c5db199SXin Li 80*9c5db199SXin Li 81*9c5db199SXin Lidef get_chromeos_release_builder_path(lsb_release_content=None): 82*9c5db199SXin Li """Get chromeos builder path from device under test as string. 83*9c5db199SXin Li 84*9c5db199SXin Li @param lsb_release_content: A string representing the content of 85*9c5db199SXin Li lsb-release. If the caller is from drone, it can pass in the file 86*9c5db199SXin Li content here. 87*9c5db199SXin Li 88*9c5db199SXin Li @return chromeos builder path in device under test as string. None on fail. 89*9c5db199SXin Li """ 90*9c5db199SXin Li return _lsbrelease_search(r'^CHROMEOS_RELEASE_BUILDER_PATH=(.+)$', 91*9c5db199SXin Li group_id=1, 92*9c5db199SXin Li lsb_release_content=lsb_release_content) 93*9c5db199SXin Li 94*9c5db199SXin Li 95*9c5db199SXin Lidef get_chromeos_release_milestone(lsb_release_content=None): 96*9c5db199SXin Li """Get chromeos milestone in device under test as string. None on fail. 97*9c5db199SXin Li 98*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 99*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 100*9c5db199SXin Li 101*9c5db199SXin Li @return chromeos release milestone in device under test as string. 102*9c5db199SXin Li None on fail. 103*9c5db199SXin Li """ 104*9c5db199SXin Li return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$', 105*9c5db199SXin Li group_id=1, 106*9c5db199SXin Li lsb_release_content=lsb_release_content) 107*9c5db199SXin Li 108*9c5db199SXin Li 109*9c5db199SXin Lidef is_moblab(lsb_release_content=None): 110*9c5db199SXin Li """Return if we are running on a Moblab system or not. 111*9c5db199SXin Li 112*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 113*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 114*9c5db199SXin Li 115*9c5db199SXin Li @return the board string if this is a Moblab device or None if it is not. 116*9c5db199SXin Li """ 117*9c5db199SXin Li return 'MOBLAB' in os.environ 118*9c5db199SXin Li 119*9c5db199SXin Li 120*9c5db199SXin Lidef is_jetstream(lsb_release_content=None): 121*9c5db199SXin Li """Parses lsb_contents to determine if the host is a Jetstream host. 122*9c5db199SXin Li 123*9c5db199SXin Li @param lsb_release_content: The string contents of lsb-release. 124*9c5db199SXin Li If None, the local lsb-release is used. 125*9c5db199SXin Li 126*9c5db199SXin Li @return True if the host is a Jetstream device, otherwise False. 127*9c5db199SXin Li """ 128*9c5db199SXin Li board = get_current_board(lsb_release_content=lsb_release_content) 129*9c5db199SXin Li return board in JETSTREAM_BOARDS 130*9c5db199SXin Li 131*9c5db199SXin Lidef is_gce_board(lsb_release_content=None): 132*9c5db199SXin Li """Parses lsb_contents to determine if host is a GCE board. 133*9c5db199SXin Li 134*9c5db199SXin Li @param lsb_release_content: The string contents of lsb-release. 135*9c5db199SXin Li If None, the local lsb-release is used. 136*9c5db199SXin Li 137*9c5db199SXin Li @return True if the host is a GCE board otherwise False. 138*9c5db199SXin Li """ 139*9c5db199SXin Li return is_lakitu(lsb_release_content=lsb_release_content) 140*9c5db199SXin Li 141*9c5db199SXin Lidef is_lakitu(lsb_release_content=None): 142*9c5db199SXin Li """Parses lsb_contents to determine if host is lakitu. 143*9c5db199SXin Li 144*9c5db199SXin Li @param lsb_release_content: The string contents of lsb-release. 145*9c5db199SXin Li If None, the local lsb-release is used. 146*9c5db199SXin Li 147*9c5db199SXin Li @return True if the host is lakitu otherwise False. 148*9c5db199SXin Li """ 149*9c5db199SXin Li board = get_current_board(lsb_release_content=lsb_release_content) 150*9c5db199SXin Li if board is not None: 151*9c5db199SXin Li return board.startswith('lakitu') 152*9c5db199SXin Li return False 153*9c5db199SXin Li 154*9c5db199SXin Lidef get_chrome_milestone(lsb_release_content=None): 155*9c5db199SXin Li """Get the value for the Chrome milestone. 156*9c5db199SXin Li 157*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 158*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 159*9c5db199SXin Li 160*9c5db199SXin Li @return the value for the Chrome milestone 161*9c5db199SXin Li """ 162*9c5db199SXin Li return _lsbrelease_search(r'^CHROMEOS_RELEASE_CHROME_MILESTONE=(.+)$', 163*9c5db199SXin Li group_id=1, 164*9c5db199SXin Li lsb_release_content=lsb_release_content) 165*9c5db199SXin Li 166*9c5db199SXin Li 167*9c5db199SXin Lidef get_device_type(lsb_release_content=None): 168*9c5db199SXin Li """Get the device type string, e.g. "CHROMEBOOK" or "CHROMEBOX". 169*9c5db199SXin Li 170*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 171*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 172*9c5db199SXin Li 173*9c5db199SXin Li @return the DEVICETYPE value for this machine. 174*9c5db199SXin Li """ 175*9c5db199SXin Li return _lsbrelease_search(r'^DEVICETYPE=(.+)$', group_id=1, 176*9c5db199SXin Li lsb_release_content=lsb_release_content) 177*9c5db199SXin Li 178*9c5db199SXin Li 179*9c5db199SXin Lidef is_arc_available(lsb_release_content=None): 180*9c5db199SXin Li """Returns True if the device has ARC installed. 181*9c5db199SXin Li 182*9c5db199SXin Li @param lsb_release_content: A string represents the content of lsb-release. 183*9c5db199SXin Li If the caller is from drone, it can pass in the file content here. 184*9c5db199SXin Li 185*9c5db199SXin Li @return True if the device has ARC installed. 186*9c5db199SXin Li """ 187*9c5db199SXin Li return (_lsbrelease_search(r'^CHROMEOS_ARC_VERSION', 188*9c5db199SXin Li lsb_release_content=lsb_release_content) 189*9c5db199SXin Li is not None) 190