-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathupdate-tool.py
72 lines (53 loc) · 2.63 KB
/
update-tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import yaml
import os
import glob
import copy
import argparse
import logging
from bioblend import toolshed
ts = toolshed.ToolShedInstance(url='https://toolshed.g2.bx.psu.edu')
def update_file(fn, owner=None, name=None, without=False):
with open(fn + '.lock', 'r') as handle:
locked = yaml.safe_load(handle)
# Update any locked tools.
for tool in locked['tools']:
# If without, then if it is lacking, we should exec.
logging.debug("Examining {owner}/{name}".format(**tool))
if without:
if 'revisions' in tool and not len(tool.get('revisions', [])) == 0:
continue
if not without and owner and tool['owner'] != owner:
continue
if not without and name and tool['name'] != name:
continue
logging.info("Fetching updates for {owner}/{name}".format(**tool))
try:
revs = ts.repositories.get_ordered_installable_revisions(tool['name'], tool['owner'])
except Exception as e:
print(e)
continue
logging.debug('TS revisions: %s' % ','.join(revs))
latest_rev = revs[-1]
if latest_rev in tool.get('revisions', []):
# The rev is already known, don't add again.
continue
logging.info("Found newer revision of {owner}/{name} ({rev})".format(rev=latest_rev, **tool))
# Get latest rev, if not already added, add it.
if 'revisions' not in tool:
tool['revisions'] = []
if latest_rev not in tool['revisions']:
# TS doesn't support utf8 and we don't want to either.
tool['revisions'].append(str(latest_rev))
tool['revisions'] = sorted(list(set( tool['revisions'] )))
with open(fn + '.lock', 'w') as handle:
yaml.dump(locked, handle, default_flow_style=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('fn', type=argparse.FileType('r'), help="Tool.yaml file")
parser.add_argument('--owner', help="Repository owner to filter on, anything matching this will be updated")
parser.add_argument('--name', help="Repository name to filter on, anything matching this will be updated")
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.")
parser.add_argument('--log', choices=('critical', 'error', 'warning', 'info', 'debug'), default='info')
args = parser.parse_args()
logging.basicConfig(level=getattr(logging, args.log.upper()))
update_file(args.fn.name, owner=args.owner, name=args.name, without=args.without)