aboutsummaryrefslogtreecommitdiff
path: root/crosperf/label.py
blob: 5ad3821f5393e3c3a3a97c0a9b9e218517439393 (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
#!/usr/bin/python

# 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."""

import os

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


class Label(object):
  def __init__(self, name, chromeos_image, chromeos_root, board, remote,
               image_args, cache_dir, cache_only, log_level, 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.chromeos_image = chromeos_image
    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 = ""

    if not chromeos_root:
      if self.image_type == "local":
        chromeos_root = FileUtils().ChromeOSRootFromImage(chromeos_image)
      if not chromeos_root:
        raise Exception("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 Exception("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 Exception("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).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):
  def __init__(self, name, chromeos_image, chromeos_root, board, remote,
               image_args, cache_dir, cache_only, chrome_src=None):
    self.name = name
    self.chromeos_image = chromeos_image
    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 = ''

  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