From: Cameron Dale Date: Mon, 14 Apr 2008 05:22:25 +0000 (-0700) Subject: Use the apt_p2p_conf config import rather than passing parameters around. X-Git-Url: https://git.mxchange.org/?p=quix0rs-apt-p2p.git;a=commitdiff_plain;h=1e0537c366d9c95b4cda1b105fbcca52a9dbbb3a Use the apt_p2p_conf config import rather than passing parameters around. --- diff --git a/apt_p2p/AptPackages.py b/apt_p2p/AptPackages.py index 44c84b5..ef2a1f3 100644 --- a/apt_p2p/AptPackages.py +++ b/apt_p2p/AptPackages.py @@ -40,6 +40,7 @@ import apt_pkg, apt_inst from apt import OpProgress from debian_bundle import deb822 +from apt_p2p_conf import config from Hash import HashObject apt_pkg.init() @@ -120,8 +121,6 @@ class AptPackages: @ivar essential_files: files that must be created for apt to work @type cache_dir: L{twisted.python.filepath.FilePath} @ivar cache_dir: the directory to use for storing all files - @type unload_delay: C{int} - @ivar unload_delay: the time to wait before unloading the apt cache @ivar apt_config: the configuration parameters to use for apt @type packages: L{PackageFileList} @ivar packages: the persistent storage of tracked apt index files @@ -181,13 +180,12 @@ class AptPackages: 'apt/lists/partial') essential_files = ('apt/dpkg/status', 'apt/etc/sources.list',) - def __init__(self, cache_dir, unload_delay): + def __init__(self, cache_dir): """Construct a new packages manager. @param cache_dir: directory to use to store files for this mirror """ self.cache_dir = cache_dir - self.unload_delay = unload_delay self.apt_config = deepcopy(self.DEFAULT_APT_CONFIG) # Create the necessary files and directories for apt @@ -242,9 +240,9 @@ class AptPackages: """Make sure the package cache is initialized and loaded.""" # Reset the pending unload call if self.unload_later and self.unload_later.active(): - self.unload_later.reset(self.unload_delay) + self.unload_later.reset(config.gettime('DEFAULT', 'UNLOAD_PACKAGES_CACHE')) else: - self.unload_later = reactor.callLater(self.unload_delay, self.unload) + self.unload_later = reactor.callLater(config.gettime('DEFAULT', 'UNLOAD_PACKAGES_CACHE'), self.unload) # Make sure it's not already being loaded if self.loading is None: @@ -434,7 +432,7 @@ class TestAptPackages(unittest.TestCase): def setUp(self): """Initializes the cache with files found in the traditional apt location.""" - self.client = AptPackages(FilePath('/tmp/.apt-p2p'), 300) + self.client = AptPackages(FilePath('/tmp/.apt-p2p')) # Find the largest index files that are for 'main' self.packagesFile = os.popen('ls -Sr /var/lib/apt/lists/ | grep -E "_main_.*Packages$" | tail -n 1').read().rstrip('\n') diff --git a/apt_p2p/CacheManager.py b/apt_p2p/CacheManager.py index a990768..24c821e 100644 --- a/apt_p2p/CacheManager.py +++ b/apt_p2p/CacheManager.py @@ -19,6 +19,7 @@ from twisted.web2 import stream from twisted.web2.http import splitHostPort from Hash import HashObject +from apt_p2p_conf import config DECOMPRESS_EXTS = ['.gz', '.bz2'] DECOMPRESS_FILES = ['release', 'sources', 'packages'] @@ -206,22 +207,19 @@ class CacheManager: @ivar scanning: all the directories that are currectly being scanned or waiting to be scanned """ - def __init__(self, cache_dir, db, other_dirs = [], manager = None): + def __init__(self, cache_dir, db, manager = None): """Initialize the instance and remove any untracked files from the DB.. @type cache_dir: L{twisted.python.filepath.FilePath} @param cache_dir: the directory to use for storing all files @type db: L{db.DB} @param db: the database to use for tracking files and hashes - @type other_dirs: C{list} of L{twisted.python.filepath.FilePath} - @param other_dirs: the other directories that have shared files in them - (optional, defaults to only using the cache directory) @type manager: L{apt_p2p.AptP2P} @param manager: the main program object to send requests to (optional, defaults to not calling back with cached files) """ self.cache_dir = cache_dir - self.other_dirs = other_dirs + self.other_dirs = [FilePath(f) for f in config.getstringlist('DEFAULT', 'OTHER_DIRS')] self.all_dirs = self.other_dirs[:] self.all_dirs.insert(0, self.cache_dir) self.db = db diff --git a/apt_p2p/HTTPServer.py b/apt_p2p/HTTPServer.py index 7f74788..d6c12bf 100644 --- a/apt_p2p/HTTPServer.py +++ b/apt_p2p/HTTPServer.py @@ -12,6 +12,7 @@ from twisted.trial import unittest from twisted.python.filepath import FilePath from policies import ThrottlingFactory, ThrottlingProtocol, ProtocolWrapper +from apt_p2p_conf import config from apt_p2p_Khashmir.bencode import bencode class FileDownloader(static.File): @@ -169,7 +170,7 @@ class TopLevel(resource.Resource): addSlash = True - def __init__(self, directory, db, manager, uploadLimit): + def __init__(self, directory, db, manager): """Initialize the instance. @type directory: L{twisted.python.filepath.FilePath} @@ -183,8 +184,8 @@ class TopLevel(resource.Resource): self.db = db self.manager = manager self.uploadLimit = None - if uploadLimit > 0: - self.uploadLimit = int(uploadLimit*1024) + if config.getint('DEFAULT', 'UPLOAD_LIMIT') > 0: + self.uploadLimit = int(config.getint('DEFAULT', 'UPLOAD_LIMIT')*1024) self.factory = None def getHTTPFactory(self): @@ -266,7 +267,7 @@ class TestTopLevel(unittest.TestCase): file_hash = '\xf8~])+\x9d\xe5_B\xff\x1a\xc4!\xca \xb8\x0c\x00\xe7\x07' def setUp(self): - self.client = TopLevel(FilePath('/boot'), self, None, 0) + self.client = TopLevel(FilePath('/boot'), self, None) def lookupHash(self, hash): if hash == self.torrent_hash: diff --git a/apt_p2p/MirrorManager.py b/apt_p2p/MirrorManager.py index 4c19f10..5f51175 100644 --- a/apt_p2p/MirrorManager.py +++ b/apt_p2p/MirrorManager.py @@ -25,15 +25,12 @@ class MirrorManager: @type cache_dir: L{twisted.python.filepath.FilePath} @ivar cache_dir: the directory to use for storing all files - @type unload_delay: C{int} - @ivar unload_delay: the time to wait before unloading the apt cache @type apt_caches: C{dictionary} @ivar apt_caches: the avaliable mirrors """ - def __init__(self, cache_dir, unload_delay): + def __init__(self, cache_dir): self.cache_dir = cache_dir - self.unload_delay = unload_delay self.apt_caches = {} def extractPath(self, url): @@ -93,7 +90,7 @@ class MirrorManager: if baseDir not in self.apt_caches[site]: site_cache = self.cache_dir.child(aptpkg_dir).child('mirrors').child(site + baseDir.replace('/', '_')) site_cache.makedirs - self.apt_caches[site][baseDir] = AptPackages(site_cache, self.unload_delay) + self.apt_caches[site][baseDir] = AptPackages(site_cache) def updatedFile(self, url, file_path): """A file in the mirror has changed or been added. @@ -133,7 +130,7 @@ class TestMirrorManager(unittest.TestCase): client = None def setUp(self): - self.client = MirrorManager(FilePath('/tmp/.apt-p2p'), 300) + self.client = MirrorManager(FilePath('/tmp/.apt-p2p')) def test_extractPath(self): """Test extracting the site and base directory from various mirrors.""" diff --git a/apt_p2p/apt_p2p.py b/apt_p2p/apt_p2p.py index 1f04a87..fb47468 100644 --- a/apt_p2p/apt_p2p.py +++ b/apt_p2p/apt_p2p.py @@ -78,13 +78,11 @@ class AptP2P: self.dht = dhtClass() self.dht.loadConfig(config, config.get('DEFAULT', 'DHT')) self.dht.join().addCallbacks(self.joinComplete, self.joinError) - self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self, - config.getint('DEFAULT', 'UPLOAD_LIMIT')) + self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self) self.getHTTPFactory = self.http_server.getHTTPFactory self.peers = PeerManager(self.cache_dir, self.dht) - self.mirrors = MirrorManager(self.cache_dir, config.gettime('DEFAULT', 'UNLOAD_PACKAGES_CACHE')) - other_dirs = [FilePath(f) for f in config.getstringlist('DEFAULT', 'OTHER_DIRS')] - self.cache = CacheManager(self.cache_dir.child(download_dir), self.db, other_dirs, self) + self.mirrors = MirrorManager(self.cache_dir) + self.cache = CacheManager(self.cache_dir.child(download_dir), self.db, self) self.my_contact = None #{ DHT maintenance