|
3 | 3 | This is a workspace with a specific default configuration and shed |
4 | 4 | tool setup. It is meant to be used with various serve commands. |
5 | 5 | """ |
| 6 | +import json |
6 | 7 | import os |
| 8 | +import shutil |
7 | 9 |
|
8 | 10 | from .config import ( |
9 | 11 | DATABASE_LOCATION_TEMPLATE, |
10 | 12 | attempt_database_preseed, |
11 | 13 | ) |
| 14 | +from planemo.database import create_database_source |
12 | 15 |
|
| 16 | +PROFILE_OPTIONS_JSON_NAME = "planemo_profile_options.json" |
| 17 | +ALREADY_EXISTS_EXCEPTION = "Cannot create profile with name [%s], directory [%s] already exists." |
13 | 18 |
|
14 | | -def ensure_profile(ctx, profile_name, **kwds): |
15 | | - """Ensure a Galaxy profile exists and return profile defaults.""" |
| 19 | + |
| 20 | +def profile_exists(ctx, profile_name, **kwds): |
| 21 | + """Return a truthy value iff the specified profile already exists.""" |
16 | 22 | profile_directory = _profile_directory(ctx, profile_name) |
17 | | - database_location = os.path.join(profile_directory, "galaxy.sqlite") |
| 23 | + return os.path.exists(profile_directory) |
| 24 | + |
18 | 25 |
|
19 | | - if not os.path.exists(profile_directory): |
20 | | - os.makedirs(profile_directory) |
| 26 | +def list_profiles(ctx, **kwds): |
| 27 | + return os.listdir(ctx.galaxy_profiles_directory) |
| 28 | + |
| 29 | + |
| 30 | +def delete_profile(ctx, profile_name, **kwds): |
| 31 | + """Delete profile with the specified name.""" |
| 32 | + profile_directory = _profile_directory(ctx, profile_name) |
| 33 | + profile_options = _read_profile_options(profile_directory) |
| 34 | + database_type = profile_options.get("database_type") |
| 35 | + if database_type != "sqlite": |
| 36 | + database_source = create_database_source(**kwds) |
| 37 | + database_identifier = _profile_to_database_identifier(profile_name) |
| 38 | + database_source.delete_database( |
| 39 | + database_identifier, |
| 40 | + ) |
| 41 | + shutil.rmtree(profile_directory) |
| 42 | + |
| 43 | + |
| 44 | +def create_profile(ctx, profile_name, **kwds): |
| 45 | + """Create a profile with the specified name.""" |
| 46 | + profile_directory = _profile_directory(ctx, profile_name) |
| 47 | + if profile_exists(ctx, profile_name, **kwds): |
| 48 | + message = ALREADY_EXISTS_EXCEPTION % ( |
| 49 | + profile_name, profile_directory |
| 50 | + ) |
| 51 | + raise Exception(message) |
| 52 | + |
| 53 | + os.makedirs(profile_directory) |
| 54 | + database_type = kwds.get("database_type", "sqlite") |
| 55 | + if database_type != "sqlite": |
| 56 | + database_source = create_database_source(**kwds) |
| 57 | + database_identifier = _profile_to_database_identifier(profile_name) |
| 58 | + database_source.create_database( |
| 59 | + database_identifier, |
| 60 | + ) |
| 61 | + database_connection = database_source.sqlalchemy_url(database_identifier) |
| 62 | + else: |
| 63 | + database_location = os.path.join(profile_directory, "galaxy.sqlite") |
21 | 64 | attempt_database_preseed(None, database_location, **kwds) |
| 65 | + database_connection = DATABASE_LOCATION_TEMPLATE % database_location |
| 66 | + |
| 67 | + stored_profile_options = { |
| 68 | + 'database_type': database_type, |
| 69 | + 'database_connection': database_connection, |
| 70 | + } |
| 71 | + profile_options_path = _stored_profile_options_path(profile_directory) |
| 72 | + with open(profile_options_path, "w") as f: |
| 73 | + json.dump(stored_profile_options, f) |
| 74 | + |
22 | 75 |
|
23 | | - database_connection = DATABASE_LOCATION_TEMPLATE % database_location |
| 76 | +def ensure_profile(ctx, profile_name, **kwds): |
| 77 | + """Ensure a Galaxy profile exists and return profile defaults.""" |
| 78 | + if not profile_exists(ctx, profile_name, **kwds): |
| 79 | + create_profile(ctx, profile_name, **kwds) |
| 80 | + |
| 81 | + return _profile_options(ctx, profile_name, **kwds) |
| 82 | + |
| 83 | + |
| 84 | +def _profile_options(ctx, profile_name, **kwds): |
| 85 | + profile_directory = _profile_directory(ctx, profile_name) |
24 | 86 | file_path = os.path.join(profile_directory, "files") |
25 | 87 | shed_tool_path = os.path.join(profile_directory, "shed_tools") |
26 | 88 | shed_tool_conf = os.path.join(profile_directory, "shed_tool_conf.xml") |
27 | 89 | tool_dependency_dir = os.path.join(profile_directory, "deps") |
28 | 90 |
|
29 | | - return dict( |
| 91 | + profile_options = _read_profile_options(profile_directory) |
| 92 | + profile_options.update(dict( |
30 | 93 | file_path=file_path, |
31 | | - database_connection=database_connection, |
32 | 94 | tool_dependency_dir=tool_dependency_dir, |
33 | 95 | shed_tool_conf=shed_tool_conf, |
34 | 96 | shed_tool_path=shed_tool_path, |
| 97 | + )) |
| 98 | + |
| 99 | + return profile_options |
| 100 | + |
| 101 | + |
| 102 | +def _profile_to_database_identifier(profile_name): |
| 103 | + char_lst = [c if c.isalnum() else "_" for c in profile_name] |
| 104 | + return "plnmoprof_%s" % "".join(char_lst) |
| 105 | + |
| 106 | + |
| 107 | +def _read_profile_options(profile_directory): |
| 108 | + profile_options_path = _stored_profile_options_path(profile_directory) |
| 109 | + with open(profile_options_path, "r") as f: |
| 110 | + profile_options = json.load(f) |
| 111 | + return profile_options |
| 112 | + |
| 113 | + |
| 114 | +def _stored_profile_options_path(profile_directory): |
| 115 | + profile_options_path = os.path.join( |
| 116 | + profile_directory, PROFILE_OPTIONS_JSON_NAME |
35 | 117 | ) |
| 118 | + return profile_options_path |
36 | 119 |
|
37 | 120 |
|
38 | 121 | def _profile_directory(ctx, profile_name): |
39 | 122 | return os.path.join(ctx.galaxy_profiles_directory, profile_name) |
40 | 123 |
|
41 | 124 | __all__ = [ |
42 | 125 | "ensure_profile", |
| 126 | + "create_profile", |
| 127 | + "delete_profile", |
43 | 128 | ] |
0 commit comments