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 skylab=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.skylab = skylab 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( 65 "No ChromeOS root given for label '%s' and could " 66 "not determine one from image path: '%s'." % (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 self.chrome_src = os.path.join( 76 self.chromeos_root, '.cache/distfiles/target/chrome-src-internal') 77 if not os.path.exists(self.chrome_src): 78 self.chrome_src = os.path.join(self.chromeos_root, 79 '.cache/distfiles/target/chrome-src') 80 else: 81 chromeos_src = misc.CanonicalizePath(chrome_src) 82 if not chromeos_src: 83 raise RuntimeError("Invalid Chrome src given for label '%s': '%s'." % 84 (name, chrome_src)) 85 self.chrome_src = chromeos_src 86 87 self._SetupChecksum() 88 89 def _SetupChecksum(self): 90 """Compute label checksum only once.""" 91 92 self.checksum = None 93 if self.image_type == 'local': 94 self.checksum = ImageChecksummer().Checksum(self, self.log_level) 95 elif self.image_type == 'trybot': 96 self.checksum = hashlib.md5( 97 self.chromeos_image.encode('utf-8')).hexdigest() 98 99 def _GetImageType(self, chromeos_image): 100 image_type = None 101 if chromeos_image.find('xbuddy://') < 0: 102 image_type = 'local' 103 elif chromeos_image.find('trybot') >= 0: 104 image_type = 'trybot' 105 else: 106 image_type = 'official' 107 return image_type 108 109 def __hash__(self): 110 """Label objects are used in a map, so provide "hash" and "equal".""" 111 112 return hash(self.name) 113 114 def __eq__(self, other): 115 """Label objects are used in a map, so provide "hash" and "equal".""" 116 117 return isinstance(other, Label) and other.name == self.name 118 119 def __str__(self): 120 """For better debugging.""" 121 122 return 'label[name="{}"]'.format(self.name) 123 124 125class MockLabel(object): 126 """The mock label class.""" 127 128 def __init__(self, 129 name, 130 build, 131 chromeos_image, 132 autotest_path, 133 debug_path, 134 chromeos_root, 135 board, 136 remote, 137 image_args, 138 cache_dir, 139 cache_only, 140 log_level, 141 compiler, 142 skylab=False, 143 chrome_src=None): 144 self.name = name 145 self.build = build 146 self.chromeos_image = chromeos_image 147 self.autotest_path = autotest_path 148 self.debug_path = debug_path 149 self.board = board 150 self.remote = remote 151 self.cache_dir = cache_dir 152 self.cache_only = cache_only 153 if not chromeos_root: 154 self.chromeos_root = '/tmp/chromeos_root' 155 else: 156 self.chromeos_root = chromeos_root 157 self.image_args = image_args 158 self.chrome_src = chrome_src 159 self.image_type = self._GetImageType(chromeos_image) 160 self.checksum = '' 161 self.log_level = log_level 162 self.compiler = compiler 163 self.skylab = skylab 164 self.chrome_version = 'Fake Chrome Version 50' 165 166 def _GetImageType(self, chromeos_image): 167 image_type = None 168 if chromeos_image.find('xbuddy://') < 0: 169 image_type = 'local' 170 elif chromeos_image.find('trybot') >= 0: 171 image_type = 'trybot' 172 else: 173 image_type = 'official' 174 return image_type 175