OLD | NEW |
(Empty) | |
| 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 UpdateMd5ForFile(md5, path, block_size=2**16): |
| 10 with open(path, 'rb') as infile: |
| 11 while True: |
| 12 data = infile.read(block_size) |
| 13 if not data: |
| 14 break |
| 15 md5.update(data) |
| 16 |
| 17 |
| 18 def UpdateMd5ForDirectory(md5, dir_path): |
| 19 for root, _, files in os.walk(dir_path): |
| 20 for f in files: |
| 21 UpdateMd5ForFile(md5, os.path.join(root, f)) |
| 22 |
| 23 |
| 24 def UpdateMd5ForPath(md5, path): |
| 25 if os.path.isdir(path): |
| 26 UpdateMd5ForDirectory(md5, path) |
| 27 else: |
| 28 UpdateMd5ForFile(md5, path) |
| 29 |
| 30 |
| 31 class Md5Checker(object): |
| 32 def __init__(self, stamp=None, inputs=[], command=[]): |
| 33 self.stamp = stamp |
| 34 |
| 35 md5 = hashlib.md5() |
| 36 for i in inputs: |
| 37 UpdateMd5ForPath(md5, i) |
| 38 for s in command: |
| 39 md5.update(s) |
| 40 self.new_digest = md5.hexdigest() |
| 41 |
| 42 self.old_digest = '' |
| 43 if os.path.exists(stamp): |
| 44 with open(stamp, 'r') as old_stamp: |
| 45 self.old_digest = old_stamp.read() |
| 46 |
| 47 def IsStale(self): |
| 48 return self.old_digest != self.new_digest |
| 49 |
| 50 def Write(self): |
| 51 with open(self.stamp, 'w') as new_stamp: |
| 52 new_stamp.write(self.new_digest) |
OLD | NEW |