+ gzip.wait()
+ os.remove(final_filename)
+ os.rename(temp_filename, final_filename)
+ os.chmod(final_filename, 0664)
+
+ @classmethod
+ def write_all(class_, suite_names = [], force = False):
+ '''
+ Writes all Contents files for suites in list suite_names which defaults
+ to all 'touchable' suites if not specified explicitely. Untouchable
+ suites will be included if the force argument is set to True.
+ '''
+ session = DBConn().session()
+ suite_query = session.query(Suite)
+ if len(suite_names) > 0:
+ suite_query = suite_query.filter(Suite.suite_name.in_(suite_names))
+ if not force:
+ suite_query = suite_query.filter_by(untouchable = False)
+ deb_id = get_override_type('deb', session).overridetype_id
+ udeb_id = get_override_type('udeb', session).overridetype_id
+ main_id = get_component('main', session).component_id
+ non_free_id = get_component('non-free', session).component_id
+ pool = Pool()
+ for suite in suite_query:
+ suite_id = suite.suite_id
+ for architecture in suite.get_architectures(skipsrc = True, skipall = True):
+ arch_id = architecture.arch_id
+ # handle 'deb' packages
+ pool.apply_async(generate_helper, (suite_id, arch_id, deb_id))
+ # handle 'udeb' packages for 'main' and 'non-free'
+ pool.apply_async(generate_helper, (suite_id, arch_id, udeb_id, main_id))
+ pool.apply_async(generate_helper, (suite_id, arch_id, udeb_id, non_free_id))
+ pool.close()
+ pool.join()
+ session.close()
+
+def generate_helper(suite_id, arch_id, overridetype_id, component_id = None):
+ '''
+ This function is called in a new subprocess.
+ '''
+ DBConn().reset()
+ session = DBConn().session()
+ suite = Suite.get(suite_id, session)
+ architecture = Architecture.get(arch_id, session)
+ overridetype = OverrideType.get(overridetype_id, session)
+ if component_id is None:
+ component = None
+ else:
+ component = Component.get(component_id, session)
+ contents_writer = ContentsWriter(suite, architecture, overridetype, component)
+ contents_writer.write_file()
+
+
+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)
+ fileset = set(binary.scan_contents())
+ if len(fileset) == 0:
+ fileset.add('EMPTY_PACKAGE')
+ for filename in fileset:
+ 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. Returns the number of processed and remaining packages as a
+ dict.
+ '''
+ session = DBConn().session()
+ query = session.query(DBBinary).filter(DBBinary.contents == None)
+ remaining = query.count
+ if limit is not None:
+ query = query.limit(limit)
+ processed = query.count()
+ threadpool = ThreadPool()
+ for binary in query.yield_per(100):
+ threadpool.queueTask(ContentsScanner(binary).scan)
+ threadpool.joinAll()
+ remaining = remaining()
+ session.close()
+ return { 'processed': processed, 'remaining': remaining }