Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 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. |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 4 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 5 | """The label of benchamrks.""" |
| 6 | |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
| 9 | import hashlib |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 10 | import os |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 11 | |
| 12 | from image_checksummer import ImageChecksummer |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 13 | from cros_utils.file_utils import FileUtils |
| 14 | from cros_utils import misc |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class Label(object): |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 18 | """The label class.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 19 | def __init__(self, name, chromeos_image, chromeos_root, board, remote, |
Caroline Tice | ddde505 | 2015-09-23 09:43:35 -0700 | [diff] [blame] | 20 | image_args, cache_dir, cache_only, log_level, compiler, |
| 21 | chrome_src=None): |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 22 | |
| 23 | self.image_type = self._GetImageType(chromeos_image) |
| 24 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 25 | # Expand ~ |
| 26 | chromeos_root = os.path.expanduser(chromeos_root) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 27 | if self.image_type == "local": |
| 28 | chromeos_image = os.path.expanduser(chromeos_image) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 29 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 30 | self.name = name |
| 31 | self.chromeos_image = chromeos_image |
| 32 | self.board = board |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 33 | self.remote = remote |
| 34 | self.image_args = image_args |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 35 | self.cache_dir = cache_dir |
cmtice | 2a370f7 | 2014-05-29 16:16:25 -0700 | [diff] [blame] | 36 | self.cache_only = cache_only |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 37 | self.log_level = log_level |
Caroline Tice | 31fedb0 | 2015-09-14 16:30:37 -0700 | [diff] [blame] | 38 | self.chrome_version = "" |
Caroline Tice | ddde505 | 2015-09-23 09:43:35 -0700 | [diff] [blame] | 39 | self.compiler = compiler |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 40 | |
| 41 | if not chromeos_root: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 42 | if self.image_type == "local": |
| 43 | chromeos_root = FileUtils().ChromeOSRootFromImage(chromeos_image) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 44 | 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 Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 55 | if not chrome_src: |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 56 | self.chrome_src = os.path.join( |
| 57 | self.chromeos_root, |
cmtice | 226e3e0 | 2014-04-27 22:28:42 -0700 | [diff] [blame] | 58 | ".cache/distfiles/target/chrome-src-internal") |
cmtice | 0440388 | 2013-11-04 16:38:37 -0500 | [diff] [blame] | 59 | if not os.path.exists(self.chrome_src): |
| 60 | self.chrome_src = os.path.join(self.chromeos_root, |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 61 | ".cache/distfiles/target/chrome-src") |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 62 | else: |
Yunlian Jiang | ffd98bb | 2013-08-02 10:42:25 -0700 | [diff] [blame] | 63 | 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 Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 68 | |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 69 | 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 | |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 80 | 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 Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 89 | |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 90 | def __hash__(self): |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 91 | """Label objects are used in a map, so provide "hash" and "equal".""" |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 92 | |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 93 | return hash(self.name) |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 94 | |
| 95 | def __eq__(self, other): |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 96 | """Label objects are used in a map, so provide "hash" and "equal".""" |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 97 | |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 98 | return isinstance(other, Label) and other.name == self.name |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 99 | |
| 100 | def __str__(self): |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 101 | """For better debugging.""" |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 102 | |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 103 | return 'label[name="{}"]'.format(self.name) |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 104 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 105 | class MockLabel(object): |
Yunlian Jiang | a19dc55 | 2015-12-10 11:57:46 -0800 | [diff] [blame] | 106 | """The mock label class.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 107 | def __init__(self, name, chromeos_image, chromeos_root, board, remote, |
Caroline Tice | 6e8726d | 2015-12-09 12:42:13 -0800 | [diff] [blame] | 108 | image_args, cache_dir, cache_only, log_level, compiler, |
| 109 | chrome_src=None): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 110 | self.name = name |
| 111 | self.chromeos_image = chromeos_image |
| 112 | self.board = board |
| 113 | self.remote = remote |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 114 | self.cache_dir = cache_dir |
cmtice | 2a370f7 | 2014-05-29 16:16:25 -0700 | [diff] [blame] | 115 | self.cache_only = cache_only |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 116 | 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 |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 121 | self.chrome_src = chrome_src |
cmtice | 1505b6a | 2014-06-04 14:19:19 -0700 | [diff] [blame] | 122 | self.image_type = self._GetImageType(chromeos_image) |
Han Shen | 7a939a3 | 2015-09-16 11:08:09 -0700 | [diff] [blame] | 123 | self.checksum = '' |
Caroline Tice | 6e8726d | 2015-12-09 12:42:13 -0800 | [diff] [blame] | 124 | self.log_level = log_level |
| 125 | self.compiler = compiler |
Caroline Tice | 7057cf6 | 2015-12-10 12:09:40 -0800 | [diff] [blame^] | 126 | self.chrome_version = "Fake Chrome Version 50" |
cmtice | 1505b6a | 2014-06-04 14:19:19 -0700 | [diff] [blame] | 127 | |
| 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 |