annotate TrustStoreGalaxyImport.py @ 25:20dc961b7949

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