annotate TrustStoreGalaxyImport.py @ 39:64e06297da39 default tip

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