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