1# -*- coding: utf-8 -*- 2# Copyright 2016 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""This file contains util functions for the auto-update lib.""" 7 8from __future__ import print_function 9 10import re 11 12from autotest_lib.utils.frozen_chromite.lib import cros_logging as logging 13 14 15LSB_RELEASE = '/etc/lsb-release' 16 17 18def GetChromeosBuildInfo(lsb_release_content=None, regex=None): 19 """Get chromeos build info in device under test as string. None on fail. 20 21 Args: 22 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 regex: A regular expression, refers to which line this func tries to fetch 25 from lsb_release_content. 26 27 Returns: 28 A kind of chromeos build info in device under test as string. None on fail. 29 """ 30 if not lsb_release_content or not regex: 31 return None 32 33 for line in lsb_release_content.split('\n'): 34 m = re.match(regex, line) 35 if m: 36 return m.group(1) 37 38 return None 39 40def VersionMatch(build_version, release_version): 41 """Compare release version from lsb-release with cros-version label. 42 43 build_version is a string based on build name. It is prefixed with builder 44 info and branch ID, e.g., lumpy-release/R43-6809.0.0. 45 release_version is retrieved from lsb-release. 46 These two values might not match exactly. 47 48 The method is designed to compare version for following 6 scenarios with 49 samples of build version and expected release version: 50 1. trybot non-release build (paladin, pre-cq or test-ap build). 51 build version: trybot-lumpy-paladin/R27-3837.0.0-b123 52 release version: 3837.0.2013_03_21_1340 53 54 2. trybot release build. 55 build version: trybot-lumpy-release/R27-3837.0.0-b456 56 release version: 3837.0.0 57 58 3. buildbot official release build. 59 build version: lumpy-release/R27-3837.0.0 60 release version: 3837.0.0 61 62 4. non-official paladin rc build. 63 build version: lumpy-paladin/R27-3878.0.0-rc7 64 release version: 3837.0.0-rc7 65 66 5. chrome-perf build. 67 build version: lumpy-chrome-perf/R28-3837.0.0-b2996 68 release version: 3837.0.0 69 70 6. pgo-generate build. 71 build version: lumpy-release-pgo-generate/R28-3837.0.0-b2996 72 release version: 3837.0.0-pgo-generate 73 74 TODO: This logic has a bug if a trybot paladin build failed to be 75 installed in a DUT running an older trybot paladin build with same 76 platform number, but different build number (-b###). So to conclusively 77 determine if a tryjob paladin build is imaged successfully, we may need 78 to find out the date string from update url. 79 80 Args: 81 build_version: Build name for cros version, e.g. 82 peppy-release/R43-6809.0.0 or R43-6809.0.0 83 release_version: Release version retrieved from lsb-release, 84 e.g., 6809.0.0 85 86 Returns: 87 True if the values match, otherwise returns False. 88 """ 89 # If the build is from release, CQ or PFQ builder, cros-version label must 90 # be ended with release version in lsb-release. 91 92 if build_version.endswith(release_version): 93 return True 94 95 # Remove R#- and -b# at the end of build version 96 stripped_version = re.sub(r'(R\d+-|-b\d+)', '', build_version) 97 # Trim the builder info, e.g., trybot-lumpy-paladin/ 98 stripped_version = stripped_version.split('/')[-1] 99 100 # Add toolchain here since is_trybot_non_release_build cannot detect build 101 # like 'trybot-sentry-llvm-toolchain/R56-8885.0.0-b943'. 102 is_trybot_non_release_build = re.match( 103 r'.*trybot-.+-(paladin|pre-cq|test-ap|toolchain)', build_version) 104 105 # Replace date string with 0 in release_version 106 release_version_no_date = re.sub(r'\d{4}_\d{2}_\d{2}_\d+', '0', 107 release_version) 108 has_date_string = release_version != release_version_no_date 109 110 is_pgo_generate_build = re.match(r'.+-pgo-generate', build_version) 111 112 # Remove |-pgo-generate| in release_version 113 release_version_no_pgo = release_version.replace('-pgo-generate', '') 114 has_pgo_generate = release_version != release_version_no_pgo 115 116 if is_trybot_non_release_build: 117 if not has_date_string: 118 logging.error('A trybot paladin or pre-cq build is expected. ' 119 'Version "%s" is not a paladin or pre-cq build.', 120 release_version) 121 return False 122 return stripped_version == release_version_no_date 123 elif is_pgo_generate_build: 124 if not has_pgo_generate: 125 logging.error('A pgo-generate build is expected. Version ' 126 '"%s" is not a pgo-generate build.', 127 release_version) 128 return False 129 return stripped_version == release_version_no_pgo 130 else: 131 if has_date_string: 132 logging.error('Unexpected date found in a non trybot paladin or ' 133 'pre-cq build.') 134 return False 135 # Versioned build, i.e., rc or release build. 136 return stripped_version == release_version 137