Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Thomas's own compute_dir_index function. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions dirtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down