You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

  1. # Copyright 2018 ACSONE SA/NV.
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. from fnmatch import fnmatch
  4. import hashlib
  5. import os
  6. def _fnmatch(filename, patterns):
  7. for pattern in patterns:
  8. if fnmatch(filename, pattern):
  9. return True
  10. return False
  11. def _walk(top, exclude_patterns, keep_langs):
  12. keep_langs = {l.split('_')[0] for l in keep_langs}
  13. for dirpath, dirnames, filenames in os.walk(top):
  14. dirnames.sort()
  15. reldir = os.path.relpath(dirpath, top)
  16. if reldir == '.':
  17. reldir = ''
  18. for filename in sorted(filenames):
  19. filepath = os.path.join(reldir, filename)
  20. if _fnmatch(filepath, exclude_patterns):
  21. continue
  22. if keep_langs and reldir in {'i18n', 'i18n_extra'}:
  23. basename, ext = os.path.splitext(filename)
  24. if ext == '.po':
  25. if basename.split('_')[0] not in keep_langs:
  26. continue
  27. yield filepath
  28. def addon_hash(top, exclude_patterns, keep_langs):
  29. """Compute a sha1 digest of file contents."""
  30. m = hashlib.sha1()
  31. for filepath in _walk(top, exclude_patterns, keep_langs):
  32. # hash filename so empty files influence the hash
  33. m.update(filepath.encode('utf-8'))
  34. # hash file content
  35. with open(os.path.join(top, filepath), 'rb') as f:
  36. m.update(f.read())
  37. return m.hexdigest()