Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / tests / digest_tree
1 #!/usr/bin/python
2 #
3 # Cumulus: Efficient Filesystem Backup to the Cloud
4 # Copyright (C) 2012 The Cumulus Developers
5 # See the AUTHORS file for a list of contributors.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 # Print a dump containing file metadata and content digests for a specified
22 # file system tree.  Used to compare file system trees when running tests.
23
24 import hashlib
25 import os
26 import stat
27 import sys
28
29 include_mode = True
30 include_mtime = True
31
32 def stat_file(path):
33     st = os.lstat(path)
34     metadata = [path]
35     if include_mode:
36         metadata.append("mode=%o" % st.st_mode)
37     if include_mtime:
38         metadata.append("mtime=%d" % st.st_mtime)
39     if stat.S_ISREG(st.st_mode):
40         digest = hashlib.sha256()
41         BUF_SIZE = 1 << 16
42         f = open(path, "r")
43         while True:
44             buf = f.read(BUF_SIZE)
45             if not buf: break
46             digest.update(buf)
47         metadata.append("sha256=%s" % digest.hexdigest())
48     return metadata
49
50 def dump_tree(root):
51     files = []
52     for (dirpath, dirnames, filenames) in os.walk(root):
53         for d in dirnames:
54             files.append(stat_file(os.path.join(dirpath, d)))
55         for f in filenames:
56             files.append(stat_file(os.path.join(dirpath, f)))
57     files.sort()
58     return files
59
60 if __name__ == "__main__":
61     if len(sys.argv) > 1:
62         root = sys.argv[1]
63     else:
64         root = "."
65     os.chdir(root)
66     for file in dump_tree("."):
67         print " ".join(map(str, file))