未验证 提交 e4397235 编写于 作者: 😸 😸

add wrapper class around `apt_pkg.Hashes`

The interface changed in incompatible ways between Debian 10 (buster)
and Debian bullseye.
上级 515ec982
# -*- coding: utf-8 -*-
"""
interfaces around python-apt
@copyright: 2020 😸 <😸@43-1.org>
@license: GNU General Public License version 2 or later
"""
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import apt_pkg
"""
wrapper around `apt_pkg.Hashes`
"""
class DakHashes(object):
def __init__(self, *args, **kwargs):
self._apt_hashes = apt_pkg.Hashes(*args, **kwargs)
# python-apt in Debian 10 (buster) uses
# `apt_pkg.Hashes(...).md5`
# but in Debian bullseye it switched to
# `apt_pkg.Hashes(...).find('md5sum').hashvalue`
def _hashvalue(self, attr, name):
if hasattr(self._apt_hashes, attr):
return getattr(self._apt_hashes, attr)
else:
return self._apt_hashes.hashes.find(name).hashvalue
@property
def md5(self):
return self._hashvalue('md5', 'md5sum')
@property
def sha1(self):
return self._hashvalue('sha1', 'sha1')
@property
def sha256(self):
return self._hashvalue('sha256', 'sha256')
......@@ -16,6 +16,7 @@
import daklib.compress
import daklib.config
import daklib.dakapt
import daklib.dbconn
import daklib.gpg
import daklib.upload
......@@ -79,7 +80,7 @@ class File(object):
return self._tmp
def hashes(self):
return apt_pkg.Hashes(self.fh())
return daklib.dakapt.DakHashes(self.fh())
def obtain_file(base, path):
......
......@@ -31,6 +31,7 @@ import six
from daklib.aptversion import AptVersion
from daklib.gpg import SignedFile
from daklib.regexes import *
import daklib.dakapt
import daklib.packagelist
......@@ -156,7 +157,7 @@ class HashedFile(object):
path = os.path.join(directory, filename)
with open(path, 'r') as fh:
size = os.fstat(fh.fileno()).st_size
hashes = apt_pkg.Hashes(fh)
hashes = daklib.dakapt.DakHashes(fh)
return cls(filename, size, hashes.md5, hashes.sha1, hashes.sha256, section, priority)
def check(self, directory):
......@@ -181,7 +182,7 @@ class HashedFile(object):
def check_fh(self, fh):
size = os.fstat(fh.fileno()).st_size
fh.seek(0)
hashes = apt_pkg.Hashes(fh)
hashes = daklib.dakapt.DakHashes(fh)
if size != self.size:
raise InvalidHashException(self.filename, 'size', self.size, size)
......
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# © 2020, 😸 <😸@43-1.org>
#
# License: GPL-2+
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from base_test import DakTestCase
import daklib.dakapt
class TestDakHashes(DakTestCase):
def testDakHashes(self):
hashes = daklib.dakapt.DakHashes("/dev/null")
self.assertEqual(hashes.md5, "d41d8cd98f00b204e9800998ecf8427e")
self.assertEqual(hashes.sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
self.assertEqual(hashes.sha256, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册