Created
October 13, 2015 16:09
-
-
Save peterjc/2cf9fa7e2dc921d27fcb to your computer and use it in GitHub Desktop.
Hack for syncing local .shed.yml files with a Galaxy Tool Shed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A hack, loosly based on Eric Rasche's disgusting.py | |
# https://gist.github.com/erasche/4ac3448b036f09979e14 | |
# | |
# Intended as a one-off use script to help with syncing local | |
# .shed.yml files with a Galaxy Tool Shed. See also: | |
# https://gist.github.com/peterjc/5ebbf446d799f3aaa639 | |
import yaml | |
import os | |
from bioblend import toolshed | |
shed_url = "https://toolshed.g2.bx.psu.edu/" | |
#owner = "peterjc" | |
#github_base = "https://github.com/peterjc/pico_galaxy/tools/" | |
#local_base = "/mnt/galaxy/repositories/pico_galaxy/tools/" | |
#owner = "devteam" | |
github_base = "https://github.com/peterjc/galaxy_blast/" | |
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 = "https://github.com/peterjc/pico_galaxy/tools/" | |
#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) | |
else: | |
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: | |
continue | |
good.append(x.strip()) | |
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)): | |
continue | |
shed = os.path.join(local_base, name, ".shed.yml") | |
print(shed) | |
info = tsi.repositories.show_repository(r["id"]) | |
if os.path.isfile(shed): | |
print("%s exists" % shed) | |
os.remove(shed) | |
else: | |
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