comparison data_manager/data_manager_cat.py @ 0:cffd8e2382cf draft

"planemo upload commit 4e5a8d7629d90c41219b48b648dd4ab675b84af0"
author iuc
date Mon, 09 Dec 2019 10:28:15 -0500
parents
children 74af283d8ebd
comparison
equal deleted inserted replaced
-1:000000000000 0:cffd8e2382cf
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 sys
9 import tarfile
10 import tempfile
11 import zipfile
12 try:
13 # For Python 3.0 and later
14 from urllib.request import urlopen
15 except ImportError:
16 # Fall back to Python 2 imports
17 from urllib2 import urlopen
18
19
20 def url_download(url, workdir):
21 file_path = os.path.join(workdir, 'download.dat')
22 src = None
23 dst = None
24 try:
25 src = urlopen(url)
26 with open(file_path, 'wb') as dst:
27 while True:
28 chunk = src.read(2**10)
29 if chunk:
30 dst.write(chunk)
31 else:
32 break
33 finally:
34 if src:
35 src.close()
36 if tarfile.is_tarfile(file_path):
37 fh = tarfile.open(file_path, 'r:*')
38 elif zipfile.is_zipfile(file_path):
39 fh = zipfile.ZipFile(file_path, 'r')
40 else:
41 return
42 fh.extractall(workdir)
43 os.remove(file_path)
44
45
46 def cat_prepare(install_dir, db_dir=None, tax_dir=None):
47 if db_dir and tax_dir:
48 cmd = ['CAT', 'prepare', '--existing', '-d', db_dir, '-t', tax_dir]
49 else:
50 cmd = ['CAT', 'prepare', '--fresh', '-q']
51 cmd_stdout = tempfile.NamedTemporaryFile()
52 cmd_stderr = tempfile.NamedTemporaryFile()
53 return_code = subprocess.call(cmd, shell=False, cwd=install_dir,
54 stdout=cmd_stdout, stderr=cmd_stderr)
55 if return_code:
56 msg = "stdout:\n%s\nstderr:\n%s" % (cmd_stdout.read(),
57 cmd_stderr.read())
58 cmd_stdout.close()
59 cmd_stderr.close()
60 raise Exception('Error: (%s), returncode=%s %s'
61 % (' '.join(cmd), return_code, msg))
62
63
64 def main():
65 parser = argparse.ArgumentParser()
66 parser.add_argument('--config_file', required=True)
67 parser.add_argument('--install_path', default=None)
68 parser.add_argument('--db_url', default=None)
69 parser.add_argument('--database_folder', default=None)
70 parser.add_argument('--taxonomy_folder', default=None)
71 args = parser.parse_args()
72
73 cat_path = None
74 cat_db = None
75 tax_db = None
76 if args.database_folder and args.taxonomy_folder:
77 cat_path = os.path.dirname(args.database_folder)
78 cat_db = os.path.basename(args.database_folder)
79 tax_db = os.path.basename(args.taxonomy_folder)
80 cat_prepare(os.getcwd(),
81 db_dir=args.database_folder,
82 tax_dir=args.taxonomy_folder)
83 elif not args.install_path:
84 sys.exit(1)
85 else:
86 if not os.path.exists(args.install_path):
87 os.makedirs(args.install_path)
88 if args.db_url:
89 url_download(args.db_url, args.install_path)
90 else:
91 cat_prepare(args.install_path)
92 for root, dirs, files in os.walk(args.install_path):
93 for dname in dirs:
94 if dname.endswith('CAT_database'):
95 cat_db = dname
96 elif dname.endswith('taxonomy'):
97 tax_db = dname
98 if cat_db and tax_db:
99 cat_path = root
100 break
101 cat_dir = os.path.basename(cat_path)
102 dm_dict = {}
103 dm_dict['data_tables'] = dm_dict.get('data_tables', {})
104 data_table = 'cat_database'
105 dm_dict['data_tables'][data_table]\
106 = dm_dict['data_tables'].get(data_table, [])
107 data_table_entry = dict(value=cat_dir, name=cat_dir,
108 database_folder=os.path.join(cat_dir, cat_db),
109 taxonomy_folder=os.path.join(cat_dir, tax_db))
110 dm_dict['data_tables'][data_table].append(data_table_entry)
111 # save info to json file
112 open(args.config_file, 'w').write(json.dumps(dm_dict))
113
114
115 if __name__ == "__main__":
116 main()