annotate fetch_latest_pangolearn.py @ 8:3a656a4a43ed draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 5a3f70ab430c70b260dd6e0f5b6c53aed2ca5520"
author iuc
date Fri, 13 Aug 2021 19:53:03 +0000
parents 437e28791761
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
2
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
3 import argparse
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
4 import json
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
5 import os
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
6 import sys
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
7 import tarfile
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
8
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
9 # rely on the fact that pangolin itself uses the requests module
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
10 import requests
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
11 from requests.adapters import HTTPAdapter
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
12
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
13 parser = argparse.ArgumentParser(description='download latest pangoLEARN database')
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
14 parser.add_argument('--timeout', type=float, default=60.0, help="Timeout for connecting or downloading the pangoLEARN database (in seconds)")
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
15 parser.add_argument('--max_retries', type=int, default=5, help="How many times to retry fetching the pangoLEARN database")
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
16 args = parser.parse_args()
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
17
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
18 try:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
19 s = requests.Session()
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
20 s.mount('https://', HTTPAdapter(max_retries=args.max_retries))
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
21 response = s.get(
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
22 "https://api.github.com/repos/cov-lineages/pangoLEARN/releases/latest"
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
23 )
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
24 if response.status_code == 200:
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
25 details = json.loads(response.text)
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
26 response = s.get(details["tarball_url"], timeout=args.timeout)
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
27 if response.status_code == 200:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
28 with open("pangolearn.tgz", "wb") as handle:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
29 handle.write(response.content)
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
30 tf = tarfile.open("pangolearn.tgz")
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
31 pl_path = tf.next().name
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
32 tf.extractall()
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
33 tf.close()
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
34 os.rename(os.path.join(pl_path, "pangoLEARN"), "datadir")
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
35 else:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
36 response.raise_for_status()
1
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
37 else:
f557122d379e "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit 1522bdb834ffab157d1ca25e6e74db21f62e3aae"
iuc
parents:
diff changeset
38 response.raise_for_status()
6
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
39 except (requests.ConnectionError, requests.HTTPError, requests.TooManyRedirects) as e:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
40 sys.exit('Failed to download pangoLEARN database: {}'.format(e))
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
41 except requests.Timeout:
437e28791761 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/pangolin commit f53dc92d3cf6997da9ad21f01ad3fd3477aae068"
iuc
parents: 1
diff changeset
42 sys.exit('Timeout while trying to download pangoLEARN database')