annotate TrustStoreGalaxyBrowse.py @ 0:2a996fea49a3

Initial.
author Catherine Wise <catherine.wise@csiro.au>
date Thu, 07 May 2015 12:34:06 +1000
parents
children 0f2a5664c9eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
1 from __future__ import division, absolute_import, print_function, unicode_literals
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
2 import sys
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
3 import shutil
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
4 import gzip
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
5 import tempfile
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
6 import os
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
7 from py_ts import TrustStoreClient, utils
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
8 # from galaxy.datatypes.checkers import *
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
9
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
10 def printNice(elem, f, depth):
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
11 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
12 f.write('\t'*depth + elem.name + " (" + str(len(elem.fragments)) + " parts)\n")
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
13 except AttributeError:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
14 f.write('\t'*depth + elem.name + "\n")
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
15 for child in elem.children:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
16 printNice(child, f, depth+1)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
17
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
18 def check_gzip(file_path):
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
19 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
20 temp = open( file_path, "U" )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
21 magic_check = temp.read( 2 )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
22 temp.close()
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
23 if magic_check != util.gzip_magic:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
24 return ( False, False )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
25 except:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
26 return ( False, False )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
27 return ( True, True )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
28
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
29 def ungzip(download, outputFile):
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
30 is_gzipped, is_valid = check_gzip(download)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
31
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
32 if is_gzipped and not is_valid:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
33 print "File is compressed (gzip) but not valid."
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
34 sys.exit(4)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
35 elif is_gzipped and is_valid:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
36 # We need to uncompress the temp_name file, but BAM files must remain compressed in the BGZF format
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
37 CHUNK_SIZE = 2**20 # 1Mb
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
38 fd, uncompressed = tempfile.mkstemp(prefix='data_id_upload_gunzip_', dir=os.path.dirname(outputFile), text=False )
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
39 gzipped_file = gzip.GzipFile(download, 'rb')
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
40 while 1:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
41 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
42 chunk = gzipped_file.read(CHUNK_SIZE)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
43 except IOError:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
44 os.close(fd)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
45 os.remove(uncompressed)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
46 print 'Problem decompressing gzipped data', dataset, json_file
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
47 sys.exit(4)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
48 if not chunk:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
49 break
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
50 os.write(fd, chunk)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
51 os.close(fd)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
52 gzipped_file.close()
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
53
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
54 shutil.copy(uncompressed, outputFile)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
55 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
56 os.remove(uncompressed)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
57 os.remove(download)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
58 except OSError:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
59 pass
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
60 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
61 shutil.copy(download, outputFile)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
62
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
63 if __name__ == '__main__':
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
64
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
65 kms_url = "https://tstest-kms.it.csiro.au/kmscolab_3_0"
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
66 ims_url = "https://tstest-ims.it.csiro.au/ims_3_0/services/IMS"
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
67 username = sys.argv[1]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
68 password = sys.argv[2]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
69 client_key = "desktop"
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
70 client_secret = "cpU92F1PT7VOCANjSknuCDp4DrubmujoBaF6b0miz8OpKNokEbGMHCaSFK5/lISbBmaaGVCgeADI2A39F3Hkeg=="
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
71 storename = sys.argv[3]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
72 path = sys.argv[4]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
73 fileType = sys.argv[5]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
74 filename = ""
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
75 outputFile = ""
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
76 if len(sys.argv) > 9:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
77 filename = sys.argv[6]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
78 outputFile = sys.argv[7]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
79 outputFileId = sys.argv[8]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
80 otherFilesDir = sys.argv[9]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
81 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
82 outputFile = sys.argv[6]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
83
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
84 config = TrustStoreClient.Config(ims_url, kms_url, client_key, client_secret, tmpDir='/mnt/galaxy/tmp')
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
85 ts = TrustStoreClient.TrustStoreClient(headless=False, config=config)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
86 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
87 ts.authenticate(username, password)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
88 except TrustStoreClient.TrustStoreClientAuthenticationException as e:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
89 print e
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
90 sys.exit(5)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
91 ts.getPrivateKey('privkey.pem')
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
92 listing = ts.listStores()
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
93 found = False
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
94 for store in listing:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
95 if store.friendly_name == storename:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
96 found = True
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
97 root = ts.listDirectory(store)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
98 location = None
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
99 if path != "/":
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
100 location = ts_utils.ts_utils.dirAtPath(root, path)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
101 if not location:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
102 print "Path not found"
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
103 sys.exit(3)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
104 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
105 location = root
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
106 if filename and filename != "":
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
107 outputFileList = [outputFile]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
108 inputFileList = None
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
109 if "," in filename: # we have multiple files guys.
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
110 inputFileList = filename.split(",")
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
111 for inputFile in inputFileList[1:]: # First file will be sent to outputFile.
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
112 outName = "%s_%s_%s_%s_%s" % ('primary', outputFileId, inputFile.replace(".","-"), 'visible', fileType)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
113 outputFileList.append(os.path.join(otherFilesDir, outName))
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
114 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
115 inputFileList = [filename]
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
116 for inFile, outFile in zip(inputFileList, outputFileList):
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
117 downloadMe = ts_utils.ts_utils.recurseToChildNamed(location, inFile)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
118 if downloadMe:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
119 download = ts.getFile(store, downloadMe)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
120 ungzip(download, outFile)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
121 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
122 print "File %s not found" % inFile
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
123 sys.exit(4)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
124 else:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
125 with open(outputFile, 'w+') as f:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
126 try:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
127 for child in root.children:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
128 printNice(child, f, 0)
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
129 except AttributeError as e:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
130 print e
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
131 print root
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
132 if not found:
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
133 print "Store %s not found" % storename
2a996fea49a3 Initial.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
134 sys.exit(2)