if is_upload: if status == ExternalProcStatus.NOT_FOUND: inp_format = request.vars.inputFormat or 'nexus' inp_format = inp_format.lower() if inp_format not in input_choices: raise HTTP( 400, 'inputFormat should be one of: "{c}"'.format( c='", "'.join(input_choices))) if inp_format == 'newick': inp_format = 'relaxedphyliptree' if inp_format == 'nexml': shutil.copyfile(INPUT_FILEPATH, NEXML_FILEPATH) else: try: try: exe_path = get_conf(request).get("external", "2nexml") except: _LOG.warn( "Config does not have external/2nexml setting") raise assert (os.path.exists(exe_path)) except: response.view = 'generic.json' return { 'hb': exe_path } _LOG.warn("Could not find the 2nexml executable") raise HTTP( 501, T("Server is not configured to allow 2nexml conversion" ))
def to_nexml(): if len(request.args) == 1: _LOG = get_logger(request, 'study') try: field = db['study_file']['file'] except Error: sys.stderr.write('odd\n') raise HTTP(404) name = request.args[-1] sys.stderr.write('looking for file "' + name + '"\n') try: ext_proc_dir = get_external_proc_dir_for_upload(request, db, name) except ValueError: raise HTTP(404) if ext_proc_dir is None: raise HTTP(404) #@TEMPORARY could be refactored into a create_ext_proc_subdir() call to_nexml_dir = os.path.join(ext_proc_dir, '2nexml') if not os.path.exists(to_nexml_dir): os.makedirs(to_nexml_dir) _LOG.info('Created directory "%s"' % to_nexml_dir) block = True timeout_duration = 0.1 #@TEMPORARY should not be hard coded out_filename = 'out.xml' err_filename = 'err.txt' #@TEMPORARY could be refactored into a launch_or_get_status() call status = invoc_status(request, to_nexml_dir) launched_this_call = False if status == ExternalProcStatus.NOT_FOUND: try: try: exe_path = get_conf(request).get("external", "2nexml") except: _LOG.warn("Config does not have external/2nexml setting") raise assert (os.path.exists(exe_path)) except: _LOG.warn("Could not find the 2nexml executable") raise HTTP( 501, T("Server is not configured to allow 2nexml conversion")) try: (filename, upload_stream) = field.retrieve(name) except IOError: sys.stderr.write('not found\n') raise HTTP(404) do_ext_proc_launch(request, to_nexml_dir, [exe_path, 'in.nex'], out_filename, err_filename, [('in.nex', upload_stream)], wait=block) if not block: time.sleep(timeout_duration) status = invoc_status(request, to_nexml_dir) assert (status != ExternalProcStatus.NOT_FOUND) launched_this_call = True if status == ExternalProcStatus.RUNNING: if not launched_this_call: time.sleep(timeout_duration) status = invoc_status(request, to_nexml_dir) if status == ExternalProcStatus.RUNNING: return HTTP(102, T("Process still running")) #@TEMPORARY /end of potential launch_or_get_status call... if status == ExternalProcStatus.FAILED: try: err_file = os.path.join(to_nexml_dir, err_filename) err_content = 'Error message:\n ' + open(err_file, 'rU').read() except: err_content = '' response.headers['Content-Type'] = 'text/xml' raise HTTP(501, T("Conversion to NeXML failed.\n" + err_content)) output = os.path.join(to_nexml_dir, 'out.xml') response.headers['Content-Type'] = 'text/xml' return open(output, 'rU').read() else: raise HTTP(301) return response.download(request, db)
def to_nexml(): if len(request.args) == 1: _LOG = get_logger(request, 'study') try: field = db['study_file']['file'] except Error: sys.stderr.write('odd\n') raise HTTP(404) name = request.args[-1] sys.stderr.write('looking for file "' + name + '"\n') try: ext_proc_dir = get_external_proc_dir_for_upload(request, db, name) except ValueError: raise HTTP(404) if ext_proc_dir is None: raise HTTP(404) #@TEMPORARY could be refactored into a create_ext_proc_subdir() call to_nexml_dir = os.path.join(ext_proc_dir, '2nexml') if not os.path.exists(to_nexml_dir): os.makedirs(to_nexml_dir) _LOG.info('Created directory "%s"' % to_nexml_dir) block = True timeout_duration = 0.1 #@TEMPORARY should not be hard coded out_filename = 'out.xml' err_filename = 'err.txt' #@TEMPORARY could be refactored into a launch_or_get_status() call status = invoc_status(request, to_nexml_dir) launched_this_call = False if status == ExternalProcStatus.NOT_FOUND: try: try: exe_path = get_conf(request).get("external", "2nexml") except: _LOG.warn("Config does not have external/2nexml setting") raise assert(os.path.exists(exe_path)) except: _LOG.warn("Could not find the 2nexml executable") raise HTTP(501, T("Server is not configured to allow 2nexml conversion")) try: (filename, upload_stream) = field.retrieve(name) except IOError: sys.stderr.write('not found\n') raise HTTP(404) do_ext_proc_launch(request, to_nexml_dir, [exe_path, 'in.nex'], out_filename, err_filename, [('in.nex', upload_stream)], wait=block) if not block: time.sleep(timeout_duration) status = invoc_status(request, to_nexml_dir) assert(status != ExternalProcStatus.NOT_FOUND) launched_this_call = True if status == ExternalProcStatus.RUNNING: if not launched_this_call: time.sleep(timeout_duration) status = invoc_status(request, to_nexml_dir) if status == ExternalProcStatus.RUNNING: return HTTP(102, T("Process still running")) #@TEMPORARY /end of potential launch_or_get_status call... if status == ExternalProcStatus.FAILED: try: err_file = os.path.join(to_nexml_dir, err_filename) err_content = 'Error message:\n ' + open(err_file, 'rU').read() except: err_content = '' response.headers['Content-Type'] = 'text/xml' raise HTTP(501, T("Conversion to NeXML failed.\n" + err_content)) output = os.path.join(to_nexml_dir, 'out.xml') response.headers['Content-Type'] = 'text/xml' return open(output, 'rU').read() else: raise HTTP(301) return response.download(request, db)
status = invoc_status(request, working_dir) launched_this_call = False if is_upload: if status == ExternalProcStatus.NOT_FOUND: inp_format = request.vars.inputFormat or 'nexus' inp_format = inp_format.lower() if inp_format not in input_choices: raise HTTP(400, 'inputFormat should be one of: "{c}"'.format(c='", "'.join(input_choices))) if inp_format == 'newick': inp_format = 'relaxedphyliptree' if inp_format == 'nexml': shutil.copyfile(INPUT_FILEPATH, NEXML_FILEPATH) else: try: try: exe_path = get_conf(request).get("external", "2nexml") except: _LOG.warn("Config does not have external/2nexml setting") raise assert(os.path.exists(exe_path)) except: response.view = 'generic.json'; return {'hb':exe_path} _LOG.warn("Could not find the 2nexml executable") raise HTTP(501, T("Server is not configured to allow 2nexml conversion")) invoc = [exe_path, '-f{f}'.format(f=inp_format), ] if inp_format == 'relaxedphyliptree': invoc.extend(['-X', '-x']) if idPrefix: invoc.append('-t{u}'.format(u=idPrefix)) else: invoc.append('-g')