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