Revision 4b79a2b0b7ca415fe75d72b8d9812452467ed351 authored by Nicolas Dandrimont on 23 October 2019, 11:55:18 UTC, committed by Nicolas Dandrimont on 23 October 2019, 12:22:15 UTC
Instead of relying on attr.asdict recursion, we do recursion ourselves. This simplifies a lot of the inherited to_dict() methods.
1 parent 2e4558c
swh-hashtree
#!/usr/bin/env python3
# Use sample:
# swh-hashtree --path . --ignore '.svn' --ignore '.git-svn' \
# --ignore-empty-folders
# 38f8d2c3a951f6b94007896d0981077e48bbd702
import click
import os
from swh.model import from_disk, hashutil
def combine_filters(*filters):
"""Combine several ignore filters"""
if len(filters) == 0:
return from_disk.accept_all_directories
elif len(filters) == 1:
return filters[0]
def combined_filter(*args, **kwargs):
return all(filter(*args, **kwargs) for filter in filters)
return combined_filter
@click.command()
@click.option('--path', default='.',
help='Optional path to hash.')
@click.option('--ignore-empty-folder', is_flag=True, default=False,
help='Ignore empty folder.')
@click.option('--ignore', multiple=True,
help='Ignore pattern.')
def main(path, ignore_empty_folder=False, ignore=None):
filters = []
if ignore_empty_folder:
filters.append(from_disk.ignore_empty_directories)
if ignore:
filters.append(
from_disk.ignore_named_directories(
[os.fsencode(name) for name in ignore]
)
)
try:
d = from_disk.Directory.from_disk(path=os.fsencode(path),
dir_filter=combine_filters(*filters))
hash = d.hash
except Exception as e:
print(e)
return
else:
print(hashutil.hash_to_hex(hash))
if __name__ == '__main__':
main()
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...