comparison data_manager/data_manager.py @ 1:926847693e4d draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
author iuc
date Tue, 04 Apr 2017 18:08:21 -0400
parents fafcd2f5db36
children 4af4e175db0d
comparison
equal deleted inserted replaced
0:fafcd2f5db36 1:926847693e4d
1 import argparse 1 import argparse
2 import datetime 2 import datetime
3 import json 3 import json
4 import os 4 import os
5 import shutil 5 import shutil
6 import sys
7 import tarfile 6 import tarfile
8 import urllib2
9 import zipfile 7 import zipfile
8 try:
9 # For Python 3.0 and later
10 from urllib.request import Request, urlopen
11 except ImportError:
12 # Fall back to Python 2 imports
13 from urllib2 import Request, urlopen
10 14
11 parser = argparse.ArgumentParser(description='Create data manager json.')
12 parser.add_argument('--out', dest='output', action='store', help='JSON filename')
13 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID')
14 parser.add_argument('--url', dest='url', action='store', default='ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz', help='Download URL')
15
16 args = parser.parse_args()
17 15
18 def url_download(url, workdir): 16 def url_download(url, workdir):
19 file_path = os.path.join(workdir, 'download.dat') 17 file_path = os.path.join(workdir, 'download.dat')
20 if not os.path.exists(workdir): 18 if not os.path.exists(workdir):
21 os.makedirs(workdir) 19 os.makedirs(workdir)
22 src = None 20 src = None
23 dst = None 21 dst = None
24 try: 22 try:
25 req = urllib2.Request(url) 23 req = Request(url)
26 src = urllib2.urlopen(req) 24 src = urlopen(req)
27 dst = open(file_path, 'wb') 25 with open(file_path, 'wb') as dst:
28 while True: 26 while True:
29 chunk = src.read(2**10) 27 chunk = src.read(2**10)
30 if chunk: 28 if chunk:
31 dst.write(chunk) 29 dst.write(chunk)
32 else: 30 else:
33 break 31 break
34 except Exception, e:
35 print >>sys.stderr, str(e)
36 finally: 32 finally:
37 if src: 33 if src:
38 src.close() 34 src.close()
39 if dst:
40 dst.close()
41 if tarfile.is_tarfile(file_path): 35 if tarfile.is_tarfile(file_path):
42 fh = tarfile.open(file_path, 'r:*') 36 fh = tarfile.open(file_path, 'r:*')
43 elif zipfile.is_zipfile(file_path): 37 elif zipfile.is_zipfile(file_path):
44 fh = zipfile.ZipFile(file_path, 'r') 38 fh = zipfile.ZipFile(file_path, 'r')
45 else: 39 else:
62 output_path = os.path.abspath(os.path.join(os.getcwd(), 'taxonomy')) 56 output_path = os.path.abspath(os.path.join(os.getcwd(), 'taxonomy'))
63 for filename in os.listdir(workdir): 57 for filename in os.listdir(workdir):
64 shutil.move(os.path.join(output_path, filename), target_directory) 58 shutil.move(os.path.join(output_path, filename), target_directory)
65 file(args.output, 'w').write(json.dumps(data_manager_json)) 59 file(args.output, 'w').write(json.dumps(data_manager_json))
66 60
61
67 if __name__ == '__main__': 62 if __name__ == '__main__':
63 parser = argparse.ArgumentParser(description='Create data manager json.')
64 parser.add_argument('--out', dest='output', action='store', help='JSON filename')
65 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID')
66 parser.add_argument('--url', dest='url', action='store', default='ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz', help='Download URL')
67 args = parser.parse_args()
68
68 main(args) 69 main(args)