Use the apt_p2p_conf config import rather than passing parameters around.
authorCameron Dale <camrdale@gmail.com>
Mon, 14 Apr 2008 05:22:25 +0000 (22:22 -0700)
committerCameron Dale <camrdale@gmail.com>
Mon, 14 Apr 2008 05:22:25 +0000 (22:22 -0700)
apt_p2p/AptPackages.py
apt_p2p/CacheManager.py
apt_p2p/HTTPServer.py
apt_p2p/MirrorManager.py
apt_p2p/apt_p2p.py

index 44c84b56bdb1b4e7b5158f300e6bb70bbd7e81d1..ef2a1f327c90429b3d1d9d11f6f0e51e9b4e42e0 100644 (file)
@@ -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')
index a990768dfc3d7f7d906ace58b67f53f303bef1b8..24c821eda49a9d220bec462c0914122500c39b1e 100644 (file)
@@ -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
index 7f74788d8b1476f1224ef153346def8dd9a5ec3f..d6c12bf3ce402a949700b53887a4fd94c5a06d2a 100644 (file)
@@ -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:
index 4c19f1070afb66e49d53250251342dad2a845e11..5f511758dfda0026a722f2b7bd4b14120eb3d732 100644 (file)
@@ -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."""
index 1f04a87da3b97feb1d9e03a0b0ebaf516a16f67b..fb47468cb8c3b7d0865863725d224873133360ef 100644 (file)
@@ -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