def _create_zip(sim, want_python, out_dir): """Zip up the json file and its dependencies Args: sim (req): simulation want_python (bool): include template's python source? out_dir (py.path): where to write to Returns: py.path.Local: zip file name """ path = out_dir.join(sim.id + '.zip') data = simulation_db.open_json_file(sim.type, sid=sim.id) simulation_db.update_rsmanifest(data) data.pkdel('report') files = sim_data.get_class(data).lib_files_for_export(data) if want_python: files.append(_python(data)) with zipfile.ZipFile( str(path), mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True, ) as z: for f in files: z.write(str(f), f.basename) z.writestr( simulation_db.SIMULATION_DATA_FILE, pkjson.dump_pretty(data, pretty=True), ) return path, data
def api_copyNonSessionSimulation(): req = _json_input() sim_type = req['simulationType'] global_path = simulation_db.find_global_simulation(sim_type, req['simulationId']) if global_path: data = simulation_db.open_json_file( sim_type, os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE), ) data['models']['simulation']['isExample'] = False data['models']['simulation']['outOfSessionSimulationId'] = req[ 'simulationId'] res = _save_new_and_reply(data) target = simulation_db.simulation_dir(sim_type, simulation_db.parse_sid(data)) template_common.copy_lib_files( data, py.path.local(os.path.dirname(global_path)).join('lib'), target.join('../lib'), ) template = sirepo.template.import_module(data) if hasattr(template, 'copy_related_files'): template.copy_related_files(data, global_path, str(target)) return res werkzeug.exceptions.abort(404)
def _create_zip(sim_type, sim_id, want_python): """Zip up the json file and its dependencies Args: sim_type (str): simulation type sim_id (str): simulation id want_python (bool): include template's python source? Returns: py.path.Local: zip file name """ from pykern import pkio from sirepo import simulation_db from sirepo.template import template_common #TODO(robnagler) need a lock with pkio.save_chdir(simulation_db.tmp_dir()): res = py.path.local(sim_id + '.zip') data = simulation_db.open_json_file(sim_type, sid=sim_id) if 'report' in data: del data['report'] files = template_common.lib_files(data) files.insert(0, simulation_db.sim_data_file(data.simulationType, sim_id)) if want_python: files.append(_python(data)) with zipfile.ZipFile( str(res), mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True, ) as z: for f in files: z.write(str(f), f.basename) return res, data
def api_copyNonSessionSimulation(): req = http_request.parse_json() sim_type = req['simulationType'] src = py.path.local( simulation_db.find_global_simulation( sim_type, req['simulationId'], checked=True, )) data = simulation_db.open_json_file( sim_type, src.join(simulation_db.SIMULATION_DATA_FILE), ) if 'report' in data: del data['report'] data['models']['simulation']['isExample'] = False data['models']['simulation']['outOfSessionSimulationId'] = req[ 'simulationId'] res = _save_new_and_reply(data) target = simulation_db.simulation_dir(sim_type, simulation_db.parse_sid(data)) template_common.copy_lib_files( data, simulation_db.lib_dir_from_sim_dir(src), simulation_db.lib_dir_from_sim_dir(target), ) template = sirepo.template.import_module(data) if hasattr(template, 'copy_related_files'): template.copy_related_files(data, str(src), str(target)) return res
def _create_zip(sim_type, sim_id, want_python): """Zip up the json file and its dependencies Args: sim_type (str): simulation type sim_id (str): simulation id want_python (bool): include template's python source? Returns: py.path.Local: zip file name """ from pykern import pkio from sirepo import simulation_db from sirepo.template import template_common #TODO(robnagler) need a lock with pkio.save_chdir(simulation_db.tmp_dir()): res = py.path.local(sim_id + '.zip') data = simulation_db.open_json_file(sim_type, sid=sim_id) files = template_common.lib_files(data) files.insert(0, simulation_db.sim_data_file(data.simulationType, sim_id)) if want_python: files.append(_python(data)) with zipfile.ZipFile( str(res), mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True, ) as z: for f in files: z.write(str(f), f.basename) return res, data
def api_copyNonSessionSimulation(): req = http_request.parse_post(id=True, template=True) simulation_db.verify_app_directory(req.type) src = pkio.py_path( simulation_db.find_global_simulation( req.type, req.id, checked=True, ), ) data = simulation_db.open_json_file( req.type, src.join(simulation_db.SIMULATION_DATA_FILE), ) data.pkdel('report') data.models.simulation.isExample = False data.models.simulation.outOfSessionSimulationId = req.id res = _save_new_and_reply(data) sirepo.sim_data.get_class(req.type).lib_files_from_other_user( data, simulation_db.lib_dir_from_sim_dir(src), ) target = simulation_db.simulation_dir(req.type, data.models.simulation.simulationId) #TODO(robnagler) does not work, supervisor needs to be notified to # copy the simulation state. # if hasattr(req.template, 'copy_related_files'): # req.template.copy_related_files(data, str(src), str(target)) return res
def api_blueskyAuth(): from sirepo import bluesky req = _json_input() bluesky.auth_login(req) return _json_response_ok(dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def app_new_simulation(): new_simulation_data = _json_input() simulation_type = new_simulation_data['simulationType'] data = simulation_db.open_json_file( simulation_type, simulation_db.STATIC_FOLDER.join('json', '{}-default{}'.format(simulation_type, simulation_db.JSON_SUFFIX)), ) data['models']['simulation']['name'] = new_simulation_data['name'] sirepo.template.import_module(simulation_type).new_simulation(data, new_simulation_data) return _save_new_and_reply(simulation_type, data)
def app_new_simulation(): new_simulation_data = _parse_data_input() sim_type = new_simulation_data['simulationType'] data = simulation_db.open_json_file( sim_type, simulation_db.STATIC_FOLDER.join('json', '{}-default{}'.format(sim_type, simulation_db.JSON_SUFFIX)), ) data['models']['simulation']['name'] = new_simulation_data['name'] data['models']['simulation']['folder'] = new_simulation_data['folder'] sirepo.template.import_module(data).new_simulation(data, new_simulation_data) return _save_new_and_reply(sim_type, data)
def app_copy_nonsession_simulation(): req = _json_input() sim_type = req['simulationType'] global_path = simulation_db.find_global_simulation(sim_type, req['simulationId']) if global_path: data = simulation_db.open_json_file(sim_type, os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE)) data['models']['simulation']['isExample'] = False data['models']['simulation']['outOfSessionSimulationId'] = req['simulationId'] res = _save_new_and_reply(sim_type, data) sirepo.template.import_module(data).copy_related_files(data, global_path, str(simulation_db.simulation_dir(sim_type, simulation_db.parse_sid(data)))) return res werkzeug.exceptions.abort(404)
def api_blueskyAuth(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) cookie.set_user(simulation_db.uid_from_dir_name(path)) return http_reply.gen_json_ok(dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def api_blueskyAuth(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) cookie.set_user(simulation_db.uid_from_dir_name(path)) return http_reply.gen_json_ok( dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def app_python_source(simulation_type, simulation_id): data = simulation_db.open_json_file(simulation_type, sid=simulation_id) template = sirepo.template.import_module(simulation_type) # ensure the whole source gets generated, not up to the last watchpoint report last_watchpoint = None if 'beamline' in data['models']: for item in reversed(data['models']['beamline']): if item['type'] == 'watch': last_watchpoint = 'watchpointReport{}'.format(item['id']) break if last_watchpoint: data['report'] = last_watchpoint return flask.Response( '{}{}'.format( template.generate_parameters_file(data, _schema_cache(simulation_type)), template.run_all_text()), mimetype='text/plain', )
def api_authBlueskyLogin(): req = sirepo.http_request.parse_post(id=True) auth_hash(req.req_data, verify=True) path = simulation_db.find_global_simulation( req.type, req.id, checked=True, ) sirepo.auth.login( this_module, uid=simulation_db.uid_from_dir_name(path), # do not supply sim_type (see auth.login) ) return sirepo.http_reply.gen_json_ok( PKDict( data=simulation_db.open_json_file(req.type, sid=req.id), schema=simulation_db.get_schema(req.type), ), )
def app_copy_simulation(): """Takes the specified simulation and returns a newly named copy with the suffix (copy X)""" req = _json_input() simulation_type = req['simulationType'] data = simulation_db.open_json_file(simulation_type, sid=req['simulationId']) base_name = data['models']['simulation']['name'] names = simulation_db.iterate_simulation_datafiles(simulation_type, _simulation_name) count = 0 while True: count += 1 name = base_name + ' (copy{})'.format(' {}'.format(count) if count > 1 else '') if name in names and count < 100: continue break data['models']['simulation']['name'] = name data['models']['simulation']['isExample'] = '' data['models']['simulation']['outOfSessionSimulationId'] = '' return _save_new_and_reply(simulation_type, data)
def auth_login(req): from sirepo import server if cfg.auth_secret: auth_hash(req, verify=True) # DEPRECATED elif not server.cfg.enable_bluesky: util.raise_not_found('bluesky is not enabled') sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) server.session_user(simulation_db.uid_from_dir_name(path)) return pkcollections.Dict( status='OK', data=simulation_db.open_json_file(sim_type, sid=sid), )
def api_authBlueskyLogin(): req = http_request.parse_json() auth_hash(req, verify=True) sid = req.simulationId sim_type = req.simulationType path = simulation_db.find_global_simulation( sim_type, sid, checked=True, ) r = auth.login( this_module, uid=simulation_db.uid_from_dir_name(path), ) if r: return r return http_reply.gen_json_ok(dict( data=simulation_db.open_json_file(req.simulationType, sid=req.simulationId), schema=simulation_db.get_schema(req.simulationType), ))
def app_copy_simulation(): """Takes the specified simulation and returns a newly named copy with the suffix (copy X)""" req = _json_input() simulation_type = req['simulationType'] data = simulation_db.open_json_file(simulation_type, sid=req['simulationId']) base_name = data['models']['simulation']['name'] names = simulation_db.iterate_simulation_datafiles(simulation_type, _simulation_name) count = 0 while True: count += 1 name = base_name + ' (copy{})'.format( ' {}'.format(count) if count > 1 else '') if name in names and count < 100: continue break data['models']['simulation']['name'] = name data['models']['simulation']['isExample'] = '' data['models']['simulation']['outOfSessionSimulationId'] = '' return _save_new_and_reply(simulation_type, data)
def api_blueskyAuth(): if not cfg.enable_bluesky: return _json_response({ 'status': 'error', 'error': 'bluesky auth is not enabled', }) req = _json_input() sim_id = req.simulationId sim_type = req.simulationType global_path = simulation_db.find_global_simulation(sim_type, sim_id) if global_path: m = re.search('/user/(.+)/{}/{}$'.format(sim_type, sim_id), global_path) assert m, 'global_path user parse failed: {}'.format(global_path) session_user(m.group(1)) return _json_response({ 'status': 'OK', 'data': simulation_db.open_json_file(sim_type, sid=sim_id), }) werkzeug.exceptions.abort(404)
def app_run_status(): data = _json_input() sid = simulation_db.parse_sid(data) simulation_type = data['simulationType'] template = sirepo.template.import_module(simulation_type) run_dir = simulation_db.simulation_run_dir(data) if cfg.job_queue.is_running(sid): completion = template.background_percent_complete(data, run_dir, True) state = 'running' else: data = simulation_db.open_json_file(simulation_type, sid=sid) state = data['models']['simulationStatus']['state'] completion = template.background_percent_complete(data, run_dir, False) if state == 'running': if completion['frame_count'] == completion['total_frames']: state = 'completed' else: state = 'canceled' data['models']['simulationStatus']['state'] = state simulation_db.save_simulation_json(data['simulationType'], data) frame_id = '' elapsed_time = '' if 'last_update_time' in completion: frame_id = completion['last_update_time'] elapsed_time = int(frame_id) - int( data['models']['simulationStatus']['startTime']) return flask.jsonify({ 'state': state, 'percentComplete': completion['percent_complete'], 'frameCount': completion['frame_count'], 'totalFrames': completion['total_frames'], 'frameId': frame_id, 'elapsedTime': elapsed_time, })
def app_run_status(): data = _json_input() sid = simulation_db.parse_sid(data) simulation_type = data['simulationType'] template = sirepo.template.import_module(simulation_type) run_dir = simulation_db.simulation_run_dir(data) if cfg.job_queue.is_running(sid): completion = template.background_percent_complete(data, run_dir, True) state = 'running' else: data = simulation_db.open_json_file(simulation_type, sid=sid) state = data['models']['simulationStatus']['state'] completion = template.background_percent_complete(data, run_dir, False) if state == 'running': if completion['frame_count'] == completion['total_frames']: state = 'completed' else: state = 'canceled' data['models']['simulationStatus']['state'] = state simulation_db.save_simulation_json(data['simulationType'], data) frame_id = '' elapsed_time = '' if 'last_update_time' in completion: frame_id = completion['last_update_time'] elapsed_time = int(frame_id) - int(data['models']['simulationStatus']['startTime']) return flask.jsonify({ 'state': state, 'percentComplete': completion['percent_complete'], 'frameCount': completion['frame_count'], 'totalFrames': completion['total_frames'], 'frameId': frame_id, 'elapsedTime': elapsed_time, })
def app_simulation_data(simulation_type, simulation_id): response = flask.jsonify(simulation_db.open_json_file(simulation_type, sid=simulation_id)) _no_cache(response) return response
def app_simulation_data(simulation_type, simulation_id): response = flask.jsonify( simulation_db.open_json_file(simulation_type, sid=simulation_id)) _no_cache(response) return response