Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Hack for syncing local .shed.yml files with a Galaxy Tool Shed
# A hack, loosly based on Eric Rasche's
# Intended as a one-off use script to help with syncing local
# .shed.yml files with a Galaxy Tool Shed. See also:
import yaml
import os
from bioblend import toolshed
shed_url = ""
#owner = "peterjc"
#github_base = ""
#local_base = "/mnt/galaxy/repositories/pico_galaxy/tools/"
#owner = "devteam"
github_base = ""
local_base = "/mnt/galaxy/repositories/galaxy_blast/"
#owner = "iuc"
#repo_type = "dependencies"
#owner = "peterjc"
#repo_type = "workflows"
owner = "peterjc"
repo_type = "tools"
github_base += repo_type + "/"
local_base += repo_type + "/"
assert os.path.isdir(local_base), local_base
tsi = toolshed.ToolShedInstance(shed_url)
repos = tsi.repositories.get_repositories()
repos = [r for r in repos if r["owner"]==owner]
cats = tsi.repositories.get_categories()
#github_base = ""
#local_base = "/mnt/galaxy/repositories/pico_galaxy/tools/"
keys = ("name", "owner",
"homepage_url", "remote_repository_url",
"description", "long_description")
# Ripped from planemo
def shed_repo_config(path):
shed_yaml_path = os.path.join(path, ".shed.yml")
if os.path.exists(shed_yaml_path):
with open(shed_yaml_path, "r") as f:
return yaml.load(f)
return {}
def serialize_shed_repo_config(shed_yaml_path, data):
with open(shed_yaml_path, "w") as f:
for k in keys:
if k in data:
v = data[k]
if "\n" in v:
v = yaml.dump(v).rstrip()
# Strip redundant quote chars...
if v.count("'") == 2 and v[0] == "'" and v[-1] == "'":
v = v[1:-1]
assert "..." not in v, data[k]
f.write("%s: %s\n" % (k, v))
#f.write(yaml.dump(data, default_flow_style=False))
def sync_changes(name, shed_path, data):
conf = shed_repo_config(shed_path)
for k in ("name", "owner", "type",
"homepage_url", "remote_repository_url",
"description", "long_description"):
if k in data and data[k] is not None:
conf[k] = str(data[k]) # anti-unicode hack
if 'long_description' in conf:
ld = conf['long_description'].rstrip().split('\n')
good = []
for x in ld:
if 'Repository-' in x:
lds = ''
for x in good:
if x != '':
lds += x.strip() + '\n'
conf['long_description'] = lds.strip()
if 'homepage_url' not in conf:
conf['homepage_url'] = github_base + name
if 'remote_repository_url' not in conf:
conf['remote_repository_url'] = github_base + name
serialize_shed_repo_config(shed_path, conf)
for r in repos:
name = r["name"]
if not os.path.isdir(os.path.join(local_base, name)):
shed = os.path.join(local_base, name, ".shed.yml")
info = tsi.repositories.show_repository(r["id"])
if os.path.isfile(shed):
print("%s exists" % shed)
print("%s missing" % shed)
sync_changes(name, shed, info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment