1- from bioblend import galaxy
2- from bioblend .galaxy .tools import ToolClient
3- from bioblend .galaxy .histories import HistoryClient
4- from bioblend .galaxy .datasets import DatasetClient
1+ #!/usr/bin/env python
52from bioblend .galaxy import objects
63import subprocess
74import argparse
85import os
96from string import Template
107import logging
11- logging .getLogger ("bioblend" ).setLevel (logging .WARNING )
8+ DEBUG = os .environ .get ('DEBUG' , "False" ).lower () == 'true'
9+ if DEBUG :
10+ logging .basicConfig (level = logging .DEBUG )
11+ logging .getLogger ("bioblend" ).setLevel (logging .CRITICAL )
12+ log = logging .getLogger ()
1213
13- # Consider not using objects deprecated.
14- DEFAULT_USE_OBJECTS = True
15- ENV_KEYS = ('DEBUG' , 'GALAXY_WEB_PORT' , 'NOTEBOOK_PASSWORD' , 'CORS_ORIGIN' ,
16- 'DOCKER_PORT' , 'API_KEY' , 'HISTORY_ID' , 'REMOTE_HOST' ,
17- 'GALAXY_URL' )
18-
19-
20- def _get_conf ():
21- conf = {}
22- for key in ENV_KEYS :
23- conf [key .lower ()] = os .environ .get (key , None )
24- conf ['galaxy_paster_port' ] = conf ['galaxy_web_port' ]
25- return conf
2614
2715
2816def _get_ip ():
@@ -35,25 +23,23 @@ def _get_ip():
3523 cmd_awk = ['awk' , '{ print $2 }' ]
3624 p3 = subprocess .Popen (cmd_awk , stdin = p2 .stdout , stdout = subprocess .PIPE )
3725 galaxy_ip = p3 .stdout .read ()
26+ log .debug ('Host IP determined to be %s' , galaxy_ip )
3827 return galaxy_ip
3928
4029
41- def _test_url (url , key , history_id , use_objects = False ):
30+ def _test_url (url , key , history_id ):
4231 """Test the functionality of a given galaxy URL, to ensure we can connect
4332 on that address."""
4433 try :
45- if use_objects :
46- gi = objects .GalaxyInstance (url , key )
47- gi .histories .get (history_id )
48- else :
49- gi = galaxy .GalaxyInstance (url = url , key = key )
50- gi .histories .get_histories ()
34+ gi = objects .GalaxyInstance (url , key )
35+ gi .histories .get (history_id )
36+ log .debug ('Galaxy URL %s is functional' , url )
5137 return gi
5238 except Exception :
5339 return None
5440
5541
56- def get_galaxy_connection ( use_objects = DEFAULT_USE_OBJECTS ):
42+ def get_galaxy_connection (history_id = None ):
5743 """
5844 Given access to the configuration dict that galaxy passed us, we try and connect to galaxy's API.
5945
@@ -69,97 +55,76 @@ def get_galaxy_connection( use_objects=DEFAULT_USE_OBJECTS ):
6955 through. This will succeed where the previous connection fails under
7056 the conditions of REMOTE_USER and galaxy running under uWSGI.
7157 """
72- conf = _get_conf ()
73- key = conf [ 'api_key ' ]
58+ history_id = history_id or os . environ [ 'HISTORY_ID' ]
59+ key = os . environ [ 'API_KEY ' ]
7460
7561 ### Customised/Raw galaxy_url ###
7662 galaxy_ip = _get_ip ()
7763 # Substitute $DOCKER_HOST with real IP
78- url = Template (conf [ 'galaxy_url ' ]).safe_substitute ({'DOCKER_HOST' : galaxy_ip })
79- gi = _test_url (url , key , history_id , use_objects = use_objects )
64+ url = Template (os . environ [ 'GALAXY_URL ' ]).safe_substitute ({'DOCKER_HOST' : galaxy_ip })
65+ gi = _test_url (url , key , history_id )
8066 if gi is not None :
8167 return gi
8268
8369 ### Failover, fully auto-detected URL ###
8470 # Remove trailing slashes
85- app_path = conf [ 'galaxy_url ' ].rstrip ('/' )
71+ app_path = os . environ [ 'GALAXY_URL ' ].rstrip ('/' )
8672 # Remove protocol+host:port if included
8773 app_path = '' .join (app_path .split ('/' )[3 :])
8874
89- if 'galaxy_paster_port ' not in conf :
75+ if 'GALAXY_WEB_PORT ' not in os . environ :
9076 # We've failed to detect a port in the config we were given by
9177 # galaxy, so we won't be able to construct a valid URL
9278 raise Exception ("No port" )
9379 else :
9480 # We should be able to find a port to connect to galaxy on via this
9581 # conf var: galaxy_paster_port
96- galaxy_port = conf [ 'galaxy_paster_port ' ]
82+ galaxy_port = os . environ [ 'GALAXY_WEB_PORT ' ]
9783
9884 built_galaxy_url = 'http://%s:%s/%s' % (galaxy_ip .strip (), galaxy_port , app_path .strip ())
9985 url = built_galaxy_url .rstrip ('/' )
10086
101- gi = _test_url (url , key , history_id , use_objects = use_objects )
87+ gi = _test_url (url , key , history_id )
10288 if gi is not None :
10389 return gi
10490
10591 ### Fail ###
106- raise Exception ("Could not connect to a galaxy instance. Please contact your SysAdmin for help with this error" )
107-
108-
109- def _get_history_id ():
110- """
111- Extract the history ID from the config file.
112- """
113- conf = _get_conf ()
114- return conf ['history_id' ]
92+ msg = "Could not connect to a galaxy instance. Please contact your SysAdmin for help with this error"
93+ raise Exception (msg )
11594
11695
117- def put (filename , file_type = 'auto' , history_id = None , use_objects = DEFAULT_USE_OBJECTS ):
96+ def put (filename , file_type = 'auto' , history_id = None ):
11897 """
11998 Given a filename of any file accessible to the docker instance, this
12099 function will upload that file to galaxy using the current history.
121100 Does not return anything.
122101 """
123- conf = _get_conf ()
124- gi = get_galaxy_connection (use_objects )
125- history_id = history_id or _get_history_id ()
126- if use_objects :
127- history = gi .histories .get ( history_id )
128- history .upload_dataset (filename , file_type = file_type )
129- else :
130- tc = ToolClient ( gi )
131- tc .upload_file (filename , history_id , file_type = file_type )
102+ gi = get_galaxy_connection (history_id = history_id )
103+ history_id = history_id or os .environ ['HISTORY_ID' ]
104+ history = gi .histories .get ( history_id )
105+ history .upload_dataset (filename , file_type = file_type )
132106
133107
134- def get (dataset_id , history_id = None , use_objects = DEFAULT_USE_OBJECTS ):
108+ def get (dataset_id , history_id = None ):
135109 """
136110 Given the history_id that is displayed to the user, this function will
137111 download the file from the history and stores it under /import/
138112 Return value is the path to the dataset stored under /import/
139113 """
140- conf = _get_conf ()
141- gi = get_galaxy_connection (use_objects )
114+ history_id = history_id or os .environ ['HISTORY_ID' ]
115+
116+ gi = get_galaxy_connection (history_id = history_id )
142117
143118 file_path = '/import/%s' % dataset_id
144- history_id = history_id or _get_history_id ()
145119
146120 # Cache the file requests. E.g. in the example of someone doing something
147121 # silly like a get() for a Galaxy file in a for-loop, wouldn't want to
148122 # re-download every time and add that overhead.
149123 if not os .path .exists (file_path ):
150- if use_objects :
151- history = gi .histories .get (history_id )
152- datasets = dict ([( d .wrapped ["hid" ], d .id ) for d in history .get_datasets ()])
153- dataset = history .get_dataset ( datasets [dataset_id ] )
154- dataset .download ( open (file_path , 'wb' ) )
155- else :
156- hc = HistoryClient (gi )
157- dc = DatasetClient (gi )
158- dataset_mapping = dict ([(dataset ['hid' ], dataset ['id' ]) for dataset in hc .show_history (history_id , contents = True )])
159- try :
160- hc .download_dataset (history_id , dataset_mapping [dataset_id ], file_path , use_default_filename = False , to_ext = None )
161- except :
162- dc .download_dataset (dataset_mapping [dataset_id ], file_path , use_default_filename = False )
124+ history = gi .histories .get (history_id )
125+ datasets = dict ([( d .wrapped ["hid" ], d .id ) for d in history .get_datasets ()])
126+ dataset = history .get_dataset ( datasets [dataset_id ] )
127+ dataset .download ( open (file_path , 'wb' ) )
163128
164129 return file_path
165130
@@ -175,6 +140,6 @@ def get(dataset_id, history_id=None, use_objects=DEFAULT_USE_OBJECTS):
175140
176141 if args .action == 'get' :
177142 # Ensure it's a numerical value
178- get (int (args .argument ))
143+ get (int (args .argument ), history_id = args . history_id )
179144 elif args .action == 'put' :
180- put (args .argument , file_type = args .filetype )
145+ put (args .argument , file_type = args .filetype , history_id = args . history_id )
0 commit comments