Created
August 18, 2016 14:56
-
-
Save peterjc/27c84e32c83012e1d83681ca4a767cbf to your computer and use it in GitHub Desktop.
Hack for updating Galaxy tool_dependencies.xml files with depot.galaxyproject.org URLs
This file contains hidden or 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
# 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 | |
https://github.com/galaxyproject/cargo-port/blob/master/urls.tsv | |
This file defines the https://depot.galaxyproject.org/software/ | |
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 update_tool_dependencies.py ../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("#"): | |
continue | |
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 = "https://depot.galaxyproject.org/software/%s/%s_%s_%s_%s%s" % (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 | |
else: | |
# 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)) | |
handle.write(line) | |
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