PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/scripts/set_dataset_sizes.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 53 lines | 41 code | 11 blank | 1 comment | 7 complexity | 062bd0d359d4ec8c0e327375cf72d47a MD5 | raw file
 1#!/usr/bin/env python
 2
 3import os, sys
 4from ConfigParser import ConfigParser
 5from optparse import OptionParser
 6
 7default_config = os.path.abspath( os.path.join( os.path.dirname( __file__ ), '..', 'universe_wsgi.ini') )
 8
 9parser = OptionParser()
10parser.add_option( '-c', '--config', dest='config', help='Path to Galaxy config file (universe_wsgi.ini)', default=default_config )
11( options, args ) = parser.parse_args()
12
13def init():
14
15    options.config = os.path.abspath( options.config )
16    os.chdir( os.path.dirname( options.config ) )
17    sys.path.append( 'lib' )
18
19    from galaxy import eggs
20    import pkg_resources
21
22    config = ConfigParser( dict( file_path = 'database/files',
23                                 database_connection = 'sqlite:///database/universe.sqlite?isolation_level=IMMEDIATE' ) )
24    config.read( os.path.basename( options.config ) )
25
26    from galaxy.model import mapping
27
28    return mapping.init( config.get( 'app:main', 'file_path' ), config.get( 'app:main', 'database_connection' ), create_tables = False )
29
30if __name__ == '__main__':
31    print 'Loading Galaxy model...'
32    model = init()
33    sa_session = model.context.current
34
35    set = 0
36    dataset_count = sa_session.query( model.Dataset ).count()
37    print 'Processing %i datasets...' % dataset_count
38    percent = 0
39    print 'Completed %i%%' % percent,
40    sys.stdout.flush()
41    for i, dataset in enumerate( sa_session.query( model.Dataset ).enable_eagerloads( False ).yield_per( 1000 ) ):
42        if dataset.total_size is None:
43            dataset.set_total_size()
44            set += 1
45            if not set % 1000:
46                sa_session.flush()
47        new_percent = int( float(i) / dataset_count * 100 )
48        if new_percent != percent:
49            percent = new_percent
50            print '\rCompleted %i%%' % percent,
51            sys.stdout.flush()
52    sa_session.flush()
53    print 'Completed 100%%'