dakapt.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. """
  3. interfaces around python-apt
  4. @copyright: 2020 😸 <😸@43-1.org>
  5. @license: GNU General Public License version 2 or later
  6. """
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. import apt_pkg
  20. class DakHashes:
  21. """
  22. wrapper around `apt_pkg.Hashes`
  23. """
  24. def __init__(self, *args, **kwargs):
  25. self._apt_hashes = apt_pkg.Hashes(*args, **kwargs)
  26. # python-apt in Debian 10 (buster) uses
  27. # `apt_pkg.Hashes(...).md5`
  28. # but in Debian bullseye it switched to
  29. # `apt_pkg.Hashes(...).find('md5sum').hashvalue`
  30. def _hashvalue(self, name):
  31. h = self._apt_hashes.hashes.find(name)
  32. try:
  33. return h.hashvalue
  34. except AttributeError:
  35. return str(h)[len(name) + 1:]
  36. @property
  37. def md5(self):
  38. return self._hashvalue('md5sum')
  39. @property
  40. def sha1(self):
  41. return self._hashvalue('sha1')
  42. @property
  43. def sha256(self):
  44. return self._hashvalue('sha256')