]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_p2p/AptPackages.py
Fix some documentation errors.
[quix0rs-apt-p2p.git] / apt_p2p / AptPackages.py
index 44c84b56bdb1b4e7b5158f300e6bb70bbd7e81d1..f7681a7380c1c53050a8b77c19f0aeb503b5b3f9 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()
@@ -91,6 +92,7 @@ class PackageFileList(DictMixin):
         if filename.lower() in TRACKED_FILES:
             log.msg("Registering package file: "+cache_path)
             self.packages[cache_path] = file_path
+            self.packages.sync()
             return True
         return False
 
@@ -102,6 +104,7 @@ class PackageFileList(DictMixin):
             if not self.packages[f].exists():
                 log.msg("File in packages database has been deleted: "+f)
                 del self.packages[f]
+        self.packages.sync()
 
     #{ Dictionary interface details
     def __getitem__(self, key): return self.packages[key]
@@ -120,8 +123,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
@@ -130,6 +131,8 @@ class AptPackages:
     @type loading: L{twisted.internet.defer.Deferred}
     @ivar loading: if the cache is currently being loaded, this will be
         called when it is loaded, otherwise it is None
+    @type loading_unload: C{boolean}
+    @ivar loading_unload: whether there is an unload pending on the current load
     @type unload_later: L{twisted.internet.interfaces.IDelayedCall}
     @ivar unload_later: the delayed call to unload the apt cache
     @type indexrecords: C{dictionary}
@@ -181,13 +184,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
@@ -205,6 +207,7 @@ class AptPackages:
         self.packages = PackageFileList(cache_dir)
         self.loaded = False
         self.loading = None
+        self.loading_unload = False
         self.unload_later = None
         
     def __del__(self):
@@ -242,13 +245,18 @@ 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)
+        
+        # Check if it's already loaded
+        if self.loaded:
+            return defer.succeed(True)
+        
         # Make sure it's not already being loaded
         if self.loading is None:
             log.msg('Loading the packages cache')
+            self.loading_unload = False
             self.loading = threads.deferToThread(self._load)
             self.loading.addCallback(self.doneLoading)
         return self.loading
@@ -256,6 +264,16 @@ class AptPackages:
     def doneLoading(self, loadResult):
         """Cache is loaded."""
         self.loading = None
+        
+        # Check for a pending unload
+        if self.loading_unload:
+            log.msg('Re-loading the packages cache')
+            self.unload()
+            self.loading_unload = False
+            self.loading = threads.deferToThread(self._load)
+            self.loading.addCallback(self.doneLoading)
+            return self.loading
+            
         # Must pass on the result for the next callback
         return loadResult
         
@@ -328,7 +346,9 @@ class AptPackages:
         if self.unload_later and self.unload_later.active():
             self.unload_later.cancel()
         self.unload_later = None
-        if self.loaded:
+        if self.loading:
+            self.loading_unload = True
+        elif self.loaded:
             log.msg('Unloading the packages cache')
             # This should save memory
             del self.cache
@@ -434,7 +454,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')