blob: a34416d2f8d2cec6fb1994a30682ef431020f168 [file] [log] [blame]
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -07001# 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.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08004
Ahmad Sharif4467f002012-12-20 12:09:49 -08005"""The label of benchamrks."""
6
Yunlian Jianga19dc552015-12-10 11:57:46 -08007from __future__ import print_function
8
9import hashlib
Ahmad Sharif4467f002012-12-20 12:09:49 -080010import os
Han Shenba649282015-08-05 17:19:55 -070011
12from image_checksummer import ImageChecksummer
Yunlian Jianga19dc552015-12-10 11:57:46 -080013from cros_utils.file_utils import FileUtils
14from cros_utils import misc
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080015
16
17class Label(object):
Yunlian Jianga19dc552015-12-10 11:57:46 -080018 """The label class."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080019 def __init__(self, name, chromeos_image, chromeos_root, board, remote,
Caroline Ticeddde5052015-09-23 09:43:35 -070020 image_args, cache_dir, cache_only, log_level, compiler,
21 chrome_src=None):
cmtice0cc4e772014-01-30 15:52:37 -080022
23 self.image_type = self._GetImageType(chromeos_image)
24
Ahmad Sharif4467f002012-12-20 12:09:49 -080025 # Expand ~
26 chromeos_root = os.path.expanduser(chromeos_root)
cmtice0cc4e772014-01-30 15:52:37 -080027 if self.image_type == "local":
28 chromeos_image = os.path.expanduser(chromeos_image)
Ahmad Sharif4467f002012-12-20 12:09:49 -080029
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080030 self.name = name
31 self.chromeos_image = chromeos_image
32 self.board = board
Ahmad Sharif4467f002012-12-20 12:09:49 -080033 self.remote = remote
34 self.image_args = image_args
Luis Lozanof81680c2013-03-15 14:44:13 -070035 self.cache_dir = cache_dir
cmtice2a370f72014-05-29 16:16:25 -070036 self.cache_only = cache_only
Han Shenba649282015-08-05 17:19:55 -070037 self.log_level = log_level
Caroline Tice31fedb02015-09-14 16:30:37 -070038 self.chrome_version = ""
Caroline Ticeddde5052015-09-23 09:43:35 -070039 self.compiler = compiler
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080040
41 if not chromeos_root:
cmtice0cc4e772014-01-30 15:52:37 -080042 if self.image_type == "local":
43 chromeos_root = FileUtils().ChromeOSRootFromImage(chromeos_image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080044 if not chromeos_root:
45 raise Exception("No ChromeOS root given for label '%s' and could not "
46 "determine one from image path: '%s'." %
47 (name, chromeos_image))
48 else:
49 chromeos_root = FileUtils().CanonicalizeChromeOSRoot(chromeos_root)
50 if not chromeos_root:
51 raise Exception("Invalid ChromeOS root given for label '%s': '%s'."
52 % (name, chromeos_root))
53
54 self.chromeos_root = chromeos_root
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070055 if not chrome_src:
Yunlian Jianga19dc552015-12-10 11:57:46 -080056 self.chrome_src = os.path.join(
57 self.chromeos_root,
cmtice226e3e02014-04-27 22:28:42 -070058 ".cache/distfiles/target/chrome-src-internal")
cmtice04403882013-11-04 16:38:37 -050059 if not os.path.exists(self.chrome_src):
60 self.chrome_src = os.path.join(self.chromeos_root,
Yunlian Jianga19dc552015-12-10 11:57:46 -080061 ".cache/distfiles/target/chrome-src")
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070062 else:
Yunlian Jiangffd98bb2013-08-02 10:42:25 -070063 chromeos_src = misc.CanonicalizePath(chrome_src)
64 if not chromeos_src:
65 raise Exception("Invalid Chrome src given for label '%s': '%s'."
66 % (name, chrome_src))
67 self.chrome_src = chromeos_src
Ahmad Sharif4467f002012-12-20 12:09:49 -080068
Han Shenba649282015-08-05 17:19:55 -070069 self._SetupChecksum()
70
71 def _SetupChecksum(self):
72 """Compute label checksum only once."""
73
74 self.checksum = None
75 if self.image_type == "local":
76 self.checksum = ImageChecksummer().Checksum(self, self.log_level)
77 elif self.image_type == "trybot":
78 self.checksum = hashlib.md5(self.chromeos_image).hexdigest()
79
cmtice0cc4e772014-01-30 15:52:37 -080080 def _GetImageType(self, chromeos_image):
81 image_type = None
82 if chromeos_image.find("xbuddy://") < 0:
83 image_type = "local"
84 elif chromeos_image.find("trybot") >= 0:
85 image_type = "trybot"
86 else:
87 image_type = "official"
88 return image_type
Ahmad Sharif4467f002012-12-20 12:09:49 -080089
Han Shenba649282015-08-05 17:19:55 -070090 def __hash__(self):
Yunlian Jianga19dc552015-12-10 11:57:46 -080091 """Label objects are used in a map, so provide "hash" and "equal"."""
Han Shenba649282015-08-05 17:19:55 -070092
Yunlian Jianga19dc552015-12-10 11:57:46 -080093 return hash(self.name)
Han Shenba649282015-08-05 17:19:55 -070094
95 def __eq__(self, other):
Yunlian Jianga19dc552015-12-10 11:57:46 -080096 """Label objects are used in a map, so provide "hash" and "equal"."""
Han Shenba649282015-08-05 17:19:55 -070097
Yunlian Jianga19dc552015-12-10 11:57:46 -080098 return isinstance(other, Label) and other.name == self.name
Han Shenba649282015-08-05 17:19:55 -070099
100 def __str__(self):
Yunlian Jianga19dc552015-12-10 11:57:46 -0800101 """For better debugging."""
Han Shenba649282015-08-05 17:19:55 -0700102
Yunlian Jianga19dc552015-12-10 11:57:46 -0800103 return 'label[name="{}"]'.format(self.name)
Han Shenba649282015-08-05 17:19:55 -0700104
Ahmad Sharif4467f002012-12-20 12:09:49 -0800105class MockLabel(object):
Yunlian Jianga19dc552015-12-10 11:57:46 -0800106 """The mock label class."""
Ahmad Sharif4467f002012-12-20 12:09:49 -0800107 def __init__(self, name, chromeos_image, chromeos_root, board, remote,
Caroline Tice6e8726d2015-12-09 12:42:13 -0800108 image_args, cache_dir, cache_only, log_level, compiler,
109 chrome_src=None):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800110 self.name = name
111 self.chromeos_image = chromeos_image
112 self.board = board
113 self.remote = remote
Luis Lozanof81680c2013-03-15 14:44:13 -0700114 self.cache_dir = cache_dir
cmtice2a370f72014-05-29 16:16:25 -0700115 self.cache_only = cache_only
Ahmad Sharif4467f002012-12-20 12:09:49 -0800116 if not chromeos_root:
117 self.chromeos_root = "/tmp/chromeos_root"
118 else:
119 self.chromeos_root = chromeos_root
120 self.image_args = image_args
cmtice0cc4e772014-01-30 15:52:37 -0800121 self.chrome_src = chrome_src
cmtice1505b6a2014-06-04 14:19:19 -0700122 self.image_type = self._GetImageType(chromeos_image)
Han Shen7a939a32015-09-16 11:08:09 -0700123 self.checksum = ''
Caroline Tice6e8726d2015-12-09 12:42:13 -0800124 self.log_level = log_level
125 self.compiler = compiler
Caroline Tice7057cf62015-12-10 12:09:40 -0800126 self.chrome_version = "Fake Chrome Version 50"
cmtice1505b6a2014-06-04 14:19:19 -0700127
128 def _GetImageType(self, chromeos_image):
129 image_type = None
130 if chromeos_image.find("xbuddy://") < 0:
131 image_type = "local"
132 elif chromeos_image.find("trybot") >= 0:
133 image_type = "trybot"
134 else:
135 image_type = "official"
136 return image_type