comparison data_manager/data_manager_cat.py @ 0:f59e7e242bde draft

planemo upload commit f80f020c77d04c2e13b89aaea3d784314b940931-dirty
author jjohnson
date Sun, 24 Nov 2019 21:54:57 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f59e7e242bde
1 #!/usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 import json
6 import os.path
7 import subprocess
8 import tarfile
9 import tempfile
10 import zipfile
11 try:
12 # For Python 3.0 and later
13 from urllib.request import urlopen
14 except ImportError:
15 # Fall back to Python 2 imports
16 from urllib2 import urlopen
17
18
19 def url_download(url, workdir):
20 file_path = os.path.join(workdir, 'download.dat')
21 src = None
22 dst = None
23 try:
24 src = urlopen(url)
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 cat_prepare(install_dir):
46 cmd = ['CAT', 'prepare' '--fresh', '-q']
47 cmd_stdout = tempfile.NamedTemporaryFile()
48 cmd_stderr = tempfile.NamedTemporaryFile()
49 return_code = subprocess.call(cmd, shell=True, cwd=install_dir,
50 stdout=cmd_stdout, stderr=cmd_stderr)
51 if return_code:
52 msg = "stdout:\n%s\nstderr:\n%s" % (cmd_stdout.read(),
53 cmd_stderr.read())
54 cmd_stdout.close()
55 cmd_stderr.close()
56 raise Exception('Error: (%s), returncode=%s %s'
57 % (' '.join(cmd), return_code, msg))
58
59
60 def main():
61 parser = argparse.ArgumentParser()
62 parser.add_argument('--config_file')
63 parser.add_argument('--install_path')
64 parser.add_argument('--db_url', default=None)
65 args = parser.parse_args()
66
67 if not os.path.exists(args.install_path):
68 os.makedirs(args.install_path)
69 if args.db_url:
70 url_download(args.db_url, args.install_path)
71 else:
72 cat_prepare(args.install_path)
73
74 cat_path = None
75 cat_db = None
76 tax_db = None
77 for root, dirs, files in os.walk(args.install_path):
78 for dname in dirs:
79 if dname.endswith('CAT_database'):
80 cat_db = dname
81 elif dname.endswith('taxonomy'):
82 tax_db = dname
83 if cat_db and tax_db:
84 cat_path = root
85 break
86
87 cat_dir = os.path.basename(cat_path)
88 # params = json.loads(open(args.config_file).read())
89 dm_dict = {}
90 dm_dict['data_tables'] = dm_dict.get('data_tables', {})
91 data_table = 'cat_database'
92 dm_dict['data_tables'][data_table]\
93 = dm_dict['data_tables'].get(data_table, [])
94 data_table_entry = dict(value=cat_dir, name=cat_dir,
95 database_folder=os.path.join(cat_dir, cat_db),
96 taxonomy_folder=os.path.join(cat_dir, tax_db))
97 dm_dict['data_tables'][data_table].append(data_table_entry)
98 # save info to json file
99 open(args.config_file, 'wb').write(json.dumps(dm_dict))
100
101
102 if __name__ == "__main__":
103 main()