From 0488972359ffb66d1ac11be5e93649fd79e5be2e Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Fri, 6 Mar 2020 10:48:28 +0100 Subject: [PATCH 1/4] Set status code even if creating a html message body That makes it way easier to understand that for instance an exported history has not been shared and so the archive can't be downloaded. --- lib/galaxy/web/framework/webapp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/galaxy/web/framework/webapp.py b/lib/galaxy/web/framework/webapp.py index 273eb40cc69..d77b230a275 100644 --- a/lib/galaxy/web/framework/webapp.py +++ b/lib/galaxy/web/framework/webapp.py @@ -106,6 +106,7 @@ class WebApplication(base.WebApplication): if isinstance(e, MessageException): # In the case of a controller exception, sanitize to make sure # unsafe html input isn't reflected back to the user + trans.response.status = e.status_code return trans.show_message(sanitize_html(e.err_msg), e.type) def make_body_iterable(self, trans, body): From 2d8ab3bc6b6da44535bb9faaf63efd2d1cdfe2d3 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Fri, 6 Mar 2020 10:51:15 +0100 Subject: [PATCH 2/4] Use exit to determine failure, let exceptions bubble up --- .../imp_exp/imp_history_from_archive.xml | 5 ++- .../tools/imp_exp/unpack_tar_gz_archive.py | 35 ++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/galaxy/tools/imp_exp/imp_history_from_archive.xml b/lib/galaxy/tools/imp_exp/imp_history_from_archive.xml index 94e165c7d5a..c749b6115f6 100644 --- a/lib/galaxy/tools/imp_exp/imp_history_from_archive.xml +++ b/lib/galaxy/tools/imp_exp/imp_history_from_archive.xml @@ -1,6 +1,9 @@ - + + + requests + #from base64 import b64encode# python '$__tool_directory__/unpack_tar_gz_archive.py' '${ b64encode(str($__ARCHIVE_SOURCE__).encode('utf-8')).decode('utf-8')}' diff --git a/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py b/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py index f1a8d1daf4b..5611fe6d2be 100644 --- a/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py +++ b/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py @@ -10,7 +10,6 @@ from __future__ import print_function import math import optparse import os -import sys import tarfile import tempfile from base64 import b64decode @@ -26,22 +25,19 @@ def url_to_file(url, dest_file): """ Transfer a file from a remote URL to a temporary file. """ - try: - url_reader = requests.get(url, stream=True) - CHUNK = 10 * 1024 # 10k - total = 0 - fp = open(dest_file, 'wb') - for chunk in url_reader.iter_content(chunk_size=CHUNK): - if chunk: - fp.write(chunk) - total += CHUNK - if total > MAX_SIZE: - break - fp.close() - return dest_file - except Exception as e: - print("Exception getting file from URL: %s" % e, file=sys.stderr) - return None + url_reader = requests.get(url, stream=True) + assert url_reader.ok, "History import failed, server returned '%s'" % url_reader.reason + CHUNK = 10 * 1024 # 10k + total = 0 + fp = open(dest_file, 'wb') + for chunk in url_reader.iter_content(chunk_size=CHUNK): + if chunk: + fp.write(chunk) + total += CHUNK + if total > MAX_SIZE: + break + fp.close() + return dest_file def check_archive(archive_file, dest_dir): @@ -96,7 +92,4 @@ if __name__ == "__main__": parser.add_option('-F', '--file', dest='is_file', action="store_true", help='Source is a file.') parser.add_option('-e', '--encoded', dest='is_b64encoded', action="store_true", default=False, help='Source and destination dir values are base64 encoded.') (options, args) = parser.parse_args() - try: - main(options, args) - except Exception as e: - print("Error unpacking tar/gz archive: %s" % e, file=sys.stderr) + main(options, args) From 3d5ec396db64cc895185a483093f3925c3acfe88 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Fri, 6 Mar 2020 11:01:34 +0100 Subject: [PATCH 3/4] Fix history export for outputs_to_working_directory --- lib/galaxy/jobs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/jobs/__init__.py b/lib/galaxy/jobs/__init__.py index f485b52e88b..71dd60e7852 100644 --- a/lib/galaxy/jobs/__init__.py +++ b/lib/galaxy/jobs/__init__.py @@ -1939,7 +1939,7 @@ class JobWrapper(HasResourceParameters): self.output_paths = [t[2] for t in results] self.output_hdas_and_paths = dict([(t[0], t[1:]) for t in results]) if special: - false_path = dataset_path_rewriter.rewrite_dataset_path(special.dataset, 'output') + false_path = dataset_path_rewriter.rewrite_dataset_path(special, 'output') dsp = DatasetPath(special.dataset.id, special.dataset.file_name, false_path) self.output_paths.append(dsp) return self.output_paths From d4e6ebe9f722709e1d47b949205878e8b969ae67 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Fri, 6 Mar 2020 16:46:47 +0100 Subject: [PATCH 4/4] Use with statement --- lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py b/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py index 5611fe6d2be..f3fad868169 100644 --- a/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py +++ b/lib/galaxy/tools/imp_exp/unpack_tar_gz_archive.py @@ -29,14 +29,13 @@ def url_to_file(url, dest_file): assert url_reader.ok, "History import failed, server returned '%s'" % url_reader.reason CHUNK = 10 * 1024 # 10k total = 0 - fp = open(dest_file, 'wb') - for chunk in url_reader.iter_content(chunk_size=CHUNK): - if chunk: - fp.write(chunk) - total += CHUNK - if total > MAX_SIZE: - break - fp.close() + with open(dest_file, 'wb') as fp: + for chunk in url_reader.iter_content(chunk_size=CHUNK): + if chunk: + fp.write(chunk) + total += CHUNK + if total > MAX_SIZE: + break return dest_file