Mercurial > repos > cathywise > truststore_browse_testing
annotate TrustStoreGalaxyBrowse.py @ 45:31838e0ecf45 default tip
Fixes.
| author | Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> | 
|---|---|
| date | Fri, 26 Jun 2015 10:24:40 +1000 | 
| parents | 174df9815ecd | 
| children | 
| rev | line source | 
|---|---|
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 1 """TrustStore downloaded for Galaxy.""" | 
| 0 | 2 from __future__ import division, absolute_import, print_function, unicode_literals | 
| 3 import sys | |
| 4 import shutil | |
| 5 import gzip | |
| 6 import tempfile | |
| 7 import os | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 8 import json | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 9 import operator | 
| 15 | 10 import urlparse | 
| 0 | 11 from py_ts import TrustStoreClient, utils | 
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 12 # import galaxy.model # need to import model before sniff to resolve a circular import dependency | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 13 from galaxy.datatypes.checkers import util | 
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 14 # from galaxy.datatypes import sniff | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 15 # from galaxy.datatypes.registry import Registry | 
| 0 | 16 | 
| 16 | 17 # Tell urllib3 to use pyOpenSSL because we are on old Python stdlib. | 
| 24 
f4585bf75a8a
Give up, try to update Python instead.
 Catherine Wise <catherine.wise@csiro.au> parents: 
16diff
changeset | 18 # import urllib3.contrib.pyopenssl | 
| 
f4585bf75a8a
Give up, try to update Python instead.
 Catherine Wise <catherine.wise@csiro.au> parents: 
16diff
changeset | 19 # urllib3.contrib.pyopenssl.inject_into_urllib3() | 
| 25 
18fe446f2d02
Tell requests about the new cert bundle. FFS.
 Catherine Wise <catherine.wise@csiro.au> parents: 
24diff
changeset | 20 # | 
| 26 
463b024b53ff
The path was wrong. UUUUGH.
 Catherine Wise <catherine.wise@csiro.au> parents: 
25diff
changeset | 21 os.environ["REQUESTS_CA_BUNDLE"] = "/etc/ssl/certs/ca-certificates.crt" | 
| 16 | 22 | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 23 CLIENT_KEY = "desktop" | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 24 CLIENT_SECRET = "cpU92F1PT7VOCANjSknuCDp4DrubmujoBaF6b0miz8OpKNokEbGMHCaSFK5/lISbBmaaGVCgeADI2A39F3Hkeg==" | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 25 CHUNK_SIZE = 2**20 # 1Mb | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 26 SAFE_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 27 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 28 def print_nice(elem, f, depth): | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 29 """Print the file name.""" | 
| 0 | 30 try: | 
| 31 f.write('\t'*depth + elem.name + " (" + str(len(elem.fragments)) + " parts)\n") | |
| 32 except AttributeError: | |
| 33 f.write('\t'*depth + elem.name + "\n") | |
| 34 for child in elem.children: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 35 print_nice(child, f, depth+1) | 
| 0 | 36 | 
| 37 def check_gzip(file_path): | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 38 """Check if file is gziped.""" | 
| 0 | 39 try: | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 40 temp = open(file_path, "U") | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 41 magic_check = temp.read(2) | 
| 0 | 42 temp.close() | 
| 43 if magic_check != util.gzip_magic: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 44 return (False, False) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 45 except Exception: | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 46 return (False, False) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 47 return (True, True) | 
| 0 | 48 | 
| 49 def ungzip(download, outputFile): | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 50 """Uncompress file.""" | 
| 0 | 51 is_gzipped, is_valid = check_gzip(download) | 
| 52 | |
| 53 if is_gzipped and not is_valid: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 54 print("File is compressed (gzip) but not valid.") | 
| 0 | 55 sys.exit(4) | 
| 56 elif is_gzipped and is_valid: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 57 # We need to uncompress the temp_name file, but BAM files must | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 58 # remain compressed in the BGZF format | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 59 file_handle, uncompressed = tempfile.mkstemp(prefix='data_id_upload_gunzip_', dir=os.path.dirname(outputFile), text=False ) | 
| 0 | 60 gzipped_file = gzip.GzipFile(download, 'rb') | 
| 61 while 1: | |
| 62 try: | |
| 63 chunk = gzipped_file.read(CHUNK_SIZE) | |
| 64 except IOError: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 65 os.close(file_handle) | 
| 0 | 66 os.remove(uncompressed) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 67 print('Problem decompressing gzipped data %s %s' % (download, outputFile)) | 
| 0 | 68 sys.exit(4) | 
| 69 if not chunk: | |
| 70 break | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 71 os.write(file_handle, chunk) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 72 os.close(file_handle) | 
| 0 | 73 gzipped_file.close() | 
| 74 | |
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 75 try: | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 76 shutil.copy(uncompressed, outputFile) | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 77 except shutil.Error: | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 78 pass # If the file is already in the right location, move along. | 
| 0 | 79 try: | 
| 80 os.remove(uncompressed) | |
| 81 os.remove(download) | |
| 82 except OSError: | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 83 pass | 
| 0 | 84 else: | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 85 try: | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 86 shutil.copy(download, outputFile) | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 87 except shutil.Error: | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 88 pass # If the file is already in the right location, move along. | 
| 0 | 89 | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 90 def construct_multi_filename(id, name, file_type): | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 91 """ Implementation of *Number of Output datasets cannot be determined until | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 92 tool run* from documentation_. | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 93 .. _documentation: http://wiki.galaxyproject.org/Admin/Tools/Multiple%20Output%20Files | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 94 From https://github.com/mdshw5/galaxy-json-data-source/blob/master/json_data_source.py | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 95 """ | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 96 filename = "%s_%s_%s_%s_%s" % ('primary', id, name, 'visible', file_type) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 97 return filename | 
| 0 | 98 | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 99 def metadata_to_json(dataset_id, filename, name, extension, ds_type='dataset', primary=False): | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 100 """ Return line separated JSON | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 101 From https://github.com/mdshw5/galaxy-json-data-source/blob/master/json_data_source.py | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 102 """ | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 103 ext = extension | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 104 if ext == 'fa': | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 105 ext = 'fasta' | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 106 meta_dict = dict(type=ds_type, | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 107 ext=ext, | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 108 filename=filename, | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 109 name=name, | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 110 metadata={}) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 111 if primary: | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 112 meta_dict['base_dataset_id'] = dataset_id | 
| 0 | 113 else: | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 114 meta_dict['dataset_id'] = dataset_id | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 115 return "%s\n" % json.dumps(meta_dict) | 
| 0 | 116 | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 117 def main(): | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 118 properties_file = sys.argv[1] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 119 json_params = None | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 120 metadata_path = None | 
| 14 | 121 all_params = None | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 122 with open(properties_file, 'r') as file_: | 
| 13 | 123 settings = file_.read() | 
| 124 all_params = json.loads(settings) | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 125 json_params = all_params.get("param_dict") | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 126 metadata_path = all_params["job_config"]["TOOL_PROVIDED_JOB_METADATA_FILE"] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 127 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 128 output_filename = json_params.get('output', None) | 
| 14 | 129 output_data = all_params.get('output_data') | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 130 extra_files_path, file_name, ext, out_data_name, hda_id, dataset_id = \ | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 131 operator.itemgetter('extra_files_path', 'file_name', 'ext', 'out_data_name', 'hda_id', 'dataset_id')(output_data[0]) | 
| 40 
5ea7590f7395
Try different path for extra files.
 Catherine Wise <catherine.wise@csiro.au> parents: 
39diff
changeset | 132 extra_files_path = json_params['__new_file_path__'] | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 133 | 
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 134 # datatypes_registry = Registry() | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 135 # datatypes_registry.load_datatypes( | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 136 # root_dir=all_params['job_config']['GALAXY_ROOT_DIR'], | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 137 # config=all_params['job_config']['GALAXY_DATATYPES_CONF_FILE'] | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 138 # ) | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 139 | 
| 15 | 140 url_params = urlparse.unquote(json_params['URL']).split(";") | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 141 if len(url_params) < 3: | 
| 15 | 142 print("The url we got back is malformed: "+ json_params['URL']) | 
| 0 | 143 sys.exit(5) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 144 short_url = url_params[0] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 145 username = url_params[1] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 146 password = url_params[2] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 147 if "/short" not in short_url: | 
| 15 | 148 print("The url we got back is malformed: " + json_params['URL']) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 149 sys.exit(5) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 150 kms_url = short_url.split("/short")[0] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 151 | 
| 29 | 152 tmp_dir = '/mnt/galaxy/tmp' | 
| 153 tmp = None | |
| 154 if os.path.exists(tmp_dir): | |
| 155 tmp = tmp_dir | |
| 156 | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 157 config = TrustStoreClient.Config( | 
| 29 | 158 None, kms_url, CLIENT_KEY, CLIENT_SECRET, tmpDir=tmp) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 159 truststore = TrustStoreClient.TrustStoreClient(headless=False, config=config) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 160 try: | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 161 truststore.authenticate(username, password) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 162 except TrustStoreClient.TrustStoreClientAuthenticationException as err: | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 163 print(err) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 164 sys.exit(5) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 165 truststore.getPrivateKey('privkey.pem') | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 166 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 167 path_texts = truststore.lengthenPath(short_url) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 168 if len(path_texts) < 2: | 
| 27 
901b359bb74a
We are getting closer.
 Catherine Wise <catherine.wise@csiro.au> parents: 
26diff
changeset | 169 print("The path we got was malformed: " + str(path_texts)) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 170 sys.exit(3) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 171 paths = path_texts[1:] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 172 store_id = path_texts[0] | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 173 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 174 store = truststore.getStoreByID(store_id) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 175 if store is None: | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 176 print("Coudn't find store with that ID, or don't have access.") | 
| 0 | 177 sys.exit(2) | 
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 178 root = truststore.listDirectory(store) | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 179 | 
| 14 | 180 first = True | 
| 181 | |
| 32 | 182 print("Preparing the following for downloading: " + str(paths)) | 
| 183 | |
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 184 # Empty parameter file. | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 185 open(file_name, 'w' ).close() | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 186 | 
| 29 | 187 if root is not None: | 
| 188 with open(metadata_path, 'wb') as metadata_file: | |
| 189 for path in paths: | |
| 190 locations = utils.Navigation.files_at_path(root, path) | |
| 33 | 191 if not locations or locations == []: | 
| 192 print("Path not found: " + path) | |
| 193 print("In root: " + str(root)) | |
| 194 else: | |
| 35 
d25b82c3276f
Print a little less :) mostly working now!!!
 Catherine Wise <catherine.wise@csiro.au> parents: 
33diff
changeset | 195 print("Downloading file..." + ", ".join([loc.name for loc in locations])) | 
| 29 | 196 for location in locations: | 
| 197 filename = "".join(c in SAFE_CHARS and c or '-' for c in location.name) | |
| 39 
624d8102cb75
Fix json output again.
 Catherine Wise <catherine.wise@csiro.au> parents: 
38diff
changeset | 198 extension = os.path.splitext(filename)[1].strip(".") | 
| 29 | 199 name = construct_multi_filename(hda_id, filename, extension) | 
| 200 target_output_filename = None | |
| 39 
624d8102cb75
Fix json output again.
 Catherine Wise <catherine.wise@csiro.au> parents: 
38diff
changeset | 201 data_type = "new_primary_dataset" | 
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 202 target_output_filename = os.path.normpath(os.path.join(extra_files_path, name)) | 
| 29 | 203 download = truststore.getFile(store, location) | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 204 primary = not first | 
| 29 | 205 if download is None: | 
| 206 print("File %s not found." % location.name) | |
| 207 sys.exit(4) | |
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 208 if first: | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 209 first = False | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 210 target_output_filename = file_name | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 211 data_type = "dataset" | 
| 29 | 212 ungzip(download, target_output_filename) | 
| 45 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 213 metadata_file.write( | 
| 
31838e0ecf45
Fixes.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
43diff
changeset | 214 metadata_to_json(dataset_id, target_output_filename, name, extension, data_type, primary=primary)) | 
| 43 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 215 # ext = sniff.handle_uploaded_dataset_file(target_output_filename, datatypes_registry, ext=ext) | 
| 
174df9815ecd
Fix data types.
 Wise, Catherine (Digital, Acton) <Catherine.Wise@csiro.au> parents: 
40diff
changeset | 216 # print("Guessed file type: " + ext) | 
| 29 | 217 else: | 
| 218 print("Store is damaged or we don't have sufficient access.") | |
| 219 sys.exit(4) | |
| 9 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 220 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 221 | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 222 if __name__ == '__main__': | 
| 
0f2a5664c9eb
Actually update downloader (probably not working).
 Catherine Wise <catherine.wise@csiro.au> parents: 
0diff
changeset | 223 main() | 
