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

# Copyright 2011 Google Inc. All Rights Reserved.

import threading
from utils import logger
from utils.file_utils import FileUtils


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

    def Checksum(self):
      with self._lock:
        if not self._checksum:
          logger.GetLogger().LogOutput("Computing checksum for '%s'." %
                                       self.filename)
          self._checksum = FileUtils().Md5File(self.filename)
          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, filename):
    with self._lock:
      if filename not in self._per_image_checksummers:
        self._per_image_checksummers[filename] = (ImageChecksummer.
                                                  PerImageChecksummer(filename))
      checksummer = self._per_image_checksummers[filename]

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