<s> import os <EOL> from string import Template <EOL> import yaml <EOL> from cloudbio . custom . bio_general import * <EOL> from cloudbio . custom . bio_nextgen import * <EOL> from cloudbio . custom . bio_proteomics import * <EOL> from cloudbio . custom . shared import _set_default_config , _add_to_profiles <EOL> from cloudbio . galaxy . applications import * <EOL> from cloudbio . galaxy . r import _install_r_packages <EOL> from cloudbio . galaxy . utils import _chown_galaxy , _read_boolean <EOL> FAILED_INSTALL_MESSAGE = "<STR_LIT>" <EOL> def _install_tools ( env , tools_conf = None ) : <EOL> """<STR_LIT>""" <EOL> if not tools_conf : <EOL> tools_conf = _load_tools_conf ( env ) <EOL> if _read_boolean ( env , "<STR_LIT>" , False ) : <EOL> _setup_install_dir ( env ) <EOL> _install_configured_applications ( env , tools_conf ) <EOL> _chown_galaxy ( env , env . galaxy_tools_dir ) <EOL> _chown_galaxy ( env , env . galaxy_jars_dir ) <EOL> if _read_boolean ( env , "<STR_LIT>" , False ) : <EOL> _install_r_packages ( tools_conf ) <EOL> def _tools_conf_path ( env ) : <EOL> """<STR_LIT>""" <EOL> contrib_dir = os . path . join ( env . config_dir , os . pardir , "<STR_LIT>" ) <EOL> default_tools_conf_path = os . path . join ( contrib_dir , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> tools_conf_path = env . get ( "<STR_LIT>" , default_tools_conf_path ) <EOL> return Template ( tools_conf_path ) . safe_substitute ( { "<STR_LIT>" : contrib_dir } ) <EOL> def _load_tools_conf ( env ) : <EOL> with open ( _tools_conf_path ( env ) ) as in_handle : <EOL> full_data = yaml . load ( in_handle ) <EOL> return full_data <EOL> def _setup_install_dir ( env ) : <EOL> """<STR_LIT>""" <EOL> if not env . safe_exists ( env . galaxy_tools_dir ) : <EOL> env . safe_sudo ( "<STR_LIT>" % env . galaxy_tools_dir ) <EOL> _chown_galaxy ( env , env . galaxy_tools_dir ) <EOL> bin_dir = os . path . join ( env . galaxy_tools_dir , '<STR_LIT>' ) <EOL> if not env . safe_exists ( bin_dir ) : <EOL> env . safe_sudo ( "<STR_LIT>" % bin_dir ) <EOL> _chown_galaxy ( env , bin_dir ) <EOL> line = "<STR_LIT>" . format ( bin_dir ) <EOL> _add_to_profiles ( line ) <EOL> if not env . safe_exists ( env . galaxy_jars_dir ) : <EOL> env . safe_sudo ( "<STR_LIT>" % env . galaxy_jars_dir ) <EOL> _chown_galaxy ( env , env . galaxy_jars_dir ) <EOL> def _install_configured_applications ( env , tools_conf ) : <EOL> """<STR_LIT>""" <EOL> applications = tools_conf [ "<STR_LIT>" ] or { } <EOL> defer_errors = env . get ( "<STR_LIT>" , True ) <EOL> exceptions = { } <EOL> for ( name , tool_conf ) in applications . iteritems ( ) : <EOL> if not __check_conditional ( tool_conf ) : <EOL> continue <EOL> try : <EOL> _install_application ( name , tool_conf ) <EOL> except BaseException , e : <EOL> exceptions [ name ] = e <EOL> if not defer_errors : <EOL> break <EOL> if exceptions : <EOL> for name , exception in exceptions . iteritems ( ) : <EOL> env . logger . warn ( FAILED_INSTALL_MESSAGE % name ) <EOL> first_exception = exceptions . values ( ) [ <NUM_LIT:0> ] <EOL> raise first_exception <EOL> def _install_application ( name , versions , tool_install_dir = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( versions ) is str : <EOL> versions = [ versions ] <EOL> for version_info in versions : <EOL> if type ( version_info ) is str : <EOL> _install_tool ( env , name , version = version_info , requirement_name = name , tool_install_dir = tool_install_dir ) <EOL> else : <EOL> version = version_info [ "<STR_LIT:version>" ] <EOL> bin_dirs = version_info . get ( "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> env_vars = version_info . get ( "<STR_LIT>" , { } ) <EOL> provides = version_info . get ( "<STR_LIT>" , [ ] ) <EOL> if isinstance ( provides , ( str , unicode , basestring ) ) : <EOL> provides = [ provides ] <EOL> for provide_conf in provides [ : ] : <EOL> if isinstance ( provide_conf , dict ) : <EOL> provides . remove ( provide_conf ) <EOL> if __check_conditional ( provide_conf ) : <EOL> provies . append ( provide_conf [ "<STR_LIT:name>" ] ) <EOL> requirement_name = version_info . get ( "<STR_LIT>" , name ) <EOL> tool_env = _install_tool ( env , name , version , bin_dirs = bin_dirs , env_vars = env_vars , requirement_name = requirement_name , tool_install_dir = tool_install_dir ) <EOL> symlink_versions = version_info . get ( "<STR_LIT>" , [ ] ) <EOL> if type ( symlink_versions ) is str : <EOL> symlink_versions = [ symlink_versions ] <EOL> for symlink_version in symlink_versions : <EOL> _set_default_config ( tool_env , tool_env [ "<STR_LIT>" ] , symlink_version ) <EOL> if provides : <EOL> install_dir = tool_env [ "<STR_LIT>" ] <EOL> tool_dir = "<STR_LIT>" % install_dir <EOL> tools_dir = "<STR_LIT>" % tool_dir <EOL> for package in provides : <EOL> link_dir = "<STR_LIT>" % ( tools_dir , package ) <EOL> env . safe_sudo ( "<STR_LIT>" % ( requirement_name , link_dir ) ) <EOL> def _install_tool ( env , name , version , requirement_name , bin_dirs = [ "<STR_LIT>" ] , env_vars = { } , tool_install_dir = None ) : <EOL> tool_env = _build_tool_env ( env , requirement_name , version , tool_install_dir ) <EOL> env . logger . debug ( "<STR_LIT>" % name ) <EOL> eval ( "<STR_LIT>" % name ) ( tool_env ) <EOL> _install_galaxy_config ( tool_env , bin_dirs , env_vars = env_vars ) <EOL> return tool_env <EOL> def _build_tool_env ( env , name , version , tool_install_dir ) : <EOL> """<STR_LIT>""" <EOL> tool_env = { "<STR_LIT>" : version , <EOL> "<STR_LIT>" : True } <EOL> for key , value in env . iteritems ( ) : <EOL> tool_env [ key ] = value <EOL> if not tool_install_dir : <EOL> tool_install_dir = os . path . join ( env . galaxy_tools_dir , name , version ) <EOL> tool_env [ "<STR_LIT>" ] = tool_install_dir <EOL> tool_env [ "<STR_LIT>" ] = tool_install_dir <EOL> tool_env [ "<STR_LIT>" ] = "<STR_LIT>" % ( tool_env [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> return AttributeDict ( tool_env ) <EOL> def __check_conditional ( conf_dict ) : <EOL> passes = True <EOL> try : <EOL> if "<STR_LIT>" in conf_dict : <EOL> value = conf_dict [ "<STR_LIT>" ] <EOL> passes = _read_boolean ( env , value , False ) <EOL> elif "<STR_LIT>" in conf_dict : <EOL> value = conf_dict [ "<STR_LIT>" ] <EOL> passes = not _read_boolean ( env , value , False ) <EOL> except TypeError : <EOL> pass <EOL> return passes <EOL> class AttributeDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> __getattr__ = dict . __getitem__ <EOL> __setattr__ = dict . __setitem__ <EOL> def _install_galaxy_config ( tool_env , bin_dirs , env_vars ) : <EOL> """<STR_LIT>""" <EOL> install_dir = tool_env [ "<STR_LIT>" ] <EOL> env_path = os . path . join ( install_dir , "<STR_LIT>" ) <EOL> bin_paths = [ os . path . join ( install_dir , bin_dir ) for bin_dir in bin_dirs ] <EOL> path_pieces = [ bin_path for bin_path in bin_paths if env . safe_exists ( bin_path ) ] <EOL> if len ( path_pieces ) > <NUM_LIT:0> and not env . safe_exists ( env_path ) : <EOL> path_addtion = "<STR_LIT::>" . join ( path_pieces ) <EOL> env . safe_sudo ( "<STR_LIT>" % ( path_addtion , env_path ) ) <EOL> venv_path = "<STR_LIT>" % ( install_dir , "<STR_LIT>" ) <EOL> if env . safe_exists ( venv_path ) : <EOL> env . safe_sudo ( "<STR_LIT>" % ( venv_path , env_path ) ) <EOL> env . safe_sudo ( "<STR_LIT>" % env_path ) <EOL> for env_var , env_var_value in env_vars . iteritems ( ) : <EOL> env_var_template = Template ( env_var_value ) <EOL> expanded_env_var_value = env_var_template . substitute ( tool_env ) <EOL> env . safe_sudo ( "<STR_LIT>" % ( env_var , expanded_env_var_value , env_path ) ) <EOL> env . logger . debug ( "<STR_LIT>" % env_path ) <EOL> _set_default_config ( tool_env , install_dir ) <EOL> if _read_boolean ( tool_env , "<STR_LIT>" , False ) and env . safe_exists ( env_path ) : <EOL> _add_to_profiles ( "<STR_LIT>" % env_path , profiles = [ "<STR_LIT>" ] ) </s>
