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.

45 lines
1.4 KiB

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