X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_dht%2FMirrorManager.py;h=c89a5915bc6622fdddd9fc4394656aa77732420f;hb=08e747941eaa12ddf466a49e4dadf292758d6cd2;hp=efa46d8d4529302d7c3400ee74221bb407bf5ec6;hpb=48b7787f8a2c434d67eb92d1d8bc6e97e9ecb755;p=quix0rs-apt-p2p.git diff --git a/apt_dht/MirrorManager.py b/apt_dht/MirrorManager.py index efa46d8..c89a591 100644 --- a/apt_dht/MirrorManager.py +++ b/apt_dht/MirrorManager.py @@ -3,6 +3,7 @@ from bz2 import BZ2Decompressor from zlib import decompressobj, MAX_WBITS from gzip import FCOMMENT, FEXTRA, FHCRC, FNAME, FTEXT from urlparse import urlparse +from binascii import a2b_hex import os from twisted.python import log, filepath @@ -186,13 +187,26 @@ class MirrorManager: site_cache = os.path.join(self.cache_dir, aptpkg_dir, 'mirrors', site + baseDir.replace('/', '_')) self.apt_caches[site][baseDir] = AptPackages(site_cache) + def updatedFile(self, url, file_path): + site, baseDir, path = self.extractPath(url) + self.init(site, baseDir) + self.apt_caches[site][baseDir].file_updated(path, file_path) + def findHash(self, url): site, baseDir, path = self.extractPath(url) if site in self.apt_caches and baseDir in self.apt_caches[site]: - return self.apt_caches[site][baseDir].findHash(path) + d = self.apt_caches[site][baseDir].findHash(path) + d.addCallback(self.translateHash) + return d d = defer.Deferred() d.errback(MirrorError("Site Not Found")) return d + + def translateHash(self, (hash, size)): + """Translate a hash from apt's hex encoding to a string.""" + if hash: + hash = a2b_hex(hash) + return (hash, size) def save_file(self, response, hash, size, url): """Save a downloaded file to the cache and stream it.""" @@ -235,11 +249,9 @@ class MirrorManager: if ext: os.utime(decFile.path, (modtime, modtime)) - site, baseDir, path = self.extractPath(url) - self.init(site, baseDir) - self.apt_caches[site][baseDir].file_updated(path, destFile.path) + self.updatedFile(url, destFile.path) if ext: - self.apt_caches[site][baseDir].file_updated(path[:-len(ext)], decFile.path) + self.updatedFile(url[:-len(ext)], decFile.path) def save_error(self, failure, url): """An error has occurred in downloadign or saving the file.""" @@ -303,7 +315,7 @@ class TestMirrorManager(unittest.TestCase): idx_path = 'http://' + self.releaseFile.replace('_','/')[:-7] + 'main/binary-i386/Packages.bz2' d = self.client.findHash(idx_path) - d.addCallback(self.verifyHash, idx_path, idx_hash) + d.addCallback(self.verifyHash, idx_path, a2b_hex(idx_hash)) pkg_hash = os.popen('grep -A 30 -E "^Package: dpkg$" ' + '/var/lib/apt/lists/' + self.packagesFile + @@ -316,7 +328,7 @@ class TestMirrorManager(unittest.TestCase): ' | cut -d\ -f 2').read().rstrip('\n') d = self.client.findHash(pkg_path) - d.addCallback(self.verifyHash, pkg_path, pkg_hash) + d.addCallback(self.verifyHash, pkg_path, a2b_hex(pkg_hash)) src_dir = os.popen('grep -A 30 -E "^Package: dpkg$" ' + '/var/lib/apt/lists/' + self.sourcesFile + @@ -334,7 +346,7 @@ class TestMirrorManager(unittest.TestCase): for i in range(len(src_hashes)): src_path = 'http://' + self.releaseFile[:self.releaseFile.find('_dists_')+1].replace('_','/') + src_dir + '/' + src_paths[i] d = self.client.findHash(src_path) - d.addCallback(self.verifyHash, src_path, src_hashes[i]) + d.addCallback(self.verifyHash, src_path, a2b_hex(src_hashes[i])) idx_hash = os.popen('grep -A 3000 -E "^SHA1:" ' + '/var/lib/apt/lists/' + self.releaseFile + @@ -343,7 +355,7 @@ class TestMirrorManager(unittest.TestCase): idx_path = 'http://' + self.releaseFile.replace('_','/')[:-7] + 'main/source/Sources.bz2' d = self.client.findHash(idx_path) - d.addCallback(self.verifyHash, idx_path, idx_hash) + d.addCallback(self.verifyHash, idx_path, a2b_hex(idx_hash)) d.addBoth(lastDefer.callback) return lastDefer