annotate TrustStoreGalaxyImport.py @ 22:1179f6e90e78

tmpfile
author Catherine Wise <catherine.wise@csiro.au>
date Mon, 13 Jan 2014 09:15:15 +1100
parents 8cf18ca6f13f
children cf4cf95e6678
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
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
5 from py_ts import TrustStoreClient, ts_utils
20
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
6 from galaxy.datatypes.checkers import *
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
7
5
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
8 def printNice(elem, f, depth):
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
9 try:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
10 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
11 except AttributeError:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
12 f.write('\t'*depth + elem.name + "\n")
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
13 for child in elem.children:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
14 printNice(child, f, depth+1)
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
15
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
16 if __name__ == '__main__':
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
17
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
18 kms_url = sys.argv[1]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
19 ims_url = sys.argv[2]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
20 username = sys.argv[3]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
21 password = sys.argv[4]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
22 client_key = sys.argv[5]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
23 client_secret = sys.argv[6]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
24 storename = sys.argv[7]
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
25 path = sys.argv[8]
5
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
26 filename = ""
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
27 outputFile = ""
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
28 if len(sys.argv) > 10:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
29 filename = sys.argv[9]
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
30 outputFile = sys.argv[10]
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
31 else:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
32 outputFile = sys.argv[9]
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
33
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
34 config = TrustStoreClient.Config(ims_url, kms_url, client_key, client_secret)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
35 ts = TrustStoreClient.TrustStoreClient(headless=True, config=config)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
36 try:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
37 ts.authenticate(username, password)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
38 except TrustStoreClient.TrustStoreClientAuthenticationException as e:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
39 print e
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
40 sys.exit(5)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
41 ts.getPrivateKey('privkey.pem')
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
42 listing = ts.listStores()
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
43 found = False
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
44 for store in listing:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
45 if store.friendly_name == storename:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
46 found = True
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
47 root = ts.listDirectory(store)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
48 location = None
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
49 if path != "/":
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
50 location = ts_utils.ts_utils.dirAtPath(root, path)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
51 if not location:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
52 print "Path not found"
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
53 sys.exit(3)
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
54 else:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
55 location = root
5
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
56 if filename and filename != "":
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
57 downloadMe = ts_utils.ts_utils.recurseToChildNamed(location, filename)
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
58 if downloadMe:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
59 download = ts.getFile(store, downloadMe)
20
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
60 is_gzipped, is_valid = check_gzip(download)
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
61
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
62 if is_gzipped and not is_valid:
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
63 print "File is compressed (gzip) but not valid."
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
64 sys.exit(4)
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
65 elif is_gzipped and is_valid:
21
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
66 # We need to uncompress the temp_name file, but BAM files must remain compressed in the BGZF format
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
67 CHUNK_SIZE = 2**20 # 1Mb
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
68 fd, uncompressed = tempfile.mkstemp(prefix='data_id_%s_upload_gunzip_' % dataset.dataset_id, dir=os.path.dirname(outputFile), text=False )
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
69 gzipped_file = gzip.GzipFile(download, 'rb')
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
70 while 1:
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
71 try:
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
72 chunk = gzipped_file.read(CHUNK_SIZE)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
73 except IOError:
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
74 os.close(fd)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
75 os.remove(uncompressed)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
76 print 'Problem decompressing gzipped data', dataset, json_file
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
77 sys.exit(4)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
78 if not chunk:
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
79 break
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
80 os.write(fd, chunk)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
81 os.close(fd)
Catherine Wise <catherine.wise@csiro.au>
parents: 20
diff changeset
82 gzipped_file.close()
20
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
83
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
84 shutil.copy(uncompressed, outputFile)
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
85 else:
b94a8f55b1da Un-gzip.
Catherine Wise <catherine.wise@csiro.au>
parents: 5
diff changeset
86 shutil.copy(download, outputFile)
5
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
87 else:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
88 print "File not found"
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
89 sys.exit(4)
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
90 else:
5
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
91 with open(outputFile, 'w+') as f:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
92 try:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
93 for child in root.children:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
94 printNice(child, f, 0)
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
95 except AttributeError as e:
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
96 print e
159d2159e745 Add list files.
Catherine Wise <catherine.wise@csiro.au>
parents: 4
diff changeset
97 print root
4
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
98 if not found:
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
99 print "Store not found"
32d9b3343955 Toooool upload.
Catherine Wise <catherine.wise@csiro.au>
parents:
diff changeset
100 sys.exit(2)