Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/planemo/galaxy/profiles.py @ 5:9b1c78e6ba9c draft default tip
"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
| author | shellac |
|---|---|
| date | Mon, 01 Jun 2020 08:59:25 -0400 |
| parents | 79f47841a781 |
| children |
comparison
equal
deleted
inserted
replaced
| 4:79f47841a781 | 5:9b1c78e6ba9c |
|---|---|
| 1 """This modules describes the abstraction of a Galaxy profile. | |
| 2 | |
| 3 This is a workspace with a specific default configuration and shed | |
| 4 tool setup. It is meant to be used with various serve commands. | |
| 5 """ | |
| 6 import json | |
| 7 import os | |
| 8 import shutil | |
| 9 | |
| 10 from galaxy.tool_util.deps.commands import which | |
| 11 | |
| 12 from planemo.config import ( | |
| 13 OptionSource, | |
| 14 ) | |
| 15 from planemo.database import create_database_source | |
| 16 from .config import DATABASE_LOCATION_TEMPLATE | |
| 17 | |
| 18 PROFILE_OPTIONS_JSON_NAME = "planemo_profile_options.json" | |
| 19 ALREADY_EXISTS_EXCEPTION = "Cannot create profile with name [%s], directory [%s] already exists." | |
| 20 | |
| 21 | |
| 22 def profile_exists(ctx, profile_name, **kwds): | |
| 23 """Return a truthy value iff the specified profile already exists.""" | |
| 24 profile_directory = _profile_directory(ctx, profile_name) | |
| 25 return os.path.exists(profile_directory) | |
| 26 | |
| 27 | |
| 28 def list_profiles(ctx, **kwds): | |
| 29 """Return a list of current profile names.""" | |
| 30 return os.listdir(ctx.galaxy_profiles_directory) | |
| 31 | |
| 32 | |
| 33 def delete_profile(ctx, profile_name, **kwds): | |
| 34 """Delete profile with the specified name.""" | |
| 35 profile_directory = _profile_directory(ctx, profile_name) | |
| 36 profile_options = _read_profile_options(profile_directory) | |
| 37 database_type = profile_options.get("database_type") | |
| 38 if database_type != "sqlite": | |
| 39 database_source = create_database_source(**kwds) | |
| 40 database_identifier = _profile_to_database_identifier(profile_name) | |
| 41 database_source.delete_database( | |
| 42 database_identifier, | |
| 43 ) | |
| 44 shutil.rmtree(profile_directory) | |
| 45 | |
| 46 | |
| 47 def create_profile(ctx, profile_name, **kwds): | |
| 48 """Create a profile with the specified name.""" | |
| 49 engine_type = kwds.get("engine", "galaxy") | |
| 50 profile_directory = _profile_directory(ctx, profile_name) | |
| 51 if profile_exists(ctx, profile_name, **kwds): | |
| 52 message = ALREADY_EXISTS_EXCEPTION % ( | |
| 53 profile_name, profile_directory | |
| 54 ) | |
| 55 raise Exception(message) | |
| 56 | |
| 57 os.makedirs(profile_directory) | |
| 58 create_for_engine = _create_profile_docker if engine_type == "docker_galaxy" else _create_profile_local | |
| 59 stored_profile_options = create_for_engine(ctx, profile_directory, profile_name, kwds) | |
| 60 | |
| 61 profile_options_path = _stored_profile_options_path(profile_directory) | |
| 62 with open(profile_options_path, "w") as f: | |
| 63 json.dump(stored_profile_options, f) | |
| 64 | |
| 65 | |
| 66 def _create_profile_docker(ctx, profile_directory, profile_name, kwds): | |
| 67 export_directory = os.path.join(profile_directory, "export") | |
| 68 os.makedirs(export_directory) | |
| 69 return { | |
| 70 "engine": "docker_galaxy", | |
| 71 } | |
| 72 | |
| 73 | |
| 74 def _create_profile_local(ctx, profile_directory, profile_name, kwds): | |
| 75 database_type = kwds.get("database_type", "auto") | |
| 76 if database_type == "auto": | |
| 77 if which("psql"): | |
| 78 database_type = "postgres" | |
| 79 elif which("docker"): | |
| 80 database_type = "postgres_docker" | |
| 81 else: | |
| 82 database_type = "sqlite" | |
| 83 | |
| 84 if database_type != "sqlite": | |
| 85 database_source = create_database_source(**kwds) | |
| 86 database_identifier = _profile_to_database_identifier(profile_name) | |
| 87 database_source.create_database( | |
| 88 database_identifier, | |
| 89 ) | |
| 90 database_connection = database_source.sqlalchemy_url(database_identifier) | |
| 91 else: | |
| 92 database_location = os.path.join(profile_directory, "galaxy.sqlite") | |
| 93 database_connection = DATABASE_LOCATION_TEMPLATE % database_location | |
| 94 | |
| 95 return { | |
| 96 "database_type": database_type, | |
| 97 "database_connection": database_connection, | |
| 98 "engine": "galaxy", | |
| 99 } | |
| 100 | |
| 101 | |
| 102 def ensure_profile(ctx, profile_name, **kwds): | |
| 103 """Ensure a Galaxy profile exists and return profile defaults.""" | |
| 104 if not profile_exists(ctx, profile_name, **kwds): | |
| 105 create_profile(ctx, profile_name, **kwds) | |
| 106 | |
| 107 return _profile_options(ctx, profile_name, **kwds) | |
| 108 | |
| 109 | |
| 110 def _profile_options(ctx, profile_name, **kwds): | |
| 111 profile_directory = _profile_directory(ctx, profile_name) | |
| 112 profile_options = _read_profile_options(profile_directory) | |
| 113 specified_engine_type = kwds.get("engine", "galaxy") | |
| 114 profile_engine_type = profile_options["engine"] | |
| 115 if specified_engine_type != profile_engine_type: | |
| 116 if ctx.get_option_source("engine") == OptionSource.cli: | |
| 117 raise Exception("Configured profile engine type [%s] does not match specified engine type [%s].") | |
| 118 | |
| 119 if profile_engine_type == "docker_galaxy": | |
| 120 engine_options = dict( | |
| 121 export_directory=os.path.join(profile_directory, "export") | |
| 122 ) | |
| 123 else: | |
| 124 file_path = os.path.join(profile_directory, "files") | |
| 125 shed_tool_path = os.path.join(profile_directory, "shed_tools") | |
| 126 shed_tool_conf = os.path.join(profile_directory, "shed_tool_conf.xml") | |
| 127 tool_dependency_dir = os.path.join(profile_directory, "deps") | |
| 128 | |
| 129 engine_options = dict( | |
| 130 file_path=file_path, | |
| 131 tool_dependency_dir=tool_dependency_dir, | |
| 132 shed_tool_conf=shed_tool_conf, | |
| 133 shed_tool_path=shed_tool_path, | |
| 134 galaxy_brand=profile_name, | |
| 135 ) | |
| 136 profile_options.update(engine_options) | |
| 137 profile_options["galaxy_brand"] = profile_name | |
| 138 return profile_options | |
| 139 | |
| 140 | |
| 141 def _profile_to_database_identifier(profile_name): | |
| 142 char_lst = [c if c.isalnum() else "_" for c in profile_name] | |
| 143 return "plnmoprof_%s" % "".join(char_lst) | |
| 144 | |
| 145 | |
| 146 def _read_profile_options(profile_directory): | |
| 147 profile_options_path = _stored_profile_options_path(profile_directory) | |
| 148 with open(profile_options_path, "r") as f: | |
| 149 profile_options = json.load(f) | |
| 150 return profile_options | |
| 151 | |
| 152 | |
| 153 def _stored_profile_options_path(profile_directory): | |
| 154 profile_options_path = os.path.join( | |
| 155 profile_directory, PROFILE_OPTIONS_JSON_NAME | |
| 156 ) | |
| 157 return profile_options_path | |
| 158 | |
| 159 | |
| 160 def _profile_directory(ctx, profile_name): | |
| 161 return os.path.join(ctx.galaxy_profiles_directory, profile_name) | |
| 162 | |
| 163 | |
| 164 __all__ = ( | |
| 165 "create_profile", | |
| 166 "delete_profile", | |
| 167 "ensure_profile", | |
| 168 "list_profiles", | |
| 169 ) |
