1# -*- coding: utf-8 -*- 2# Copyright (c) 2013 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"""The label of benchamrks.""" 7 8from __future__ import print_function 9 10import hashlib 11import os 12 13from image_checksummer import ImageChecksummer 14from cros_utils.file_utils import FileUtils 15from cros_utils import misc 16 17 18class Label(object): 19 """The label class.""" 20 21 def __init__(self, 22 name, 23 build, 24 chromeos_image, 25 autotest_path, 26 debug_path, 27 chromeos_root, 28 board, 29 remote, 30 image_args, 31 cache_dir, 32 cache_only, 33 log_level, 34 compiler, 35 crosfleet=False, 36 chrome_src=None): 37 38 self.image_type = self._GetImageType(chromeos_image) 39 40 # Expand ~ 41 chromeos_root = os.path.expanduser(chromeos_root) 42 if self.image_type == 'local': 43 chromeos_image = os.path.expanduser(chromeos_image) 44 45 self.name = name 46 self.build = build 47 self.chromeos_image = chromeos_image 48 self.autotest_path = autotest_path 49 self.debug_path = debug_path 50 self.board = board 51 self.remote = remote 52 self.image_args = image_args 53 self.cache_dir = cache_dir 54 self.cache_only = cache_only 55 self.log_level = log_level 56 self.chrome_version = '' 57 self.compiler = compiler 58 self.crosfleet = crosfleet 59 60 if not chromeos_root: 61 if self.image_type == 'local': 62 chromeos_root = FileUtils().ChromeOSRootFromImage(chromeos_image) 63 if not chromeos_root: 64 raise RuntimeError("No ChromeOS root given for label '%s' and could " 65 "not determine one from image path: '%s'." % 66 (name, chromeos_image)) 67 else: 68 chromeos_root = FileUtils().CanonicalizeChromeOSRoot(chromeos_root) 69 if not chromeos_root: 70 raise RuntimeError("Invalid ChromeOS root given for label '%s': '%s'." % 71 (name, chromeos_root)) 72 73 self.chromeos_root = chromeos_root 74 if not chrome_src: 75 # Old and new chroots may have different chrome src locations. 76 # The path also depends on the chrome build flags. 77 # Give priority to chrome-src-internal. 78 chrome_src_rel_paths = [ 79 '.cache/distfiles/target/chrome-src-internal', 80 '.cache/distfiles/chrome-src-internal', 81 '.cache/distfiles/target/chrome-src', 82 '.cache/distfiles/chrome-src', 83 ] 84 for chrome_src_rel_path in chrome_src_rel_paths: 85 chrome_src_abs_path = os.path.join(self.chromeos_root, 86 chrome_src_rel_path) 87 if os.path.exists(chrome_src_abs_path): 88 chrome_src = chrome_src_abs_path 89 break 90 if not chrome_src: 91 raise RuntimeError('Can not find location of Chrome sources.\n' 92 f'Checked paths: {chrome_src_rel_paths}') 93 else: 94 chrome_src = misc.CanonicalizePath(chrome_src) 95 # Make sure the path exists. 96 if not os.path.exists(chrome_src): 97 raise RuntimeError("Invalid Chrome src given for label '%s': '%s'." % 98 (name, chrome_src)) 99 self.chrome_src = chrome_src 100 101 self._SetupChecksum() 102 103 def _SetupChecksum(self): 104 """Compute label checksum only once.""" 105 106 self.checksum = None 107 if self.image_type == 'local': 108 self.checksum = ImageChecksummer().Checksum(self, self.log_level) 109 elif self.image_type == 'trybot': 110 self.checksum = hashlib.md5( 111 self.chromeos_image.encode('utf-8')).hexdigest() 112 113 def _GetImageType(self, chromeos_image): 114 image_type = None 115 if chromeos_image.find('xbuddy://') < 0: 116 image_type = 'local' 117 elif chromeos_image.find('trybot') >= 0: 118 image_type = 'trybot' 119 else: 120 image_type = 'official' 121 return image_type 122 123 def __hash__(self): 124 """Label objects are used in a map, so provide "hash" and "equal".""" 125 126 return hash(self.name) 127 128 def __eq__(self, other): 129 """Label objects are used in a map, so provide "hash" and "equal".""" 130 131 return isinstance(other, Label) and other.name == self.name 132 133 def __str__(self): 134 """For better debugging.""" 135 136 return 'label[name="{}"]'.format(self.name) 137 138 139class MockLabel(object): 140 """The mock label class.""" 141 142 def __init__(self, 143 name, 144 build, 145 chromeos_image, 146 autotest_path, 147 debug_path, 148 chromeos_root, 149 board, 150 remote, 151 image_args, 152 cache_dir, 153 cache_only, 154 log_level, 155 compiler, 156 crosfleet=False, 157 chrome_src=None): 158 self.name = name 159 self.build = build 160 self.chromeos_image = chromeos_image 161 self.autotest_path = autotest_path 162 self.debug_path = debug_path 163 self.board = board 164 self.remote = remote 165 self.cache_dir = cache_dir 166 self.cache_only = cache_only 167 if not chromeos_root: 168 self.chromeos_root = '/tmp/chromeos_root' 169 else: 170 self.chromeos_root = chromeos_root 171 self.image_args = image_args 172 self.chrome_src = chrome_src 173 self.image_type = self._GetImageType(chromeos_image) 174 self.checksum = '' 175 self.log_level = log_level 176 self.compiler = compiler 177 self.crosfleet = crosfleet 178 self.chrome_version = 'Fake Chrome Version 50' 179 180 def _GetImageType(self, chromeos_image): 181 image_type = None 182 if chromeos_image.find('xbuddy://') < 0: 183 image_type = 'local' 184 elif chromeos_image.find('trybot') >= 0: 185 image_type = 'trybot' 186 else: 187 image_type = 'official' 188 return image_type 189