aboutsummaryrefslogtreecommitdiff
path: root/crosperf/label.py
blob: 9aeff56220d2c4622997ffd5bfd91d7ff3a2c792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
# Copyright 2013 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""The label of benchamrks."""


import hashlib
import os

from cros_utils import misc
from cros_utils.file_utils import FileUtils
from image_checksummer import ImageChecksummer


class Label(object):
    """The label class."""

    def __init__(
        self,
        name,
        build,
        chromeos_image,
        autotest_path,
        debug_path,
        chromeos_root,
        board,
        remote,
        image_args,
        cache_dir,
        cache_only,
        log_level,
        compiler,
        crosfleet=False,
        chrome_src=None,
    ):

        self.image_type = self._GetImageType(chromeos_image)

        # Expand ~
        chromeos_root = os.path.expanduser(chromeos_root)
        if self.image_type == "local":
            chromeos_image = os.path.expanduser(chromeos_image)

        self.name = name
        self.build = build
        self.chromeos_image = chromeos_image
        self.autotest_path = autotest_path
        self.debug_path = debug_path
        self.board = board
        self.remote = remote
        self.image_args = image_args
        self.cache_dir = cache_dir
        self.cache_only = cache_only
        self.log_level = log_level
        self.chrome_version = ""
        self.compiler = compiler
        self.crosfleet = crosfleet

        if not chromeos_root:
            if self.image_type == "local":
                chromeos_root = FileUtils().ChromeOSRootFromImage(
                    chromeos_image
                )
            if not chromeos_root:
                raise RuntimeError(
                    "No ChromeOS root given for label '%s' and could "
                    "not determine one from image path: '%s'."
                    % (name, chromeos_image)
                )
        else:
            chromeos_root = FileUtils().CanonicalizeChromeOSRoot(chromeos_root)
            if not chromeos_root:
                raise RuntimeError(
                    "Invalid ChromeOS root given for label '%s': '%s'."
                    % (name, chromeos_root)
                )

        self.chromeos_root = chromeos_root
        if not chrome_src:
            # Old and new chroots may have different chrome src locations.
            # The path also depends on the chrome build flags.
            # Give priority to chrome-src-internal.
            chrome_src_rel_paths = [
                ".cache/distfiles/target/chrome-src-internal",
                ".cache/distfiles/chrome-src-internal",
                ".cache/distfiles/target/chrome-src",
                ".cache/distfiles/chrome-src",
            ]
            for chrome_src_rel_path in chrome_src_rel_paths:
                chrome_src_abs_path = os.path.join(
                    self.chromeos_root, chrome_src_rel_path
                )
                if os.path.exists(chrome_src_abs_path):
                    chrome_src = chrome_src_abs_path
                    break
            if not chrome_src:
                raise RuntimeError(
                    "Can not find location of Chrome sources.\n"
                    f"Checked paths: {chrome_src_rel_paths}"
                )
        else:
            chrome_src = misc.CanonicalizePath(chrome_src)
            # Make sure the path exists.
            if not os.path.exists(chrome_src):
                raise RuntimeError(
                    "Invalid Chrome src given for label '%s': '%s'."
                    % (name, chrome_src)
                )
        self.chrome_src = chrome_src

        self._SetupChecksum()

    def _SetupChecksum(self):
        """Compute label checksum only once."""

        self.checksum = None
        if self.image_type == "local":
            self.checksum = ImageChecksummer().Checksum(self, self.log_level)
        elif self.image_type == "trybot":
            self.checksum = hashlib.md5(
                self.chromeos_image.encode("utf-8")
            ).hexdigest()

    def _GetImageType(self, chromeos_image):
        image_type = None
        if chromeos_image.find("xbuddy://") < 0:
            image_type = "local"
        elif chromeos_image.find("trybot") >= 0:
            image_type = "trybot"
        else:
            image_type = "official"
        return image_type

    def __hash__(self):
        """Label objects are used in a map, so provide "hash" and "equal"."""

        return hash(self.name)

    def __eq__(self, other):
        """Label objects are used in a map, so provide "hash" and "equal"."""

        return isinstance(other, Label) and other.name == self.name

    def __str__(self):
        """For better debugging."""

        return 'label[name="{}"]'.format(self.name)


class MockLabel(object):
    """The mock label class."""

    def __init__(
        self,
        name,
        build,
        chromeos_image,
        autotest_path,
        debug_path,
        chromeos_root,
        board,
        remote,
        image_args,
        cache_dir,
        cache_only,
        log_level,
        compiler,
        crosfleet=False,
        chrome_src=None,
    ):
        self.name = name
        self.build = build
        self.chromeos_image = chromeos_image
        self.autotest_path = autotest_path
        self.debug_path = debug_path
        self.board = board
        self.remote = remote
        self.cache_dir = cache_dir
        self.cache_only = cache_only
        if not chromeos_root:
            self.chromeos_root = "/tmp/chromeos_root"
        else:
            self.chromeos_root = chromeos_root
        self.image_args = image_args
        self.chrome_src = chrome_src
        self.image_type = self._GetImageType(chromeos_image)
        self.checksum = ""
        self.log_level = log_level
        self.compiler = compiler
        self.crosfleet = crosfleet
        self.chrome_version = "Fake Chrome Version 50"

    def _GetImageType(self, chromeos_image):
        image_type = None
        if chromeos_image.find("xbuddy://") < 0:
            image_type = "local"
        elif chromeos_image.find("trybot") >= 0:
            image_type = "trybot"
        else:
            image_type = "official"
        return image_type