<s> from bioblend . galaxy import GalaxyInstance <EOL> from tempfile import NamedTemporaryFile <EOL> from bioblend . galaxyclient import ConnectionError <EOL> from os . path import basename <EOL> import argparse <EOL> import datetime <EOL> import time <EOL> import ftplib <EOL> import pysftp <EOL> import urlparse <EOL> import urllib2 <EOL> import requests <EOL> import json <EOL> def parse_args ( ) : <EOL> args = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , "<STR_LIT>" , required = True , <EOL> help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT:http>" , choices = [ '<STR_LIT:http>' , '<STR_LIT>' ] , <EOL> help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , "<STR_LIT>" , required = True , <EOL> help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , choices = [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT> , type = int , help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , type = str , help = "<STR_LIT>" ) <EOL> args . add_argument ( "<STR_LIT>" , type = str , help = "<STR_LIT>" ) <EOL> return args . parse_args ( ) <EOL> def inject_auth ( f , http_auth_username , http_auth_password ) : <EOL> def auth ( * args , ** kwargs ) : <EOL> kwargs [ "<STR_LIT>" ] = ( http_auth_username , http_auth_password ) <EOL> return f ( * args , ** kwargs ) <EOL> return auth <EOL> def make_post_request ( self , url , payload , params = None , files_attached = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if params is not None and params . get ( '<STR_LIT:key>' , False ) is False : <EOL> params [ '<STR_LIT:key>' ] = self . key <EOL> else : <EOL> params = self . default_params <EOL> if files_attached : <EOL> payload . update ( params ) <EOL> payload = MultipartEncoder ( fields = payload ) <EOL> headers = self . json_headers . copy ( ) <EOL> headers [ '<STR_LIT:Content-Type>' ] = payload . content_type <EOL> post_params = { } <EOL> else : <EOL> payload = json . dumps ( payload ) <EOL> headers = self . json_headers <EOL> post_params = params <EOL> r = requests . post ( url , data = payload , headers = headers , <EOL> verify = self . verify , params = post_params , ** kwargs ) <EOL> if r . status_code == <NUM_LIT:200> : <EOL> return r . json ( ) <EOL> raise ConnectionError ( "<STR_LIT>" % <EOL> r . status_code , body = r . text ) <EOL> def make_delete_request ( self , url , payload = None , params = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if params is not None and params . get ( '<STR_LIT:key>' , False ) is False : <EOL> params [ '<STR_LIT:key>' ] = self . key <EOL> else : <EOL> params = self . default_params <EOL> r = requests . delete ( url , verify = self . verify , data = payload , params = params , ** kwargs ) <EOL> return r <EOL> def make_put_request ( self , url , payload = None , params = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if params is not None and params . get ( '<STR_LIT:key>' , False ) is False : <EOL> params [ '<STR_LIT:key>' ] = self . key <EOL> else : <EOL> params = self . default_params <EOL> payload = json . dumps ( payload ) <EOL> r = requests . put ( url , verify = self . verify , data = payload , params = params , ** kwargs ) <EOL> return r <EOL> def create_user ( url , api_key ) : <EOL> """<STR_LIT>""" <EOL> gi = GalaxyInstance ( url , api_key ) <EOL> date = str ( datetime . datetime . now ( ) . date ( ) ) <EOL> username = "<STR_LIT>" . format ( date = date ) <EOL> user_email = username + "<STR_LIT>" <EOL> password = user_email <EOL> userlist = gi . users . get_users ( ) <EOL> if not user_exists ( username , userlist ) : <EOL> gi . users . create_local_user ( username , user_email , password ) <EOL> userlist = gi . users . get_users ( ) <EOL> api_key = create_user_api_key ( gi , username , userlist ) <EOL> return api_key , user_email , password <EOL> def user_exists ( username , userlist ) : <EOL> return sum ( [ entry [ "<STR_LIT:username>" ] == username for entry in userlist ] ) == <NUM_LIT:1> <EOL> def create_user_api_key ( gi , username , userlist ) : <EOL> userid = [ user [ '<STR_LIT:id>' ] for user in userlist if user [ '<STR_LIT:username>' ] == username ] [ <NUM_LIT:0> ] <EOL> return gi . users . create_user_apikey ( userid ) <EOL> def ftp_upload_file ( fqdn , user_email , password , port , tmpfile ) : <EOL> """<STR_LIT>""" <EOL> session = ftplib . FTP ( ) <EOL> session . connect ( fqdn , port ) <EOL> session . login ( user_email , password ) <EOL> tmpfile_basename = basename ( tmpfile ) <EOL> with open ( tmpfile ) as file : <EOL> session . storbinary ( '<STR_LIT>' . format ( fn = tmpfile_basename ) , file ) <EOL> session . quit ( ) <EOL> def sftp_upload_file ( fqdn , user_email , password , port , tmpfile ) : <EOL> """<STR_LIT>""" <EOL> with pysftp . Connection ( fqdn , username = user_email , password = password , port = port ) as sftp : <EOL> sftp . put ( tmpfile ) <EOL> def successfull_upload ( url , new_api_key , tmpfile ) : <EOL> """<STR_LIT>""" <EOL> gi = GalaxyInstance ( url , new_api_key ) <EOL> files = [ True for ftp_file in gi . ftpfiles . get_ftp_files ( ) if str ( ftp_file [ "<STR_LIT:path>" ] ) == basename ( tmpfile ) ] <EOL> if files : <EOL> return True <EOL> else : <EOL> return False <EOL> def main ( ) : <EOL> args = parse_args ( ) <EOL> web_protocol , fqdn , ftp_protocol , port , api_key = args . protocol , args . fqdn , args . ftp_protocol , args . port , args . api_key <EOL> if args . http_auth_username : <EOL> GalaxyInstance . make_delete_request = inject_auth ( make_delete_request , args . http_auth_username , args . http_auth_password ) <EOL> GalaxyInstance . make_post_request = inject_auth ( make_post_request , args . http_auth_username , args . http_auth_password ) <EOL> GalaxyInstance . make_put_request = inject_auth ( make_put_request , args . http_auth_username , args . http_auth_password ) <EOL> GalaxyInstance . make_get_request = inject_auth ( GalaxyInstance . make_get_request , args . http_auth_username , args . http_auth_password ) <EOL> url = web_protocol + "<STR_LIT>" + fqdn <EOL> new_api_key , user_email , password = create_user ( url , api_key ) <EOL> tmpfile = NamedTemporaryFile ( ) <EOL> tmpfile . write ( "<STR_LIT>" ) <EOL> if ftp_protocol == "<STR_LIT>" : <EOL> ftp_upload_file ( fqdn , user_email , password , port , tmpfile . name ) <EOL> else : <EOL> sftp_upload_file ( fqdn , user_email , password , port , tmpfile . name ) <EOL> if not successfull_upload ( url , new_api_key , tmpfile . name ) : <EOL> sys . exit ( "<STR_LIT>" . format ( ftp_protocol = ftp_protocol , fqdn = fqdn ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
