Home | History | Annotate | Download | only in util
      1 # Copyright 2013 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import hashlib
      6 import os
      7 
      8 
      9 def CallAndRecordIfStale(
     10     function, record_path=None, input_paths=[], input_strings=[], force=False):
     11   """Calls function if the md5sum of the input paths/strings has changed.
     12 
     13   The md5sum of the inputs is compared with the one stored in record_path. If
     14   this has changed (or the record doesn't exist), function will be called and
     15   the new md5sum will be recorded.
     16 
     17   If force is True, the function will be called regardless of whether the
     18   md5sum is out of date.
     19   """
     20   md5_checker = _Md5Checker(
     21       record_path=record_path,
     22       input_paths=input_paths,
     23       input_strings=input_strings)
     24   if force or md5_checker.IsStale():
     25     function()
     26     md5_checker.Write()
     27 
     28 
     29 def _UpdateMd5ForFile(md5, path, block_size=2**16):
     30   with open(path, 'rb') as infile:
     31     while True:
     32       data = infile.read(block_size)
     33       if not data:
     34         break
     35       md5.update(data)
     36 
     37 
     38 def _UpdateMd5ForDirectory(md5, dir_path):
     39   for root, _, files in os.walk(dir_path):
     40     for f in files:
     41       _UpdateMd5ForFile(md5, os.path.join(root, f))
     42 
     43 
     44 def _UpdateMd5ForPath(md5, path):
     45   if os.path.isdir(path):
     46     _UpdateMd5ForDirectory(md5, path)
     47   else:
     48     _UpdateMd5ForFile(md5, path)
     49 
     50 
     51 class _Md5Checker(object):
     52   def __init__(self, record_path=None, input_paths=[], input_strings=[]):
     53     assert record_path.endswith('.stamp'), (
     54         'record paths must end in \'.stamp\' so that they are easy to find '
     55         'and delete')
     56 
     57     self.record_path = record_path
     58 
     59     md5 = hashlib.md5()
     60     for i in sorted(input_paths):
     61       _UpdateMd5ForPath(md5, i)
     62     for s in input_strings:
     63       md5.update(s)
     64     self.new_digest = md5.hexdigest()
     65 
     66     self.old_digest = ''
     67     if os.path.exists(self.record_path):
     68       with open(self.record_path, 'r') as old_record:
     69         self.old_digest = old_record.read()
     70 
     71   def IsStale(self):
     72     return self.old_digest != self.new_digest
     73 
     74   def Write(self):
     75     with open(self.record_path, 'w') as new_record:
     76       new_record.write(self.new_digest)
     77