aboutsummaryrefslogtreecommitdiff
path: root/crosperf/label.py
blob: b81226131f1d44811fc9de435bdb6b873044a91f (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
# -*- coding: utf-8 -*-
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""The label of benchamrks."""

from __future__ import print_function

import hashlib
import os

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


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,
               skylab=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.skylab = skylab

    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:
      self.chrome_src = os.path.join(
          self.chromeos_root, '.cache/distfiles/target/chrome-src-internal')
      if not os.path.exists(self.chrome_src):
        self.chrome_src = os.path.join(self.chromeos_root,
                                       '.cache/distfiles/target/chrome-src')
    else:
      chromeos_src = misc.CanonicalizePath(chrome_src)
      if not chromeos_src:
        raise RuntimeError("Invalid Chrome src given for label '%s': '%s'." %
                           (name, chrome_src))
      self.chrome_src = chromeos_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,
               skylab=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.skylab = skylab
    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