Mercurial > repos > iuc > data_manager_gtdbtk_database_installer
comparison data_manager/gtdbtk_database_installer.py @ 0:629464b96c2e draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_gtdbtk_database_installer commit 69da0d34ea6e72c0427ec2d3e882c86cbb90833f
author | iuc |
---|---|
date | Thu, 08 Dec 2022 19:26:49 +0000 |
parents | |
children | 6ab422fba1a3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:629464b96c2e |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import argparse | |
4 import json | |
5 import os | |
6 import shutil | |
7 import sys | |
8 import tarfile | |
9 from urllib.parse import urlparse | |
10 from urllib.request import Request | |
11 from urllib.request import urlopen | |
12 | |
13 | |
14 def url_download(url, target_directory): | |
15 url_parts = urlparse(url) | |
16 tarball = os.path.abspath(os.path.join(target_directory, os.path.basename(url_parts.path))) | |
17 src = None | |
18 dst = None | |
19 try: | |
20 req = Request(url) | |
21 src = urlopen(req) | |
22 with open(tarball, 'wb') as dst: | |
23 while True: | |
24 chunk = src.read(2**10) | |
25 if chunk: | |
26 dst.write(chunk) | |
27 else: | |
28 break | |
29 except Exception as e: | |
30 sys.exit(str(e)) | |
31 finally: | |
32 if src is not None: | |
33 src.close() | |
34 if tarfile.is_tarfile(tarball): | |
35 fh = tarfile.open(tarball, 'r:*') | |
36 else: | |
37 return tarball | |
38 fh.extractall(target_directory) | |
39 fh.close() | |
40 os.remove(tarball) | |
41 # The tarball extraction will create a directory named | |
42 # something like release202 in the target_directory, so | |
43 # we need to move the items in that directory to the | |
44 # target directory. | |
45 subdir = next(os.walk(target_directory))[1][0] | |
46 subdir_path = os.path.join(target_directory, subdir) | |
47 items = os.listdir(subdir_path) | |
48 for item in items: | |
49 item_path = os.path.join(subdir_path, item) | |
50 shutil.move(item_path, target_directory) | |
51 os.rmdir(subdir_path) | |
52 return target_directory | |
53 | |
54 | |
55 def download(database_id, database_name, url, out_file): | |
56 | |
57 with open(out_file) as fh: | |
58 params = json.load(fh) | |
59 | |
60 target_directory = params['output_data'][0]['extra_files_path'] | |
61 os.makedirs(target_directory) | |
62 file_path = url_download(url, target_directory) | |
63 | |
64 data_manager_json = {"data_tables": {}} | |
65 data_manager_entry = {} | |
66 data_manager_entry['value'] = database_id | |
67 data_manager_entry['name'] = database_name | |
68 data_manager_entry['path'] = file_path | |
69 data_manager_json["data_tables"]["gtdbtk_database"] = data_manager_entry | |
70 | |
71 with open(out_file, 'w') as fh: | |
72 json.dump(data_manager_json, fh, sort_keys=True) | |
73 | |
74 | |
75 parser = argparse.ArgumentParser() | |
76 | |
77 parser.add_argument('--database_name', dest='database_name', help='GTDB-Tk database display name') | |
78 parser.add_argument('--database_id', dest='database_id', help='Unique GTDB-Tk database id') | |
79 parser.add_argument('--url', dest='url', help='URL to download GTDB-Tk databse version') | |
80 parser.add_argument('--out_file', dest='out_file', help='JSON output file') | |
81 | |
82 args = parser.parse_args() | |
83 | |
84 download(args.database_id, args.database_name, args.url, args.out_file) |