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