PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/galaxy_messaging/server/galaxyweb_interface.py

https://bitbucket.org/xeme/galaxy-central
Python | 107 lines | 96 code | 7 blank | 4 comment | 2 complexity | 37a1ac981e76c976e4ae7b5af7ba2918 MD5 | raw file
  1. import ConfigParser
  2. import sys, os
  3. import array
  4. import time
  5. import optparse,array
  6. import shutil, traceback
  7. import urllib,urllib2, cookielib
  8. assert sys.version_info[:2] >= ( 2, 4 )
  9. new_path = [ os.path.join( os.getcwd(), "lib" ) ]
  10. new_path.extend( sys.path[1:] ) # remove scripts/ from the path
  11. sys.path = new_path
  12. from galaxy import eggs
  13. import pkg_resources
  14. import pkg_resources
  15. pkg_resources.require( "pycrypto" )
  16. from Crypto.Cipher import Blowfish
  17. from Crypto.Util.randpool import RandomPool
  18. from Crypto.Util import number
  19. class GalaxyWebInterface(object):
  20. def __init__(self, server_host, server_port, datatx_email, datatx_password, config_id_secret):
  21. self.server_host = server_host
  22. self.server_port = server_port
  23. self.datatx_email = datatx_email
  24. self.datatx_password = datatx_password
  25. self.config_id_secret = config_id_secret
  26. # create url
  27. self.base_url = "http://%s:%s" % (self.server_host, self.server_port)
  28. # login
  29. url = "%s/user/login?email=%s&password=%s&login_button=Login" % (self.base_url, self.datatx_email, self.datatx_password)
  30. cj = cookielib.CookieJar()
  31. self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  32. #print url
  33. f = self.opener.open(url)
  34. if f.read().find("ogged in as "+self.datatx_email) == -1:
  35. # if the user doesnt exist, create the user
  36. url = "%s/user/create?email=%s&username=%s&password=%s&confirm=%s&create_user_button=Submit" % ( self.base_url, self.datatx_email, self.datatx_email, self.datatx_password, self.datatx_password )
  37. f = self.opener.open(url)
  38. if f.read().find("ogged in as "+self.datatx_email) == -1:
  39. raise Exception("The "+self.datatx_email+" user could not login to Galaxy")
  40. def add_to_library(self, server_dir, library_id, folder_id, dbkey=''):
  41. '''
  42. This method adds the dataset file to the target data library & folder
  43. by opening the corresponding url in Galaxy server running.
  44. '''
  45. params = urllib.urlencode(dict( cntrller='library_admin',
  46. tool_id='upload1',
  47. tool_state='None',
  48. library_id=self.encode_id(library_id),
  49. folder_id=self.encode_id(folder_id),
  50. upload_option='upload_directory',
  51. file_type='auto',
  52. server_dir=os.path.basename(server_dir),
  53. dbkey=dbkey,
  54. show_dataset_id='True',
  55. runtool_btn='Upload to library'))
  56. url = self.base_url+"/library_common/upload_library_dataset"
  57. print url
  58. print params
  59. try:
  60. f = self.opener.open(url, params)
  61. if f.read().find("Data Library") == -1:
  62. raise Exception("Dataset could not be uploaded to the data library. URL: %s, PARAMS=%s" % (url, params))
  63. except:
  64. return 'ERROR', url, params
  65. def import_to_history(self, ldda_id, library_id, folder_id):
  66. params = urllib.urlencode(dict( cntrller='library_admin',
  67. show_deleted='False',
  68. library_id=self.encode_id(library_id),
  69. folder_id=self.encode_id(folder_id),
  70. ldda_ids=self.encode_id(ldda_id),
  71. do_action='import_to_history',
  72. use_panels='False'))
  73. #url = "http://lion.bx.psu.edu:8080/library_common/act_on_multiple_datasets?library_id=adb5f5c93f827949&show_deleted=False&ldda_ids=adb5f5c93f827949&cntrller=library_admin&do_action=import_to_history&use_panels=False"
  74. url = self.base_url+"/library_common/act_on_multiple_datasets"
  75. f = self.opener.open(url, params)
  76. x = f.read()
  77. if x.find("1 dataset(s) have been imported into your history.") == -1:
  78. raise Exception("Dataset could not be imported into history")
  79. def run_workflow(self, workflow_id, hid, workflow_step):
  80. input = str(workflow_step)+'|input'
  81. params = urllib.urlencode({'id':self.encode_id(workflow_id),
  82. 'run_workflow': 'Run workflow',
  83. input: hid})
  84. url = self.base_url+"/workflow/run"
  85. f = self.opener.open(url, params)
  86. def logout(self):
  87. # finally logout
  88. f = self.opener.open(self.base_url+'/user/logout')
  89. def encode_id(self, obj_id ):
  90. id_cipher = Blowfish.new( self.config_id_secret )
  91. # Convert to string
  92. s = str( obj_id )
  93. # Pad to a multiple of 8 with leading "!"
  94. s = ( "!" * ( 8 - len(s) % 8 ) ) + s
  95. # Encrypt
  96. return id_cipher.encrypt( s ).encode( 'hex' )