[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[dak/master 4/8] Add new class ContentsScanner.



Signed-off-by: Torsten Werner <twerner@debian.org>
---
 daklib/contents.py       |   44 ++++++++++++++++++++++++++++++++++++++++++++
 tests/dbtest_contents.py |    8 +++++++-
 2 files changed, 51 insertions(+), 1 deletions(-)

diff --git a/daklib/contents.py b/daklib/contents.py
index 7914c20..3abc501 100755
--- a/daklib/contents.py
+++ b/daklib/contents.py
@@ -27,6 +27,7 @@ Helper code for contents generation.
 
 from daklib.dbconn import *
 from daklib.config import Config
+from daklib.threadpool import ThreadPool
 
 from sqlalchemy import desc, or_
 from subprocess import Popen, PIPE
@@ -177,3 +178,46 @@ select bc.file, substring(o.section from position('/' in o.section) + 1) || '/'
             pipe.write(item)
         pipe.close()
         output_file.close()
+
+
+class ContentsScanner(object):
+    '''
+    ContentsScanner provides a threadsafe method scan() to scan the contents of
+    a DBBinary object.
+    '''
+    def __init__(self, binary):
+        '''
+        The argument binary is the actual DBBinary object that should be
+        scanned.
+        '''
+        self.binary_id = binary.binary_id
+
+    def scan(self, dummy_arg = None):
+        '''
+        This method does the actual scan and fills in the associated BinContents
+        property. It commits any changes to the database. The argument dummy_arg
+        is ignored but needed by our threadpool implementation.
+        '''
+        session = DBConn().session()
+        binary = session.query(DBBinary).get(self.binary_id)
+        for filename in binary.scan_contents():
+            binary.contents.append(BinContents(file = filename))
+        session.commit()
+        session.close()
+
+    @classmethod
+    def scan_all(class_, limit = None):
+        '''
+        The class method scan_all() scans all binaries using multiple threads.
+        The number of binaries to be scanned can be limited with the limit
+        argument.
+        '''
+        session = DBConn().session()
+        query = session.query(DBBinary)
+        if limit is not None:
+            query = query.limit(limit)
+        threadpool = ThreadPool()
+        for binary in query.yield_per(100):
+            threadpool.queueTask(ContentsScanner(binary).scan)
+        threadpool.joinAll()
+        session.close()
diff --git a/tests/dbtest_contents.py b/tests/dbtest_contents.py
index 575facd..7402601 100755
--- a/tests/dbtest_contents.py
+++ b/tests/dbtest_contents.py
@@ -3,7 +3,7 @@
 from db_test import DBDakTestCase
 
 from daklib.dbconn import *
-from daklib.contents import ContentsWriter
+from daklib.contents import ContentsWriter, ContentsScanner
 
 from sqlalchemy.exc import FlushError, IntegrityError
 import unittest
@@ -160,6 +160,12 @@ class ContentsTestCase(DBDakTestCase):
         filelist = [f for f in self.binary['hello_2.2-1_i386'].scan_contents()]
         self.assertEqual(['usr/bin/hello', 'usr/share/doc/hello/copyright'],
             filelist)
+        self.session.commit()
+        ContentsScanner(self.binary['hello_2.2-1_i386']).scan()
+        bin_contents_list = self.binary['hello_2.2-1_i386'].contents.all()
+        self.assertEqual(2, len(bin_contents_list))
+        self.assertEqual('usr/bin/hello', bin_contents_list[0].file)
+        self.assertEqual('usr/share/doc/hello/copyright', bin_contents_list[1].file)
 
     def classes_to_clean(self):
         return [Override, Suite, BinContents, DBBinary, DBSource, Architecture, Section, \
-- 
1.7.2.3



Reply to: