4 from twisted.python import log
5 from twisted.internet import defer
6 from twisted.trial import unittest
8 from AptPackages import AptPackages
12 class MirrorError(Exception):
13 """Exception raised when there's a problem with the mirror."""
16 """Manages all requests for mirror objects."""
18 def __init__(self, cache_dir):
19 self.cache_dir = cache_dir
22 def extractPath(self, path):
23 site, path = path.split('/',1)
25 site, path = path.split('/',1)
28 # Make sure a port is included for consistency
29 if site.find(':') < 0:
32 i = max(path.rfind('/dists/'), path.rfind('/pool/'))
37 # Uh oh, this is not good
38 log.msg("Couldn't find a good base directory for path: %s" % (site + path))
40 if site in self.apt_caches:
42 for base in self.apt_caches[site]:
44 for dirs in path.split('/'):
45 if base.startswith(base_match + '/' + dirs):
46 base_match += '/' + dirs
49 if len(base_match) > longest_match:
50 longest_match = len(base_match)
52 log.msg("Settled on baseDir: %s" % baseDir)
54 return site, baseDir, path
56 def init(self, site, baseDir):
57 if site not in self.apt_caches:
58 self.apt_caches[site] = {}
60 if baseDir not in self.apt_caches[site]:
61 site_cache = os.path.join(self.cache_dir, aptpkg_dir, 'mirrors', site + baseDir.replace('/', '_'))
62 self.apt_caches[site][baseDir] = AptPackages(site_cache)
64 def updatedFile(self, path, file_path):
65 site, baseDir, path = self.extractPath(path)
66 self.init(site, baseDir)
67 self.apt_caches[site][baseDir].file_updated(path, file_path)
69 def findHash(self, path):
70 site, baseDir, path = self.extractPath(path)
71 if site in self.apt_caches and baseDir in self.apt_caches[site]:
72 return self.apt_caches[site][baseDir].findHash(path)
74 d.errback(MirrorError("Site Not Found"))
77 class TestMirrorManager(unittest.TestCase):
78 """Unit tests for the mirror manager."""
84 self.client = MirrorManager('/tmp')
86 def test_extractPath(self):
87 site, baseDir, path = self.client.extractPath('/ftp.us.debian.org/debian/dists/unstable/Release')
88 self.failUnless(site == "ftp.us.debian.org:80", "no match: %s" % site)
89 self.failUnless(baseDir == "/debian", "no match: %s" % baseDir)
90 self.failUnless(path == "/dists/unstable/Release", "no match: %s" % path)
92 site, baseDir, path = self.client.extractPath('/ftp.us.debian.org:16999/debian/pool/d/dpkg/dpkg_1.2.1-1.tar.gz')
93 self.failUnless(site == "ftp.us.debian.org:16999", "no match: %s" % site)
94 self.failUnless(baseDir == "/debian", "no match: %s" % baseDir)
95 self.failUnless(path == "/pool/d/dpkg/dpkg_1.2.1-1.tar.gz", "no match: %s" % path)
97 def verifyHash(self, found_hash, path, true_hash):
98 self.failUnless(found_hash[0] == true_hash,
99 "%s hashes don't match: %s != %s" % (path, found_hash[0], true_hash))
101 def test_findHash(self):
102 self.packagesFile = os.popen('ls -Sr /var/lib/apt/lists/ | grep -E "_main_.*Packages$" | tail -n 1').read().rstrip('\n')
103 self.sourcesFile = os.popen('ls -Sr /var/lib/apt/lists/ | grep -E "_main_.*Sources$" | tail -n 1').read().rstrip('\n')
104 for f in os.walk('/var/lib/apt/lists').next()[2]:
105 if f[-7:] == "Release" and self.packagesFile.startswith(f[:-7]):
109 self.client.updatedFile('/' + self.releaseFile.replace('_','/'),
110 '/var/lib/apt/lists/' + self.releaseFile)
111 self.client.updatedFile('/' + self.releaseFile[:self.releaseFile.find('_dists_')+1].replace('_','/') +
112 self.packagesFile[self.packagesFile.find('_dists_')+1:].replace('_','/'),
113 '/var/lib/apt/lists/' + self.packagesFile)
114 self.client.updatedFile('/' + self.releaseFile[:self.releaseFile.find('_dists_')+1].replace('_','/') +
115 self.sourcesFile[self.sourcesFile.find('_dists_')+1:].replace('_','/'),
116 '/var/lib/apt/lists/' + self.sourcesFile)
118 lastDefer = defer.Deferred()
120 idx_hash = os.popen('grep -A 3000 -E "^SHA1:" ' +
121 '/var/lib/apt/lists/' + self.releaseFile +
122 ' | grep -E " main/binary-i386/Packages.bz2$"'
123 ' | head -n 1 | cut -d\ -f 2').read().rstrip('\n')
124 idx_path = '/' + self.releaseFile.replace('_','/')[:-7] + 'main/binary-i386/Packages.bz2'
126 d = self.client.findHash(idx_path)
127 d.addCallback(self.verifyHash, idx_path, idx_hash)
129 pkg_hash = os.popen('grep -A 30 -E "^Package: dpkg$" ' +
130 '/var/lib/apt/lists/' + self.packagesFile +
131 ' | grep -E "^SHA1:" | head -n 1' +
132 ' | cut -d\ -f 2').read().rstrip('\n')
133 pkg_path = '/' + self.releaseFile[:self.releaseFile.find('_dists_')+1].replace('_','/') + \
134 os.popen('grep -A 30 -E "^Package: dpkg$" ' +
135 '/var/lib/apt/lists/' + self.packagesFile +
136 ' | grep -E "^Filename:" | head -n 1' +
137 ' | cut -d\ -f 2').read().rstrip('\n')
139 d = self.client.findHash(pkg_path)
140 d.addCallback(self.verifyHash, pkg_path, pkg_hash)
142 src_dir = os.popen('grep -A 30 -E "^Package: dpkg$" ' +
143 '/var/lib/apt/lists/' + self.sourcesFile +
144 ' | grep -E "^Directory:" | head -n 1' +
145 ' | cut -d\ -f 2').read().rstrip('\n')
146 src_hashes = os.popen('grep -A 20 -E "^Package: dpkg$" ' +
147 '/var/lib/apt/lists/' + self.sourcesFile +
148 ' | grep -A 4 -E "^Files:" | grep -E "^ " ' +
149 ' | cut -d\ -f 2').read().split('\n')[:-1]
150 src_paths = os.popen('grep -A 20 -E "^Package: dpkg$" ' +
151 '/var/lib/apt/lists/' + self.sourcesFile +
152 ' | grep -A 4 -E "^Files:" | grep -E "^ " ' +
153 ' | cut -d\ -f 4').read().split('\n')[:-1]
155 for i in range(len(src_hashes)):
156 src_path = '/' + self.releaseFile[:self.releaseFile.find('_dists_')+1].replace('_','/') + src_dir + '/' + src_paths[i]
157 d = self.client.findHash(src_path)
158 d.addCallback(self.verifyHash, src_path, src_hashes[i])
160 idx_hash = os.popen('grep -A 3000 -E "^SHA1:" ' +
161 '/var/lib/apt/lists/' + self.releaseFile +
162 ' | grep -E " main/source/Sources.bz2$"'
163 ' | head -n 1 | cut -d\ -f 2').read().rstrip('\n')
164 idx_path = '/' + self.releaseFile.replace('_','/')[:-7] + 'main/source/Sources.bz2'
166 d = self.client.findHash(idx_path)
167 d.addCallback(self.verifyHash, idx_path, idx_hash)
169 d.addBoth(lastDefer.callback)
173 for p in self.pending_calls: