-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcabsdownload.py
More file actions
executable file
·67 lines (57 loc) · 2.16 KB
/
Copy pathcabsdownload.py
File metadata and controls
executable file
·67 lines (57 loc) · 2.16 KB
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
#!/usr/bin/env python
import os
import re
import sys
import time
import requests
def download_file(url, filename):
"""Download a file from a given URL.
:param url: The URL for the file to be downloaded.
"""
response = requests.get(url, stream=True)
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
def fetch_results(job_urls, base_dir):
"""Fetch the docking results for a list of jobs.
:param job_urls: List of URLs for the jobs to be downloaded.
:return: URLs for jobs whose downloads are still pending.
"""
for url in job_urls[:]:
response = requests.get(url)
if response.status_code != requests.codes.ok:
print 'Error fetching job %s. (%d)' % (
url,
response.status_code,
)
continue
status = re.findall('Status:(.*)', response.text)[0]
if 'running' in status:
print url, 'still running.'
elif 'error' in status:
print url, 'finished with errors.'
job_urls.remove(url)
elif 'done' in status:
print url, 'done, download started...'
results_url = url.replace('/job/', '/job/CABSdock_')[:-1] + '.zip'
filename = os.path.join(base_dir, os.path.basename(results_url))
download_file(results_url, filename)
job_urls.remove(url)
print url, 'results download finished.'
return job_urls
if __name__ == '__main__':
if len(sys.argv) < 1:
print 'Syntax:\n cabsfetch.py job_urls_file'
else:
base_dir = os.path.dirname(sys.argv[1])
with open(sys.argv[1]) as urls_file:
job_urls = urls_file.read().splitlines()
while len(job_urls) > 0:
print '-' * 80
print 'Retrieving results for %d jobs...' % len(job_urls)
job_urls = fetch_results(job_urls, base_dir)
# Sleep a while before polling again
if len(job_urls) > 0:
time.sleep(60)