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

# Copyright 2011 Google Inc. All Rights Reserved.

import os
import threading

from utils import logger
from utils.file_utils import FileUtils


class ImageChecksummer(object):
  class PerImageChecksummer(object):
    def __init__(self, label):
      self._lock = threading.Lock()
      self.label = label
      self._checksum = None

    def Checksum(self):
      with self._lock:
        if not self._checksum:
          logger.GetLogger().LogOutput("Acquiring checksum for '%s'." %
                                       self.label.name)
          self._checksum = None
          if self.label.chromeos_image:
            if os.path.exists(self.label.chromeos_image):
              self._checksum = FileUtils().Md5File(self.label.chromeos_image)
              logger.GetLogger().LogOutput("Computed checksum is "
                                           ": %s" % self._checksum)
          if not self._checksum:
            if self.label.image_md5sum:
              self._checksum = self.label.image_md5sum
              logger.GetLogger().LogOutput("Checksum in experiment file is "
                                           ": %s" % self._checksum)
            else:
              raise Exception("Checksum computing error.")
          logger.GetLogger().LogOutput("Checksum is: %s" % self._checksum)
        return self._checksum

  _instance = None
  _lock = threading.Lock()
  _per_image_checksummers = {}

  def __new__(cls, *args, **kwargs):
    with cls._lock:
      if not cls._instance:
        cls._instance = super(ImageChecksummer, cls).__new__(cls,
                                                             *args, **kwargs)
      return cls._instance

  def Checksum(self, label):
    with self._lock:
      if label.name not in self._per_image_checksummers:
        self._per_image_checksummers[label.name] = (ImageChecksummer.
                                                    PerImageChecksummer(label))
      checksummer = self._per_image_checksummers[label.name]

    try:
      return checksummer.Checksum()
    except Exception, e:
      logger.GetLogger().LogError("Could not compute checksum of image in label"
                                  " '%s'."% label.name)
      raise e