X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=blobdiff_plain;f=tests%2Fdigest_tree;fp=tests%2Fdigest_tree;h=2ee419e64db6a94eaebb1b31f5fc43986b2bb497;hp=0000000000000000000000000000000000000000;hb=f546e4df51cde51dd984e4ad26e70dd7533f9791;hpb=3d780590edec4583eb3ef0ca16120afd0f7451f9 diff --git a/tests/digest_tree b/tests/digest_tree new file mode 100755 index 0000000..2ee419e --- /dev/null +++ b/tests/digest_tree @@ -0,0 +1,67 @@ +#!/usr/bin/python +# +# Cumulus: Efficient Filesystem Backup to the Cloud +# Copyright (C) 2012 The Cumulus Developers +# See the AUTHORS file for a list of contributors. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Print a dump containing file metadata and content digests for a specified +# file system tree. Used to compare file system trees when running tests. + +import hashlib +import os +import stat +import sys + +include_mode = True +include_mtime = True + +def stat_file(path): + st = os.lstat(path) + metadata = [path] + if include_mode: + metadata.append("mode=%o" % st.st_mode) + if include_mtime: + metadata.append("size=%d" % st.st_mtime) + if stat.S_ISREG(st.st_mode): + digest = hashlib.sha256() + BUF_SIZE = 1 << 16 + f = open(path, "r") + while True: + buf = f.read(BUF_SIZE) + if not buf: break + digest.update(buf) + metadata.append("sha256=%s" % digest.hexdigest()) + return metadata + +def dump_tree(root): + files = [] + for (dirpath, dirnames, filenames) in os.walk(root): + for d in dirnames: + files.append(stat_file(os.path.join(dirpath, d))) + for f in filenames: + files.append(stat_file(os.path.join(dirpath, f))) + files.sort() + return files + +if __name__ == "__main__": + if len(sys.argv) > 1: + root = sys.argv[1] + else: + root = "." + os.chdir(root) + for file in dump_tree("."): + print " ".join(map(str, file))