blob: dcc1cb0221797b3c8cdabbbe57fbed0850b40e73 [file] [log] [blame]
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08001#!/usr/bin/python
2
3# Copyright 2011 Google Inc. All Rights Reserved.
4
Luis Lozanof81680c2013-03-15 14:44:13 -07005import os
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08006import threading
Luis Lozanof81680c2013-03-15 14:44:13 -07007
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08008from utils import logger
9from utils.file_utils import FileUtils
10
11
12class ImageChecksummer(object):
13 class PerImageChecksummer(object):
Luis Lozanof81680c2013-03-15 14:44:13 -070014 def __init__(self, label):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080015 self._lock = threading.Lock()
Luis Lozanof81680c2013-03-15 14:44:13 -070016 self.label = label
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080017 self._checksum = None
18
19 def Checksum(self):
20 with self._lock:
21 if not self._checksum:
Luis Lozanof81680c2013-03-15 14:44:13 -070022 logger.GetLogger().LogOutput("Acquiring checksum for '%s'." %
23 self.label.name)
24 self._checksum = None
25 if self.label.chromeos_image:
26 if os.path.exists(self.label.chromeos_image):
27 self._checksum = FileUtils().Md5File(self.label.chromeos_image)
28 logger.GetLogger().LogOutput("Computed checksum is "
29 ": %s" % self._checksum)
30 if not self._checksum:
31 if self.label.image_md5sum:
32 self._checksum = self.label.image_md5sum
33 logger.GetLogger().LogOutput("Checksum in experiment file is "
34 ": %s" % self._checksum)
35 else:
36 raise Exception("Checksum computing error.")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080037 logger.GetLogger().LogOutput("Checksum is: %s" % self._checksum)
38 return self._checksum
39
40 _instance = None
41 _lock = threading.Lock()
42 _per_image_checksummers = {}
43
44 def __new__(cls, *args, **kwargs):
45 with cls._lock:
46 if not cls._instance:
47 cls._instance = super(ImageChecksummer, cls).__new__(cls,
48 *args, **kwargs)
49 return cls._instance
50
Luis Lozanof81680c2013-03-15 14:44:13 -070051 def Checksum(self, label):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080052 with self._lock:
Luis Lozanof81680c2013-03-15 14:44:13 -070053 if label.name not in self._per_image_checksummers:
54 self._per_image_checksummers[label.name] = (ImageChecksummer.
55 PerImageChecksummer(label))
56 checksummer = self._per_image_checksummers[label.name]
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080057
58 try:
59 return checksummer.Checksum()
60 except Exception, e:
Luis Lozanof81680c2013-03-15 14:44:13 -070061 logger.GetLogger().LogError("Could not compute checksum of image in label"
62 " '%s'."% label.name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080063 raise e