]> git.decadent.org.uk Git - dak.git/blob - daklib/packages.py
Merge remote branch 'ftpmaster/master' into psimport
[dak.git] / daklib / packages.py
1 #!/usr/bin/env python
2 """
3 Helper code for packages generation.
4
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2011 Torsten Werner <twerner@debian.org>
7 @copyright: 2011 Mark Hymers <mhy@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 ################################################################################
12
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 ################################################################################
28
29 from daklib.dbconn import *
30 from daklib.config import Config
31
32 from multiprocessing import Pool
33 from subprocess import Popen, PIPE
34
35 import os.path
36
37 class PackagesScanner(object):
38     '''
39     PackagesScanner provides a threadsafe method scan() to scan the metadata of
40     a DBBinary object.
41     '''
42     def __init__(self, binary_id):
43         '''
44         The argument binary_id is the id of the DBBinary object that
45         should be scanned.
46         '''
47         self.binary_id = binary_id
48
49     def scan(self, dummy_arg = None):
50         '''
51         This method does the actual scan and fills in the associated metadata
52         property. It commits any changes to the database. The argument dummy_arg
53         is ignored but needed by our threadpool implementation.
54         '''
55         session = DBConn().session()
56         binary = session.query(DBBinary).get(self.binary_id)
57         fileset = set(binary.read_control())
58         print fileset
59         #if len(fileset) == 0:
60         #    fileset.add('EMPTY_PACKAGE')
61         #for filename in fileset:
62         #    binary.contents.append(BinContents(file = filename))
63         #session.commit()
64         session.close()
65
66     @classmethod
67     def scan_all(class_, limit = None):
68         '''
69         The class method scan_all() scans all binaries using multiple threads.
70         The number of binaries to be scanned can be limited with the limit
71         argument. Returns the number of processed and remaining packages as a
72         dict.
73         '''
74         session = DBConn().session()
75         query = session.query(DBBinary).filter(DBBinary.contents == None)
76         remaining = query.count
77         if limit is not None:
78             query = query.limit(limit)
79         processed = query.count()
80         pool = Pool()
81         for binary in query.yield_per(100):
82             pool.apply_async(scan_helper, (binary.binary_id, ))
83         pool.close()
84         pool.join()
85         remaining = remaining()
86         session.close()
87         return { 'processed': processed, 'remaining': remaining }
88
89 def scan_helper(binary_id):
90     '''
91     This function runs in a subprocess.
92     '''
93     scanner = PackagesScanner(binary_id)
94     scanner.scan()