def persist_uploads(params): """ Turn any uploads in the submitted form to persisted files. """ if 'files' in params: new_files = [] for upload_dataset in params['files']: f = upload_dataset['file_data'] if isinstance(f, FieldStorage): assert not isinstance(f.file, StringIO.StringIO) assert f.file.name != '<fdopen>' local_filename = util.mkstemp_ln(f.file.name, 'upload_file_data_') f.file.close() upload_dataset['file_data'] = dict( filename=f.filename, local_filename=local_filename) elif type(f) == dict and 'local_filename' not in f: raise Exception( 'Uploaded file was encoded in a way not understood by Galaxy.' ) if upload_dataset[ 'url_paste'] and upload_dataset['url_paste'].strip() != '': upload_dataset[ 'url_paste'], is_multi_byte = datatypes.sniff.stream_to_file( StringIO.StringIO(upload_dataset['url_paste']), prefix="strio_url_paste_") else: upload_dataset['url_paste'] = None new_files.append(upload_dataset) params['files'] = new_files return params
def persist_uploads(params, trans): """ Turn any uploads in the submitted form to persisted files. """ if 'files' in params: new_files = [] for upload_dataset in params['files']: f = upload_dataset['file_data'] if isinstance(f, cgi_FieldStorage): assert not isinstance(f.file, StringIO) assert f.file.name != '<fdopen>' local_filename = util.mkstemp_ln(f.file.name, 'upload_file_data_') f.file.close() upload_dataset['file_data'] = dict(filename=f.filename, local_filename=local_filename) elif type(f) == dict and 'local_filename' not in f: raise Exception('Uploaded file was encoded in a way not understood by Galaxy.') if 'url_paste' in upload_dataset and upload_dataset['url_paste'] and upload_dataset['url_paste'].strip() != '': upload_dataset['url_paste'] = datatypes.sniff.stream_to_file( StringIO(validate_url(upload_dataset['url_paste'], trans.app.config.fetch_url_whitelist_ips)), prefix="strio_url_paste_" ) else: upload_dataset['url_paste'] = None new_files.append(upload_dataset) params['files'] = new_files return params
def persist_uploads(params): """ Turn any uploads in the submitted form to persisted files. """ if "files" in params: new_files = [] for upload_dataset in params["files"]: f = upload_dataset["file_data"] if isinstance(f, FieldStorage): assert not isinstance(f.file, StringIO.StringIO) assert f.file.name != "<fdopen>" local_filename = util.mkstemp_ln(f.file.name, "upload_file_data_") f.file.close() upload_dataset["file_data"] = dict(filename=f.filename, local_filename=local_filename) elif type(f) == dict and "local_filename" not in f: raise Exception("Uploaded file was encoded in a way not understood by Galaxy.") if upload_dataset["url_paste"] and upload_dataset["url_paste"].strip() != "": upload_dataset["url_paste"], is_multi_byte = datatypes.sniff.stream_to_file( StringIO.StringIO(upload_dataset["url_paste"]), prefix="strio_url_paste_" ) else: upload_dataset["url_paste"] = None new_files.append(upload_dataset) params["files"] = new_files return params
def create_changeset_revision( self, trans, id, payload, **kwd ): """ POST /api/repositories/{encoded_repository_id}/changeset_revision Create a new tool shed repository commit - leaving PUT on parent resource open for updating meta-attributes of the repository (and Galaxy doesn't allow PUT multipart data anyway https://trello.com/c/CQwmCeG6). :param id: the encoded id of the Repository object The following parameters may be included in the payload. :param commit_message: hg commit message for update. """ # Example URL: http://localhost:9009/api/repositories/f9cad7b01a472135 rdah = attribute_handlers.RepositoryDependencyAttributeHandler( trans.app, unpopulate=False ) tdah = attribute_handlers.ToolDependencyAttributeHandler( trans.app, unpopulate=False ) repository = suc.get_repository_in_tool_shed( trans.app, id ) repo_dir = repository.repo_path( trans.app ) repo = hg_util.get_repo_for_repository( trans.app, repository=None, repo_path=repo_dir, create=False ) upload_point = commit_util.get_upload_point( repository, **kwd ) tip = repository.tip( trans.app ) file_data = payload.get('file') # Code stolen from gx's upload_common.py if isinstance( file_data, FieldStorage ): assert not isinstance( file_data.file, StringIO.StringIO ) assert file_data.file.name != '<fdopen>' local_filename = util.mkstemp_ln( file_data.file.name, 'upload_file_data_' ) file_data.file.close() file_data = dict( filename=file_data.filename, local_filename=local_filename ) elif type( file_data ) == dict and 'local_filename' not in file_data: raise Exception( 'Uploaded file was encoded in a way not understood.' ) commit_message = kwd.get( 'commit_message', 'Uploaded' ) uploaded_file = open(file_data['local_filename'], 'rb') uploaded_file_name = file_data['local_filename'] isgzip = False isbz2 = False isgzip = checkers.is_gzip( uploaded_file_name ) if not isgzip: isbz2 = checkers.is_bz2( uploaded_file_name ) if ( isgzip or isbz2 ): # Open for reading with transparent compression. tar = tarfile.open( uploaded_file_name, 'r:*' ) else: tar = tarfile.open( uploaded_file_name ) new_repo_alert = False remove_repo_files_not_in_tar = True ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ repository_content_util.upload_tar( trans, rdah, tdah, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) if ok: # Update the repository files for browsing. hg_util.update_repository( repo ) # Get the new repository tip. if tip == repository.tip( trans.app ): trans.response.status = 400 message = 'No changes to repository.' ok = False else: rmm = repository_metadata_manager.RepositoryMetadataManager( app=trans.app, user=trans.user, repository=repository ) status, error_message = \ rmm.set_repository_metadata_due_to_new_tip( trans.request.host, content_alert_str=content_alert_str, **kwd ) if error_message: ok = False trans.response.status = 500 message = error_message else: trans.response.status = 500 if not ok: return { "err_msg": message, "content_alert": content_alert_str } else: return { "message": message, "content_alert": content_alert_str }
def create_changeset_revision(self, trans, id, payload, **kwd): """ POST /api/repositories/{encoded_repository_id}/changeset_revision Create a new tool shed repository commit - leaving PUT on parent resource open for updating meta-attributes of the repository (and Galaxy doesn't allow PUT multipart data anyway https://trello.com/c/CQwmCeG6). :param id: the encoded id of the Repository object The following parameters may be included in the payload. :param commit_message: hg commit message for update. """ # Example URL: http://localhost:9009/api/repositories/f9cad7b01a472135 rdah = attribute_handlers.RepositoryDependencyAttributeHandler( trans.app, unpopulate=False) tdah = attribute_handlers.ToolDependencyAttributeHandler( trans.app, unpopulate=False) repository = suc.get_repository_in_tool_shed(trans.app, id) repo_dir = repository.repo_path(trans.app) repo = hg_util.get_repo_for_repository(trans.app, repository=None, repo_path=repo_dir, create=False) upload_point = commit_util.get_upload_point(repository, **kwd) tip = repository.tip(trans.app) file_data = payload.get('file') # Code stolen from gx's upload_common.py if isinstance(file_data, FieldStorage): assert not isinstance(file_data.file, StringIO.StringIO) assert file_data.file.name != '<fdopen>' local_filename = util.mkstemp_ln(file_data.file.name, 'upload_file_data_') file_data.file.close() file_data = dict(filename=file_data.filename, local_filename=local_filename) elif type(file_data) == dict and 'local_filename' not in file_data: raise Exception( 'Uploaded file was encoded in a way not understood.') commit_message = kwd.get('commit_message', 'Uploaded') uploaded_file = open(file_data['local_filename'], 'rb') uploaded_file_name = file_data['local_filename'] isgzip = False isbz2 = False isgzip = checkers.is_gzip(uploaded_file_name) if not isgzip: isbz2 = checkers.is_bz2(uploaded_file_name) if (isgzip or isbz2): # Open for reading with transparent compression. tar = tarfile.open(uploaded_file_name, 'r:*') else: tar = tarfile.open(uploaded_file_name) new_repo_alert = False remove_repo_files_not_in_tar = True ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ repository_content_util.upload_tar( trans, rdah, tdah, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) if ok: # Update the repository files for browsing. hg_util.update_repository(repo) # Get the new repository tip. if tip == repository.tip(trans.app): trans.response.status = 400 message = 'No changes to repository.' ok = False else: rmm = repository_metadata_manager.RepositoryMetadataManager( app=trans.app, user=trans.user, repository=repository) status, error_message = \ rmm.set_repository_metadata_due_to_new_tip( trans.request.host, content_alert_str=content_alert_str, **kwd ) if error_message: ok = False trans.response.status = 500 message = error_message else: trans.response.status = 500 if not ok: return {"err_msg": message, "content_alert": content_alert_str} else: return {"message": message, "content_alert": content_alert_str}