PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/helper.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 66 lines | 47 code | 13 blank | 6 comment | 7 complexity | 9549a2ad0873247c4515793d38b8ac4e MD5 | raw file
 1#!/usr/bin/env python
 2"""
 3A command line helper for common operations performed by Galaxy maintainers.
 4Encodes and decodes IDs, returns Dataset IDs if provided an HDA or LDDA id,
 5returns the disk path of a dataset.
 6"""
 7
 8import os, sys
 9from ConfigParser import ConfigParser
10from optparse import OptionParser
11
12default_config = os.path.abspath( os.path.join( os.path.dirname( __file__ ), '..', 'universe_wsgi.ini') )
13
14parser = OptionParser()
15parser.add_option( '-c', '--config', dest='config', help='Path to Galaxy config file (universe_wsgi.ini)', default=default_config )
16parser.add_option( '-e', '--encode-id', dest='encode_id', help='Encode an ID' )
17parser.add_option( '-d', '--decode-id', dest='decode_id', help='Decode an ID' )
18parser.add_option( '--hda', dest='hda_id', help='Display HistoryDatasetAssociation info' )
19parser.add_option( '--ldda', dest='ldda_id', help='Display LibraryDatasetDatasetAssociation info' )
20( options, args ) = parser.parse_args()
21
22try:
23    assert options.encode_id or options.decode_id or options.hda_id or options.ldda_id
24except:
25    parser.print_help()
26    sys.exit( 1 )
27
28options.config = os.path.abspath( options.config )
29os.chdir( os.path.dirname( options.config ) )
30sys.path.append( 'lib' )
31
32from galaxy import eggs
33import pkg_resources
34
35config = ConfigParser( dict( file_path = 'database/files',
36                             id_secret = 'USING THE DEFAULT IS NOT SECURE!',
37                             database_connection = 'sqlite:///database/universe.sqlite?isolation_level=IMMEDIATE' ) )
38config.read( os.path.basename( options.config ) )
39
40from galaxy.web import security
41from galaxy.model import mapping
42
43helper = security.SecurityHelper( id_secret = config.get( 'app:main', 'id_secret' ) )
44model = mapping.init( config.get( 'app:main', 'file_path' ), config.get( 'app:main', 'database_connection' ), create_tables = False )
45
46if options.encode_id:
47    print 'Encoded "%s": %s' % ( options.encode_id, helper.encode_id( options.encode_id ) )
48
49if options.decode_id:
50    print 'Decoded "%s": %s' % ( options.decode_id, helper.decode_id( options.decode_id ) )
51
52if options.hda_id:
53    try:
54        hda_id = int( options.hda_id )
55    except:
56        hda_id = int( helper.decode_id( options.hda_id ) )
57    hda = model.context.current.query( model.HistoryDatasetAssociation ).get( hda_id )
58    print 'HDA "%s" is Dataset "%s" at: %s' % ( hda.id, hda.dataset.id, hda.file_name )
59
60if options.ldda_id:
61    try:
62        ldda_id = int( options.ldda_id )
63    except:
64        ldda_id = int( helper.decode_id( options.ldda_id ) )
65    ldda = model.context.current.query( model.HistoryDatasetAssociation ).get( ldda_id )
66    print 'LDDA "%s" is Dataset "%s" at: %s' % ( ldda.id, ldda.dataset.id, ldda.file_name )