Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Hack for updating Galaxy tool_dependencies.xml files with URLs
# Copyright 2016 Peter Cock, James Hutton Institute.
# All Rights Reserved.
# Released as open source under the MIT license.
"""Update URLs in Galaxy tool_dependencies.xml files
Assumes have a copy of the urls.tsv file from
This file defines the
URL naming used for caching Galaxy dependencies.
The script will check or insert the SHA256 checksum, and will
preserve the download filename (since the depot renames things).
Sample usage:
$ python ../galaxy_blast/packages/*/tool_dependencies.xml
WARNING: Edits files in place!
import os
import sys
def load_urls(filename):
mapping = dict()
for line in open(filename):
if line.startswith("#"):
parts = line.rstrip("\n").split("\t")
id, version, platform, arch, old_url, ext, sha256sum, _ = line.rstrip("\n").split("\t", 7)
if ext:
assert ext.startswith("."), line
new_url = "" % (id, id, version, platform, arch, ext)
mapping[old_url] = new_url, sha256sum
return mapping
def update_tool_dependency(filename, mapping):
print("Updating %s" % filename)
lines = list(open(filename))
with open(filename, "w") as handle:
for line in lines:
if "<action " in line and "download" in line:
indent = " " * line.index("<action ")
assert not indent.strip(), line
for old, (new, checksum) in mapping.items():
if old in line:
base = os.path.basename(old)
# TODO - escape URLs, e.g. ampersand
assert '>%s</action>' % old in line
if 'target_filename=' not in line and not new.endswith("/" + base):
# Preserve the original filename
line = line.replace('>%s</action>' % old,
' target_filename="%s">%s</action>' % (base, old))
if 'sha256sum=' in line:
# Check checksum matches
assert 'sha256sum="%s"' % checksum in line
# Record the checksum
line = line.replace('>%s</action>' % old,
' sha256sum="%s">%s</action>' % (checksum, old))
# Finally, replace the URL
line = line.replace('>%s</action>' % old,
'>%s</action>' % new)
print("Updated %s --> %s" % (old, new))
handle.write("%s<!-- Original URL %s -->\n" % (indent, old))
mapping = load_urls("urls.tsv")
for filename in sys.argv[1:]:
update_tool_dependency(filename, mapping)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment