def post_ajax(): """ 实现所有表单里需要post_ajax的逻辑. 如果以后类型更多, 应该考虑把实际处理交给adapter, 每个工具的adapter去查看哪个配置选择的话需要新的哪个配置""" json_str = _f.request.form.get('data', '') if not json_str: return fail_json(error_string='wrong parameters') try: json_data = json.loads(json_str) except Exception: return fail_json(error_string='wrong json format') name = json_data.get('name', '') value = json_data.get('value', '') create_by_list = json_data.get('create_by_list', []) if not name or not value: return fail_json(error_string='wrong parameters') adapter_layers = [] for create_by in reversed(create_by_list): create_by_name = create_by.get('name', '') create_by_value = create_by.get('value', '') if not create_by_name or not create_by_value: return fail_json(error_string='wrong job adapter') else: adapter_layers.append((create_by_name, create_by_value)) adapter_layers.append((name, value)) adapter = get_adapter(adapter_layers) if adapter is None: return fail_json(error_string='wrong job Adapter') else: new_form_groups = fv_adapter.render_form(adapter.required, addition_info='create_by="%s"'%name, required=True) new_form_groups += fv_adapter.render_form(adapter.optional, addition_info='create_by="%s"'%name) new_js = fv_adapter.render_js(adapter.required) new_js += fv_adapter.render_js(adapter.optional) return success_json(data=new_form_groups, js=new_js)
def delete_file(): dir_name = secure_filename(_f.request.form.get('dir_name', '')) file_name = secure_filename(_f.request.form.get('file_name', '')) # 可以进一步限制dir_name在那四个里面 if not dir_name or not file_name: return fail_json(error_string='Wrong request parameters') else: try: _l.current_user.delete_user_file(dir_name, file_name) except Exception: return fail_json(error_string='Fail deleting file') else: return success_json()
def refresh_file_list(): # secure file name is very important dir_name = secure_filename(_f.request.form.get('dir_name', '')) if not dir_name: return fail_json(error_string='no dir_name specified') return success_json(file_list=_l.current_user.get_file_info_list(dir_name))
def pre_ajax(): """ 实现所有表单里需要pre_ajax的逻辑. 如果以后类型更多, 应该考虑把实际处理交给adapter, 每个工具的adapter去查看哪个配置选择的话需要新的哪个配置或者需要做什么, 现在逻辑感觉还不够清晰""" name = _f.request.form.get('name', None) if name == 'input_proto': input_proto_list = ['shared/' + x for x in get_shared_file_name_list('*.prototxt')] input_proto_list += ['upload_prototxt/' + x for x in _l.current_user.get_file_name_list('upload_prototxt')] input_proto_list += ['generated_prototxt/' + x for x in _l.current_user.get_file_name_list('generated_prototxt')] return success_json(data=input_proto_list) elif name == 'input_caffemodel': input_caffemodel_list = ['shared/' + x for x in get_shared_file_name_list('*.caffemodel')] input_caffemodel_list += ['upload_caffemodel/' + x for x in _l.current_user.get_file_name_list('upload_caffemodel')] input_caffemodel_list += ['generated_caffemodel/' + x for x in _l.current_user.get_file_name_list('generated_caffemodel')] return success_json(data=input_caffemodel_list) elif name == 'input_npz': input_npz_list = ['shared/' + x for x in get_shared_file_name_list('*.npz')] # fixme: 文件管理这一块可能还需要大改 input_npz_list += ['upload_caffemodel/' + x for x in _l.current_user.get_file_name_list('upload_caffemodel') if x.endswith('.npz')] return success_json(data=input_npz_list) else: # 以后如果要加更多还是放在adapter里, 其实还没有完全设计好... return fail_json()
def job_stop(): job_id = _f.request.form.get('job') if job_id is None: return fail_json(error_string="Fail stopping job: did not specify job id") job = Job.get_job_of_user_id(job_id, _l.current_user.user_id) if job is None: return fail_json(error_string="Fail stopping job: wrong job stopping request") # 检查Job status if not job.job_status == 'running': return fail_json(error_string="Fail stopping job: this job is not running") try: kill_running_job(job) except Exception as e: print(e) return fail_json(error_string='Fail stopping job: please try refreshing the page') else: return success_json()
def job_run(): job_id = _f.request.form.get('job') if job_id is None: return fail_json(error_string="Fail running job: did not specify job id") job = Job.get_job_of_user_id(job_id, _l.current_user.user_id) if job is None: return fail_json(error_string="Fail running job: wrong job running request") # 检查job状态 if job.job_status == 'running': # fixme: 如果出现at-exit没有执行的情况怎么办... return fail_json(error_string="Fail running job: this job is already running") # 新建子进程运行工具 # try: # job_runner = JobRunner(job, db) # except ExpectedException as e: # # fixme: 有什么情况会导致新建成功但无法开始运行, 比如达到同时运行的job上限了? # return fail_json(error_string='运行Job失败: ' + repr(e)) # except Exception as e: # print(e) # return fail_json(error_string='运行Job失败') # fixme: 这个run也在里面raise? 应该不用吧 user = User.get(_l.current_user.user_id) if user.history_success_job_num >= user.exist_job_limit: return fail_json(error_string="Exceed the upper limit of successfully job running.") runner = subprocess.Popen(['python', os.path.join(app_root, 'run_job.py'), str(job.job_id)], close_fds=True) # close_fds argument is supposed to be enough try: _, status = os.waitpid(runner.pid, 0) if os.WIFEXITED(status): exit_status = os.WEXITSTATUS(status) if exit_status != 0: return fail_json(error_string="Fail running job") except Exception as e: print(e) raise # status = job_runner.run() # run() method will create a new subprocess and run it # a sub process forked from here will have context, and system.exit will be catch... # so cannot exit from the second process return success_json()
def upload(): #user = _l.current_user upload_file = _f.request.files['file'] upload_file_type = _f.request.form.get('filetype', None) if not upload_file: return fail_json(error_string='No uploaded file') elif not upload_file_type: return fail_json(error_string='Did not specify file type') elif not _u.allowed_filetype(upload_file_type): return fail_json(error_string=('The file type is not supported: <%s>' % upload_file_type)) elif not _u.allowed_file(upload_file_type, upload_file.filename): return fail_json(error_string=('The filename is not supported: "%s", ' 'should be *.%s') % (upload_file.filename, upload_file_type)) else: upload_filename = _u.get_upload_filename(_l.current_user.user_id, upload_file.filename, upload_file_type) upload_file.save( os.path.join(here, app.config['UPLOAD_DIR'], upload_filename)) return success_json(filename=upload_filename)