annotate data_stores/vdb_git.py @ 1:5c5027485f7d draft

Uploaded correct file
author damion
date Sun, 09 Aug 2015 16:07:50 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 #!/usr/bin/python
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 ## ********************************* GIT ARCHIVE *******************************
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3 ##
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5 import os, sys
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 import vdb_common
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7 import vdb_data_stores
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8 import subprocess
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9 import datetime
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 class VDBGitDataStore(vdb_data_stores.VDBDataStore):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13 def __init__(self, retrieval_obj, spec_file_id):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15 Archive is expected to be in "master/" subfolder of data_store_path on server.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 super(VDBGitDataStore, self).__init__(retrieval_obj, spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18 self.command = 'git'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 gitPath = self.data_store_path + 'master/'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20 if not os.path.isdir(os.path.join(gitPath,'.git') ):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21 print "Error: Unable to locate git archive file: " + gitPath
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22 sys.exit(1)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24 command = [self.command, '--git-dir=' + gitPath + '.git', '--work-tree=' + gitPath, 'for-each-ref','--sort=-*committerdate', "--format=%(*committerdate:raw) %(refname)"]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25 # to list just 1 id: command.append('refs/tags/' + version_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26 # git --git-dir=/.../NCBI_16S/master/.git --work-tree=/.../NCBI_16S/master/ tag
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27 items, error = subprocess.Popen(command,stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 items = items.split("\n") #Loop through list of tags
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29 versions = []
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30 for ptr, item in enumerate(items):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32 # Ignore master branch name; time is included as separate field in all other cases
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 if item.strip().find(" ") >0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34 (vtime, voffset, name) = item.split(" ")
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35 created = vdb_common.get_unix_time(vtime, voffset)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36 item_name = name[10:] #strip 'refs/tags/' part off
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37 versions.append({'name':item_name, 'id':item_name, 'created': created})
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39 self.versions = versions
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42 def get_version(self, version_name):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44 Returns server folder path to version folder containing git files for a given version_id (git tag)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46 FUTURE: TO AVOID USE CONFLICTS FOR VERSION RETRIEVAL, GIT CLONE INTO TEMP FOLDER
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47 with -s / --shared and -n / --no-checkout (to avoid head build) THEN CHECKOUT version
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48 ...
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49 REMOVE CLONE GIT REPO
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51 @param galaxy_instance object A Bioblend galaxy instance
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52 @param library_id string Identifier for a galaxy data library
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53 @param library_label_path string Full hierarchic label of a library file or folder, PARENT of version id folder.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 @param base_folder_id string a library folder id under which version files should exist
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56 @param version_id alphaneumeric string (git tag)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59 version = self.get_metadata_version(version_name)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61 if not version:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62 print 'Error: Galaxy was not able to find the given version id in the %s data store.' % self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63 sys.exit( 1 )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65 version_name = version['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66 self.version_path = os.path.join(self.data_store_path, version_name)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67 self.version_label = vdb_common.lightDate(version['created']) + '_v' + version_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68 self.library_version_path = os.path.join(self.library_label_path, self.version_label)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70 # If Data Library Versioned Data folder doesn't exist for this version, then create it
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71 if not os.path.exists(self.version_path):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73 os.mkdir(self.version_path)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 except:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75 print 'Error: Galaxy was not able to create data store folder "%s". Check permissions?' % self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76 sys.exit( 1 )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78 if os.listdir(self.version_path) == []:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80 git_path = self.data_store_path + 'master/'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 # RETRIEVE LIST OF FILES FOR GIVEN GIT TAG (using "ls-tree".
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82 # It can happen independently of git checkout)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84 command = [self.command, '--git-dir=%s/.git' % git_path, 'ls-tree','--name-only','-r', version_name]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85 items, error = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 git_files = items.split('\n')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88 # PERFORM GIT CHECKOUT
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
89 command = [self.command, '--git-dir=%s/.git' % git_path, '--work-tree=%s' % git_path, 'checkout', version_name]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
90 results, error = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
91
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
92 vdb_common.move_files(git_path, self.version_path, git_files)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
93
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
94
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
95
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
96 def get_metadata_version(self, version_name=''):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
97 if version_name == '':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
98 return self.versions[0]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
99
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
100 for version in self.versions:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
101 if str(version['name']) == version_name:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
102 return version
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
103
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
104 return False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
105
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
106