/scripts/helper.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 66 lines · 47 code · 13 blank · 6 comment · 10 complexity · 9549a2ad0873247c4515793d38b8ac4e MD5 · raw file

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