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.
9 def UpdateMd5ForFile(md5
, path
, block_size
=2**16):
10 with
open(path
, 'rb') as infile
:
12 data
= infile
.read(block_size
)
18 def UpdateMd5ForDirectory(md5
, dir_path
):
19 for root
, _
, files
in os
.walk(dir_path
):
21 UpdateMd5ForFile(md5
, os
.path
.join(root
, f
))
24 def UpdateMd5ForPath(md5
, path
):
25 if os
.path
.isdir(path
):
26 UpdateMd5ForDirectory(md5
, path
)
28 UpdateMd5ForFile(md5
, path
)
31 class Md5Checker(object):
32 def __init__(self
, stamp
=None, inputs
=[], command
=[]):
37 UpdateMd5ForPath(md5
, i
)
40 self
.new_digest
= md5
.hexdigest()
43 if os
.path
.exists(stamp
):
44 with
open(stamp
, 'r') as old_stamp
:
45 self
.old_digest
= old_stamp
.read()
48 return self
.old_digest
!= self
.new_digest
51 with
open(self
.stamp
, 'w') as new_stamp
:
52 new_stamp
.write(self
.new_digest
)