|
| 1 | +# Script originally developed as part of the usegalaxy-tools project. |
| 2 | +# https://github.com/galaxyproject/usegalaxy-tools/blob/master/scripts/update-tool.py |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import yaml |
| 7 | +from collections import defaultdict |
| 8 | + |
| 9 | +from bioblend import toolshed |
| 10 | + |
| 11 | +DEFAULT_TOOL_SHED_URL = 'https://toolshed.g2.bx.psu.edu' |
| 12 | + |
| 13 | + |
| 14 | +class ToolSheds(defaultdict): |
| 15 | + default_factory = toolshed.ToolShedInstance |
| 16 | + |
| 17 | + def __missing__(self, key): |
| 18 | + logging.debug('Creating new ToolShedInstance for URL: %s', key) |
| 19 | + return self.default_factory(url=key) |
| 20 | + |
| 21 | + |
| 22 | +tool_sheds = ToolSheds() |
| 23 | + |
| 24 | + |
| 25 | +def update_file(fn, owner=None, name=None, without=False): |
| 26 | + locked_in_path = fn + ".lock" |
| 27 | + if not os.path.exists(locked_in_path): |
| 28 | + logging.info("Lockfile doesn't exist yet, starting with source as base.") |
| 29 | + locked_in_path = fn |
| 30 | + |
| 31 | + with open(locked_in_path, 'r') as handle: |
| 32 | + locked = yaml.safe_load(handle) |
| 33 | + |
| 34 | + # Update any locked tools. |
| 35 | + for tool in locked['tools']: |
| 36 | + # If without, then if it is lacking, we should exec. |
| 37 | + logging.debug("Examining {owner}/{name}".format(**tool)) |
| 38 | + |
| 39 | + if without: |
| 40 | + if 'revisions' in tool and not len(tool.get('revisions', [])) == 0: |
| 41 | + continue |
| 42 | + |
| 43 | + if not without and owner and tool['owner'] not in owner: |
| 44 | + continue |
| 45 | + |
| 46 | + if not without and name and tool['name'] != name: |
| 47 | + continue |
| 48 | + |
| 49 | + ts_url = tool.get('tool_shed_url', DEFAULT_TOOL_SHED_URL) |
| 50 | + if ts_url != DEFAULT_TOOL_SHED_URL: |
| 51 | + logging.warning('Non-default Tool Shed URL for %s/%s: %s', tool['owner'], tool['name'], ts_url) |
| 52 | + ts = tool_sheds[ts_url] |
| 53 | + |
| 54 | + logging.info("Fetching updates for {owner}/{name}".format(**tool)) |
| 55 | + |
| 56 | + try: |
| 57 | + revs = ts.repositories.get_ordered_installable_revisions(tool['name'], tool['owner']) |
| 58 | + except Exception as e: |
| 59 | + print(e) |
| 60 | + continue |
| 61 | + |
| 62 | + logging.debug('TS revisions: %s' % ','.join(revs)) |
| 63 | + latest_rev = revs[-1] |
| 64 | + if latest_rev in tool.get('revisions', []): |
| 65 | + # The rev is already known, don't add again. |
| 66 | + continue |
| 67 | + |
| 68 | + logging.info("Found newer revision of {owner}/{name} ({rev})".format(rev=latest_rev, **tool)) |
| 69 | + |
| 70 | + # Get latest rev, if not already added, add it. |
| 71 | + if 'revisions' not in tool: |
| 72 | + tool['revisions'] = [] |
| 73 | + |
| 74 | + if latest_rev not in tool['revisions']: |
| 75 | + # TS doesn't support utf8 and we don't want to either. |
| 76 | + tool['revisions'].append(str(latest_rev)) |
| 77 | + |
| 78 | + with open(fn + '.lock', 'w') as handle: |
| 79 | + yaml.dump(locked, handle, default_flow_style=False) |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + parser = argparse.ArgumentParser() |
| 84 | + parser.add_argument('fn', type=argparse.FileType('r'), help="Tool.yaml file") |
| 85 | + parser.add_argument('--owner', action='append', help="Repository owner to filter on, anything matching this will be updated. Can be specified multiple times") |
| 86 | + parser.add_argument('--name', help="Repository name to filter on, anything matching this will be updated") |
| 87 | + parser.add_argument('--without', action='store_true', help="If supplied will ignore any owner/name and just automatically add the latest hash for anything lacking one.") |
| 88 | + parser.add_argument('--log', choices=('critical', 'error', 'warning', 'info', 'debug'), default='info') |
| 89 | + args = parser.parse_args() |
| 90 | + logging.basicConfig(level=getattr(logging, args.log.upper())) |
| 91 | + update_file(args.fn.name, owner=args.owner, name=args.name, without=args.without) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + main() |
0 commit comments