Skip to content

Commit d213446

Browse files
authored
Merge pull request #849 from jmchilton/galaxy_versions
Better support for different versions of Galaxy.
2 parents bb5d5fe + c05eabe commit d213446

4 files changed

Lines changed: 55 additions & 27 deletions

File tree

planemo/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import functools
33
import logging.config
44
import os
5+
import shutil
56
import sys
67
import traceback
78

89
import click
10+
from six.moves.urllib.request import urlopen
911

1012
from planemo import __version__
1113
from planemo.exit_codes import ExitCodeException
@@ -112,6 +114,21 @@ def exit(self, exit_code):
112114
self.vlog("Exiting planemo with exit code [%d]" % exit_code)
113115
raise ExitCodeException(exit_code)
114116

117+
def cache_download(self, url, destination):
118+
cache = os.path.join(self.workspace, "cache")
119+
if not os.path.exists(cache):
120+
os.makedirs(cache)
121+
filename = os.path.basename(url)
122+
cache_destination = os.path.join(cache, filename)
123+
if not os.path.exists(cache_destination):
124+
content = urlopen(url).read()
125+
if len(content) == 0:
126+
raise Exception("Failed to download [%s]." % url)
127+
with open(cache_destination, "wb") as f:
128+
f.write(content)
129+
130+
shutil.copy(cache_destination, destination)
131+
115132

116133
pass_context = click.make_pass_decorator(Context, ensure=True)
117134
cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),

planemo/galaxy/config.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
from galaxy.containers.docker_model import DockerVolume
1414
from galaxy.tools.deps import docker_util
1515
from galaxy.tools.deps.commands import argv_to_str
16-
from galaxy.util import unicodify
1716
from six import (
1817
add_metaclass,
1918
iteritems
2019
)
21-
from six.moves.urllib.request import urlopen
22-
from six.moves.urllib.request import urlretrieve
2320

2421
from planemo import git
2522
from planemo.config import OptionSource
@@ -189,7 +186,15 @@ class = StreamHandler
189186
DOWNLOADS_URL = ("https://raw.githubusercontent.com/"
190187
"jmchilton/galaxy-downloads/master/")
191188
DOWNLOADABLE_MIGRATION_VERSIONS = [141, 127, 120, 117]
192-
LATEST_URL = DOWNLOADS_URL + "latest.sqlite"
189+
MIGRATION_PER_VERSION = {
190+
"master": 141,
191+
"18.05": 141,
192+
"18.01": 140,
193+
"17.09": 135,
194+
"17.05": 134,
195+
"17.01": 133,
196+
}
197+
OLDEST_SUPPORTED_VERSION = 127
193198

194199
DATABASE_LOCATION_TEMPLATE = "sqlite:///%s?isolation_level=IMMEDIATE"
195200

@@ -345,14 +350,12 @@ def local_galaxy_config(ctx, runnables, for_tests=False, **kwds):
345350
def config_join(*args):
346351
return os.path.join(config_directory, *args)
347352

348-
latest_galaxy = False
349353
install_env = {
350354
'GALAXY_SKIP_CLIENT_BUILD': '1',
351355
}
352356
if install_galaxy:
353357
_build_eggs_cache(ctx, install_env, kwds)
354358
_install_galaxy(ctx, config_directory, install_env, kwds)
355-
latest_galaxy = True
356359
galaxy_root = config_join("galaxy-dev")
357360

358361
server_name = "planemo%d" % random.randint(0, 100000)
@@ -388,9 +391,9 @@ def config_join(*args):
388391
master_api_key = _get_master_api_key(kwds)
389392
dependency_dir = os.path.join(config_directory, "deps")
390393
preseeded_database = attempt_database_preseed(
394+
ctx,
391395
galaxy_root,
392396
database_location,
393-
latest_galaxy=latest_galaxy,
394397
**kwds
395398
)
396399
_ensure_directory(shed_tool_path)
@@ -982,7 +985,7 @@ def _database_connection(database_location, **kwds):
982985

983986

984987
def attempt_database_preseed(
985-
effective_galaxy_root, database_location, latest_galaxy=False, **kwds
988+
ctx, effective_galaxy_root, database_location, **kwds
986989
):
987990
"""If database location is unset, attempt to seed the database."""
988991
if os.path.exists(database_location):
@@ -995,12 +998,14 @@ def attempt_database_preseed(
995998

996999
preseeded_database = True
9971000
galaxy_sqlite_database = kwds.get("galaxy_database_seed", None)
1001+
galaxy_branch = kwds.get("galaxy_branch", None)
9981002
try:
9991003
_download_database_template(
1004+
ctx,
10001005
effective_galaxy_root,
10011006
database_location,
1002-
latest=latest_galaxy,
10031007
galaxy_sqlite_database=galaxy_sqlite_database,
1008+
galaxy_branch=galaxy_branch
10041009
)
10051010
except Exception as e:
10061011
print(e)
@@ -1010,40 +1015,43 @@ def attempt_database_preseed(
10101015

10111016

10121017
def _download_database_template(
1018+
ctx,
10131019
galaxy_root,
10141020
database_location,
1015-
latest=False,
1016-
galaxy_sqlite_database=None
1021+
galaxy_sqlite_database=None,
1022+
galaxy_branch=None,
10171023
):
1024+
10181025
if galaxy_sqlite_database is not None:
10191026
shutil.copyfile(galaxy_sqlite_database, database_location)
10201027
return True
10211028

1022-
if latest or not galaxy_root:
1023-
symlink_target = unicodify(urlopen(LATEST_URL).read())
1024-
template_url = DOWNLOADS_URL + symlink_target
1025-
urlretrieve(template_url, database_location)
1026-
return True
1027-
1028-
newest_migration = _newest_migration_version(galaxy_root)
10291029
download_migration = None
1030+
newest_migration = _newest_migration_version(galaxy_root, galaxy_branch)
10301031
for migration in DOWNLOADABLE_MIGRATION_VERSIONS:
1031-
if newest_migration > migration:
1032+
if newest_migration >= migration:
10321033
download_migration = migration
10331034
break
10341035

10351036
if download_migration:
10361037
download_name = "db_gx_rev_0%d.sqlite" % download_migration
10371038
download_url = DOWNLOADS_URL + download_name
1038-
urlretrieve(download_url, database_location)
1039+
ctx.cache_download(download_url, database_location)
10391040
return True
10401041
else:
10411042
return False
10421043

10431044

1044-
def _newest_migration_version(galaxy_root):
1045-
versions = os.path.join(galaxy_root, "lib/galaxy/model/migrate/versions")
1046-
version = max(map(_file_name_to_migration_version, os.listdir(versions)))
1045+
def _newest_migration_version(galaxy_root, galaxy_branch):
1046+
versions_dir = galaxy_root and os.path.join(galaxy_root, "lib/galaxy/model/migrate/versions")
1047+
if versions_dir and os.path.exists(versions_dir):
1048+
version = max(map(_file_name_to_migration_version, os.listdir(versions_dir)))
1049+
elif galaxy_branch:
1050+
for branch in MIGRATION_PER_VERSION:
1051+
if branch in galaxy_branch:
1052+
version = MIGRATION_PER_VERSION[branch]
1053+
else:
1054+
version = OLDEST_SUPPORTED_VERSION
10471055
return version
10481056

10491057

planemo/galaxy/profiles.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,22 @@ def create_profile(ctx, profile_name, **kwds):
5959

6060
os.makedirs(profile_directory)
6161
create_for_engine = _create_profile_docker if engine_type == "docker_galaxy" else _create_profile_local
62-
stored_profile_options = create_for_engine(profile_directory, profile_name, kwds)
62+
stored_profile_options = create_for_engine(ctx, profile_directory, profile_name, kwds)
6363

6464
profile_options_path = _stored_profile_options_path(profile_directory)
6565
with open(profile_options_path, "w") as f:
6666
json.dump(stored_profile_options, f)
6767

6868

69-
def _create_profile_docker(profile_directory, profile_name, kwds):
69+
def _create_profile_docker(ctx, profile_directory, profile_name, kwds):
7070
export_directory = os.path.join(profile_directory, "export")
7171
os.makedirs(export_directory)
7272
return {
7373
"engine": "docker_galaxy",
7474
}
7575

7676

77-
def _create_profile_local(profile_directory, profile_name, kwds):
77+
def _create_profile_local(ctx, profile_directory, profile_name, kwds):
7878
database_type = kwds.get("database_type", "auto")
7979
if database_type == "auto":
8080
if which("psql"):
@@ -93,7 +93,7 @@ def _create_profile_local(profile_directory, profile_name, kwds):
9393
database_connection = database_source.sqlalchemy_url(database_identifier)
9494
else:
9595
database_location = os.path.join(profile_directory, "galaxy.sqlite")
96-
attempt_database_preseed(None, database_location, **kwds)
96+
attempt_database_preseed(ctx, None, database_location, **kwds)
9797
database_connection = DATABASE_LOCATION_TEMPLATE % database_location
9898

9999
return {

planemo/galaxy/run.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ def setup_venv(ctx, kwds):
5353
def locate_galaxy_virtualenv(ctx, kwds):
5454
if not kwds.get("no_cache_galaxy", False):
5555
workspace = ctx.workspace
56+
galaxy_branch = kwds.get("galaxy_branch", "master")
5657
shared_venv_path = os.path.join(workspace, "gx_venv")
58+
if galaxy_branch != "master":
59+
shared_venv_path = "%s_%s" % (shared_venv_path, galaxy_branch)
5760
venv_command = CACHED_VIRTUAL_ENV_COMMAND % shared_venv_path
5861
else:
5962
venv_command = UNCACHED_VIRTUAL_ENV_COMMAND

0 commit comments

Comments
 (0)