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 test_auth_login(): from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq from sirepo import sr_unit fc = sr_unit.flask_client(cfg=dict( SIREPO_BLUESKY_AUTH_SECRET= '3SExmbOzn1WeoCWeJxekaE6bMDUj034Pu5az1hLNnvENyvL1FAJ1q3eowwODoa3f', ), ) from sirepo import simulation_db from sirepo import bluesky fc.get('/srw') data = fc.sr_post( 'listSimulations', { 'simulationType': 'srw', 'search': { 'simulationName': 'Bending Magnet Radiation' } }, ) data = data[0].simulation req = pkcollections.Dict( simulationType='srw', simulationId=data.simulationId, ) bluesky.auth_hash(req) resp = fc.sr_post('blueskyAuth', req) pkeq('ok', resp['state']) pkeq(req.simulationId, simulation_db.parse_sid(resp['data'])) pkeq('srw', resp['schema']['simulationType'])
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 app_run_cancel(): data = _json_input() data['models']['simulationStatus']['state'] = 'canceled' simulation_type = data['simulationType'] simulation_db.save_simulation_json(simulation_type, data) cfg.job_queue.kill(simulation_db.parse_sid(data)) # the last frame file may not be finished, remove it t = sirepo.template.import_module(simulation_type) t.remove_last_frame(simulation_db.simulation_run_dir(data)) return '{}'
def get_animation_name(data): if data['modelName'].startswith('dicomAnimation'): return 'dicomAnimation' if data['modelName'] == 'dicomDose': # if the doseCalculation has been run, use that directory for work # otherwise, it is an imported dose file if simulation_db.simulation_dir(SIM_TYPE, simulation_db.parse_sid( data)).join('doseCalculation').exists(): return 'doseCalculation' return 'dicomAnimation' return data['modelName']
def app_run(): data = _json_input() sid = simulation_db.parse_sid(data) err = _start_simulation(data).run_and_read() run_dir = simulation_db.simulation_run_dir(data) if err: pkdp('error: sid={}, dir={}, out={}', sid, run_dir, err) return flask.jsonify({ 'error': _error_text(err), 'simulationId': sid, }) return pkio.read_text( run_dir.join('out{}'.format(simulation_db.JSON_SUFFIX)))
def app_run_background(): data = _json_input() sid = simulation_db.parse_sid(data) #TODO(robnagler) race condition. Need to lock the simulation if cfg.job_queue.is_running(sid): #TODO(robnagler) return error to user if in different window pkdp('ignoring second call to runBackground: {}'.format(sid)) return '{}' status = data['models']['simulationStatus'] status['state'] = 'running' status['startTime'] = int(time.time()) _start_simulation(data, run_async=True) return flask.jsonify({ 'state': status['state'], 'startTime': status['startTime'], })
def _start_simulation(data, run_async=False): """Setup and start the simulation. Args: data (dict): app data run_async (bool): run-background or run Returns: object: _Command or daemon instance """ run_dir = simulation_db.simulation_run_dir(data, remove_dir=True) pkio.mkdir_parent(run_dir) #TODO(robnagler) create a lock_dir -- what node/pid/thread to use? # probably can only do with celery. simulation_type = data['simulationType'] sid = simulation_db.parse_sid(data) data = simulation_db.fixup_old_data(simulation_type, data) assert simulation_type in simulation_db.APP_NAMES, \ '{}: invalid simulation type'.format(simulation_type) template = sirepo.template.import_module(simulation_type) for d in simulation_db.simulation_dir( simulation_type, sid), simulation_db.simulation_lib_dir(simulation_type): for f in glob.glob(str(d.join('*.*'))): if os.path.isfile(f): py.path.local(f).copy(run_dir) template.prepare_aux_files(run_dir, data) simulation_db.save_simulation_json(simulation_type, data) with open(str(run_dir.join('in{}'.format(simulation_db.JSON_SUFFIX))), 'w') as outfile: json.dump(data, outfile) pkio.write_text( run_dir.join(simulation_type + '_parameters.py'), template.generate_parameters_file( data, _schema_cache(simulation_type), run_dir=run_dir, run_async=run_async, )) cmd = [_ROOT_CMD, simulation_type] \ + ['run-background' if run_async else 'run'] + [str(run_dir)] if run_async: return cfg.job_queue(sid, run_dir, cmd) return _Command(cmd, cfg.foreground_time_limit)
def app_copy_nonsession_simulation(): req = _json_input() simulation_type = req['simulationType'] global_path = simulation_db.find_global_simulation(simulation_type, req['simulationId']) if global_path: data = simulation_db.open_json_file( simulation_type, os.path.join(global_path, simulation_db.SIMULATION_DATA_FILE)) data['models']['simulation']['isExample'] = '' data['models']['simulation']['outOfSessionSimulationId'] = req[ 'simulationId'] res = _save_new_and_reply(simulation_type, data) sirepo.template.import_module(simulation_type).copy_animation_file( global_path, simulation_db.simulation_dir(simulation_type, simulation_db.parse_sid(data))) return res werkzeug.exceptions.abort(404)
def test_auth_login(): from pykern import pkcollections from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkexcept from sirepo import srunit fc = srunit.flask_client( cfg={ 'SIREPO_AUTH_BLUESKY_SECRET': '3SExmbOzn1WeoCWeJxekaE6bMDUj034Pu5az1hLNnvENyvL1FAJ1q3eowwODoa3f', 'SIREPO_AUTH_METHODS': 'bluesky:guest', 'SIREPO_FEATURE_CONFIG_SIM_TYPES': 'srw:myapp', }) from sirepo import simulation_db from sirepo.auth import bluesky import werkzeug.exceptions sim_type = 'srw' uid = fc.sr_login_as_guest(sim_type) data = fc.sr_post( 'listSimulations', { 'simulationType': 'srw', 'search': { 'simulationName': 'Bending Magnet Radiatin' } }, ) fc.cookie_jar.clear() fc.sr_get('authState') data = data[0].simulation req = pkcollections.Dict( simulationType='srw', simulationId=data.simulationId, ) bluesky.auth_hash(req) resp = fc.sr_post('authBlueskyLogin', req) pkeq('ok', resp['state']) pkeq(req.simulationId, simulation_db.parse_sid(resp['data'])) pkeq('srw', resp['schema']['simulationType']) req.authHash = 'not match' resp = fc.sr_post('authBlueskyLogin', req, raw_response=True) pkeq(401, resp.status_code)
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, })