1
|
1 #!/usr/bin/python
|
|
2 ## ********************************* Kipper *************************************
|
|
3 ##
|
|
4
|
|
5 import vdb_common
|
|
6 import vdb_data_stores
|
|
7 import subprocess
|
|
8 import json
|
|
9 import glob
|
|
10 import os
|
|
11 import sys
|
|
12
|
|
13 class VDBKipperDataStore(vdb_data_stores.VDBDataStore):
|
|
14
|
|
15 metadata = None
|
|
16 versions = None
|
|
17 command = None
|
|
18
|
|
19 def __init__(self, retrieval_obj, spec_file_id):
|
|
20 """
|
|
21 Metadata (version list) is expected in "master/" subfolder of data_store_path on server.
|
|
22
|
|
23 Future: allow for more than one database / .md file in a folder?
|
|
24 """
|
|
25 super(VDBKipperDataStore, self).__init__(retrieval_obj, spec_file_id)
|
|
26 # Ensure we're working with this tool's version of Kipper.
|
|
27 self.command = os.path.join(os.path.dirname(sys._getframe().f_code.co_filename),'kipper.py')
|
|
28 self.versions = []
|
|
29
|
|
30 metadata_path = os.path.join(self.data_store_path,'master','*.md')
|
|
31 metadata_files = glob.glob(metadata_path)
|
|
32
|
|
33 if len(metadata_files) == 0:
|
|
34 return # Handled by empty list error in versioned_data_form.py
|
|
35
|
|
36 metadata_file = metadata_files[0]
|
|
37
|
|
38 with open(metadata_file,'r') as metadata_handle:
|
|
39 self.metadata = json.load(metadata_handle)
|
|
40 for volume in self.metadata['volumes']:
|
|
41 self.versions.extend(volume['versions'])
|
|
42
|
|
43 if len(self.versions) == 0:
|
|
44 print "Error: Unable to locate metadata file: " + metadata_path
|
|
45 sys.exit(1)
|
|
46
|
|
47 self.versions = sorted(self.versions, key=lambda x: x['id'], reverse=True)
|
|
48
|
|
49
|
|
50 def get_version(self, version_name):
|
|
51 """
|
|
52 Trigger populating of the appropriate version folder.
|
|
53 Returns path information to version folder, and its appropriate library label.
|
|
54
|
|
55 @param version_id string
|
|
56 """
|
|
57
|
|
58 version = self.get_metadata_version(version_name)
|
|
59
|
|
60 if not version:
|
|
61 print 'Error: Galaxy was not able to find the given version id in the %s data store.' % self.version_path
|
|
62 sys.exit( 1 )
|
|
63
|
|
64 version_name = version['name']
|
|
65 self.version_path = os.path.join(self.data_store_path, version_name)
|
|
66 self.version_label = vdb_common.lightDate(version['created']) + '_v' + version_name
|
|
67
|
|
68 print self.library_label_path
|
|
69 print self.version_label
|
|
70
|
|
71 self.library_version_path = os.path.join(self.library_label_path, self.version_label)
|
|
72
|
|
73 if not os.path.exists(self.version_path):
|
|
74 try:
|
|
75 os.mkdir(self.version_path)
|
|
76 except:
|
|
77 print 'Error: Galaxy was not able to create data store folder "%s". Check permissions?' % self.version_path
|
|
78 sys.exit( 1 )
|
|
79
|
|
80 # Generate cache if folder is empty (can take a while):
|
|
81 if os.listdir(self.version_path) == []:
|
|
82
|
|
83 db_file = os.path.join(self.data_store_path, 'master', self.metadata["db_file_name"] )
|
|
84 command = [self.command, db_file, '-e','-I', version_name, '-o', self.version_path ]
|
|
85
|
|
86 try:
|
|
87 result = subprocess.call(command);
|
|
88 except:
|
|
89 print 'Error: Galaxy was not able to run the kipper.py program successfully for this job: "%s". Check permissions?' % self.version_path
|
|
90 sys.exit( 1 )
|
|
91
|
|
92
|
|
93 def get_metadata_version(self, version_name=''):
|
|
94 if version_name == '':
|
|
95 return self.versions[0]
|
|
96
|
|
97 for version in self.versions:
|
|
98 if str(version['name']) == version_name:
|
|
99 return version
|
|
100
|
|
101 return False
|
|
102
|