Mercurial > repos > iuc > data_manager_fetch_busco
comparison data_manager/data_manager.py @ 0:53eec20e8fb6 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_busco/ commit 2896dcfd180800d00ea413a59264ef8b11788b8e
| author | iuc |
|---|---|
| date | Thu, 19 Oct 2017 15:56:20 -0400 |
| parents | |
| children | 15b97817550a |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:53eec20e8fb6 |
|---|---|
| 1 import argparse | |
| 2 import datetime | |
| 3 import json | |
| 4 import os | |
| 5 import shutil | |
| 6 import tarfile | |
| 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 | |
| 14 | |
| 15 | |
| 16 def url_download(url, workdir): | |
| 17 file_path = os.path.join(workdir, 'download.dat') | |
| 18 if not os.path.exists(workdir): | |
| 19 os.makedirs(workdir) | |
| 20 src = None | |
| 21 dst = None | |
| 22 try: | |
| 23 req = Request(url) | |
| 24 src = urlopen(req) | |
| 25 with open(file_path, 'wb') as dst: | |
| 26 while True: | |
| 27 chunk = src.read(2**10) | |
| 28 if chunk: | |
| 29 dst.write(chunk) | |
| 30 else: | |
| 31 break | |
| 32 finally: | |
| 33 if src: | |
| 34 src.close() | |
| 35 if tarfile.is_tarfile(file_path): | |
| 36 fh = tarfile.open(file_path, 'r:*') | |
| 37 elif zipfile.is_zipfile(file_path): | |
| 38 fh = zipfile.ZipFile(file_path, 'r') | |
| 39 else: | |
| 40 return | |
| 41 fh.extractall(workdir) | |
| 42 os.remove(file_path) | |
| 43 | |
| 44 | |
| 45 def main(args): | |
| 46 workdir = os.path.join(os.getcwd(), 'busco') | |
| 47 url_download(args.url, workdir) | |
| 48 data_manager_entry = {} | |
| 49 data_manager_entry['value'] = args.name.lower() | |
| 50 data_manager_entry['name'] = args.name | |
| 51 data_manager_entry['path'] = '.' | |
| 52 data_manager_json = dict(data_tables=dict(busco=data_manager_entry)) | |
| 53 params = json.loads(open(args.output).read()) | |
| 54 target_directory = params['output_data'][0]['extra_files_path'] | |
| 55 os.mkdir(target_directory) | |
| 56 output_path = os.path.abspath(os.path.join(os.getcwd(), 'busco')) | |
| 57 for filename in os.listdir(workdir): | |
| 58 shutil.move(os.path.join(output_path, filename), target_directory) | |
| 59 file(args.output, 'w').write(json.dumps(data_manager_json)) | |
| 60 | |
| 61 | |
| 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', help='Download URL') | |
| 67 args = parser.parse_args() | |
| 68 | |
| 69 main(args) |
