From 97fa18e6aa616ca0f370f6ddeb85ee198b8408a2 Mon Sep 17 00:00:00 2001 From: kvrijt Date: Thu, 18 Dec 2014 15:50:26 +0100 Subject: [PATCH] Added Thomas's own compute_dir_index function. Added Thomas's own compute_dir_index function with one correction and one addition. Correction: index[f] = os.path.getmtime(os.path.join(path, files[0])) index[f] = os.path.getmtime(os.path.join(path, f)) Addition: Optionally use hash for indexing. --- dirtools.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/dirtools.py b/dirtools.py index e9f9c98..7f1a311 100644 --- a/dirtools.py +++ b/dirtools.py @@ -368,6 +368,32 @@ def from_json(cls, path): return cls(state=json.loads(f.read())) +def compute_dir_index(path, hash=False): + """ Return a tuple containing: + - list of files (relative to path) + - lisf of subdirs (relative to path) + - a dict: filepath => last or optionally hash + """ + files = [] + subdirs = [] + + for root, dirs, filenames in os.walk(path): + for subdir in dirs: + subdirs.append(os.path.relpath(os.path.join(root, subdir), path)) + + for f in filenames: + files.append(os.path.relpath(os.path.join(root, f), path)) + + index = {} + for f in files: + if hash: + index[f] = filehash(os.path.join(path, f)) + else: + index[f] = os.path.getmtime(os.path.join(path, f)) + + return dict(files=files, subdirs=subdirs, index=index) + + def compute_diff(dir_base, dir_cmp): """ Compare `dir_base' and `dir_cmp' and returns a list with the following keys: