| 4 | 1 import sys | 
|  | 2 import shutil | 
| 20 | 3 import gzip | 
| 4 | 4 from py_ts import TrustStoreClient, ts_utils | 
| 20 | 5 from galaxy.datatypes.checkers import * | 
| 4 | 6 | 
| 5 | 7 def printNice(elem, f, depth): | 
|  | 8     try: | 
|  | 9         f.write('\t'*depth + elem.name + " (" + str(len(elem.fragments)) + " parts)\n") | 
|  | 10     except AttributeError: | 
|  | 11         f.write('\t'*depth + elem.name + "\n") | 
|  | 12         for child in elem.children: | 
|  | 13             printNice(child, f, depth+1) | 
|  | 14 | 
| 4 | 15 if __name__ == '__main__': | 
|  | 16 | 
|  | 17     kms_url = sys.argv[1] | 
|  | 18     ims_url = sys.argv[2] | 
|  | 19     username = sys.argv[3] | 
|  | 20     password = sys.argv[4] | 
|  | 21     client_key = sys.argv[5] | 
|  | 22     client_secret = sys.argv[6] | 
|  | 23     storename = sys.argv[7] | 
|  | 24     path = sys.argv[8] | 
| 5 | 25     filename = "" | 
|  | 26     outputFile = "" | 
|  | 27     if len(sys.argv) > 10: | 
|  | 28         filename = sys.argv[9] | 
|  | 29         outputFile = sys.argv[10] | 
|  | 30     else: | 
|  | 31         outputFile = sys.argv[9] | 
| 4 | 32 | 
|  | 33     config = TrustStoreClient.Config(ims_url, kms_url, client_key, client_secret) | 
|  | 34     ts = TrustStoreClient.TrustStoreClient(headless=True, config=config) | 
|  | 35     try: | 
|  | 36         ts.authenticate(username, password) | 
|  | 37     except TrustStoreClient.TrustStoreClientAuthenticationException as e: | 
|  | 38         print e | 
|  | 39         sys.exit(5) | 
|  | 40     ts.getPrivateKey('privkey.pem') | 
|  | 41     listing = ts.listStores() | 
|  | 42     found = False | 
|  | 43     for store in listing: | 
|  | 44         if store.friendly_name == storename: | 
|  | 45             found = True | 
|  | 46             root = ts.listDirectory(store) | 
|  | 47             location = None | 
|  | 48             if path != "/": | 
|  | 49                 location = ts_utils.ts_utils.dirAtPath(root, path) | 
|  | 50                 if not location: | 
|  | 51                     print "Path not found" | 
|  | 52                     sys.exit(3) | 
|  | 53             else: | 
|  | 54                 location = root | 
| 5 | 55             if filename and filename != "": | 
|  | 56                 downloadMe = ts_utils.ts_utils.recurseToChildNamed(location, filename) | 
|  | 57                 if downloadMe: | 
|  | 58                     download = ts.getFile(store, downloadMe) | 
| 20 | 59                     is_gzipped, is_valid = check_gzip(download) | 
|  | 60 | 
|  | 61                     if is_gzipped and not is_valid: | 
|  | 62                         print "File is compressed (gzip) but not valid." | 
|  | 63                         sys.exit(4) | 
|  | 64                     elif is_gzipped and is_valid: | 
|  | 65                         if link_data_only == 'copy_files': | 
|  | 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 | 
|  | 68                             fd, uncompressed = tempfile.mkstemp(prefix='data_id_%s_upload_gunzip_' % dataset.dataset_id, dir=os.path.dirname(outputFile), text=False ) | 
|  | 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() | 
|  | 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) |