Ejemplo n.º 1
0
def _run_tests():
    # Runs the SRW "Undulator Radiation" simulation's initialIntensityReport.
    simulation_type = 'srw'
    res = server.api_findByName(simulation_type, 'default',
                                'Undulator Radiation')
    m = re.search(r'\/source\/(\w+)"', res)
    if not m:
        raise RuntimeError(
            'status failed to find simulation_id: {}'.format(res))
    simulation_id = m.group(1)
    data = simulation_db.read_simulation_json(simulation_type,
                                              sid=simulation_id)
    data.simulationId = simulation_id
    data.report = 'initialIntensityReport'
    #TODO(pjm): don't call private server methods directly (_start_simulation() and _simulation_run_status())
    server._start_simulation(data)
    max_calls = 10
    for _ in range(max_calls):
        time.sleep(1)
        res = server._simulation_run_status(data)
        if res['state'] == 'error':
            raise RuntimeError(
                'status received simulation error: {}'.format(res))
        if res['state'] == 'completed':
            min_size = 50
            if len(res['z_matrix']) < min_size or len(
                    res['z_matrix'][0]) < min_size:
                raise RuntimeError('status received bad report output')
            return
    raise RuntimeError(
        'status simulation failed to complete within {} seconds'.format(
            max_calls))
Ejemplo n.º 2
0
def import_file(request, lib_dir=None, tmp_dir=None, test_data=None):
    # input_data is passed by test cases only
    f = request.files['file']
    filename = werkzeug.secure_filename(f.filename)
    input_data = test_data

    if 'simulationId' in request.form:
        input_data = simulation_db.read_simulation_json(
            elegant_common.SIM_TYPE, sid=request.form['simulationId'])
    if re.search(r'.ele$', filename, re.IGNORECASE):
        data = elegant_command_importer.import_file(f.read())
    elif re.search(r'.lte$', filename, re.IGNORECASE):
        data = elegant_lattice_importer.import_file(f.read(), input_data)
        if input_data:
            _map_commands_to_lattice(data)
    else:
        raise IOError('invalid file extension, expecting .ele or .lte')
    data['models']['simulation']['name'] = re.sub(r'\.(lte|ele)$',
                                                  '',
                                                  filename,
                                                  flags=re.IGNORECASE)
    if input_data and not test_data:
        simulation_db.delete_simulation(
            elegant_common.SIM_TYPE,
            input_data['models']['simulation']['simulationId'])
    return data
Ejemplo n.º 3
0
def api_simulationData(simulation_type, simulation_id, pretty, section=None):
    #TODO(robnagler) need real type transforms for inputs
    pretty = bool(int(pretty))
    try:
        data = simulation_db.read_simulation_json(simulation_type,
                                                  sid=simulation_id)
        template = sirepo.template.import_module(simulation_type)
        if hasattr(template, 'prepare_for_client'):
            data = template.prepare_for_client(data)
        response = _json_response(
            data,
            pretty=pretty,
        )
        if pretty:
            _as_attachment(
                response,
                app.config.get('JSONIFY_MIMETYPE', 'application/json'),
                '{}.json'.format(data['models']['simulation']['name']),
            )
    except simulation_db.CopyRedirect as e:
        if e.sr_response['redirect'] and section:
            e.sr_response['redirect']['section'] = section
        response = _json_response(e.sr_response)
    _no_cache(response)
    return response
Ejemplo n.º 4
0
def api_copySimulation():
    """Takes the specified simulation and returns a newly named copy with the suffix (copy X)"""
    req = _json_input()
    sim_type = req['simulationType']
    name = req['name'] if 'name' in req else None
    folder = req['folder'] if 'folder' in req else '/'
    data = simulation_db.read_simulation_json(sim_type,
                                              sid=req['simulationId'])
    if not name:
        base_name = data['models']['simulation']['name']
        names = simulation_db.iterate_simulation_datafiles(
            sim_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']['folder'] = folder
    data['models']['simulation']['isExample'] = False
    data['models']['simulation']['outOfSessionSimulationId'] = ''
    return _save_new_and_reply(data)
Ejemplo n.º 5
0
def api_simulationData(simulation_type, simulation_id, pretty, section=None):
    """First entry point for a simulation

    Might be non-session simulation copy (see `simulation_db.CopyRedirect`).
    We have to allow a non-user to get data.
    """
    #TODO(robnagler) need real type transforms for inputs
    req = http_request.parse_params(type=simulation_type,
                                    id=simulation_id,
                                    template=True)
    pretty = bool(int(pretty))
    try:
        d = simulation_db.read_simulation_json(req.type, sid=req.id)
        if hasattr(req.template, 'prepare_for_client'):
            d = req.template.prepare_for_client(d)
        resp = http_reply.gen_json(
            d,
            pretty=pretty,
        )
        if pretty:
            _safe_attachment(
                resp,
                d.models.simulation.name,
                'json',
            )
    except simulation_db.CopyRedirect as e:
        if e.sr_response['redirect'] and section:
            e.sr_response['redirect']['section'] = section
        resp = http_reply.gen_json(e.sr_response)
    return http_reply.headers_for_no_cache(resp)
Ejemplo n.º 6
0
def api_simulationData(simulation_type, simulation_id, pretty, section=None):
    """First entry point for a simulation

    Might be non-session simulation copy (see `simulation_db.CopyRedirect`).
    We have to allow a non-user to get data.
    """
    #TODO(robnagler) need real type transforms for inputs
    pretty = bool(int(pretty))
    try:
        data = simulation_db.read_simulation_json(simulation_type,
                                                  sid=simulation_id)
        template = sirepo.template.import_module(simulation_type)
        if hasattr(template, 'prepare_for_client'):
            data = template.prepare_for_client(data)
        resp = http_reply.gen_json(
            data,
            pretty=pretty,
        )
        if pretty:
            _as_attachment(
                resp,
                http_reply.MIME_TYPE.json,
                '{}.json'.format(data.models.simulation.name),
            )
    except simulation_db.CopyRedirect as e:
        if e.sr_response['redirect'] and section:
            e.sr_response['redirect']['section'] = section
        resp = http_reply.gen_json(e.sr_response)
    return http_reply.headers_for_no_cache(resp)
Ejemplo n.º 7
0
def import_file(req, test_data=None, **kwargs):
    # input_data is passed by test cases only
    input_data = test_data
    text = pkcompat.from_bytes(req.file_stream.read())
    if 'simulationId' in req.req_data:
        input_data = simulation_db.read_simulation_json(
            SIM_TYPE, sid=req.req_data.simulationId)
    if re.search(r'\.ele$', req.filename, re.IGNORECASE):
        data = elegant_command_importer.import_file(text)
    elif re.search(r'\.lte$', req.filename, re.IGNORECASE):
        data = elegant_lattice_importer.import_file(text, input_data)
        if input_data:
            _map_commands_to_lattice(data)
    elif re.search(r'\.madx$', req.filename, re.IGNORECASE):
        from sirepo.template import madx_converter, madx_parser
        data = madx_converter.from_madx(
            SIM_TYPE, madx_parser.parse_file(text, downcase_variables=True))
    else:
        raise IOError('invalid file extension, expecting .ele or .lte')
    data.models.simulation.name = re.sub(r'\.(lte|ele|madx)$',
                                         '',
                                         req.filename,
                                         flags=re.IGNORECASE)
    if input_data and not test_data:
        simulation_db.delete_simulation(
            SIM_TYPE, input_data.models.simulation.simulationId)
    return data
Ejemplo n.º 8
0
def app_python_source(simulation_type, simulation_id, model):
    data = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
    template = sirepo.template.import_module(data)
    return _as_attachment(
        flask.make_response(template.python_source_for_model(data, model)),
        'text/x-python',
        '{}.py'.format(data['models']['simulation']['name']),
    )
Ejemplo n.º 9
0
def app_python_source(simulation_type, simulation_id, model):
    data = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
    template = sirepo.template.import_module(data)
    return _as_attachment(
        flask.make_response(template.python_source_for_model(data, model)),
        'text/x-python',
        '{}.py'.format(data['models']['simulation']['name']),
    )
Ejemplo n.º 10
0
def api_exportRSOptConfig(simulation_type, simulation_id, filename):
    t = sirepo.template.import_module(simulation_type)
    assert hasattr(t, 'export_rsopt_config'), 'Export rsopt unavailable'
    d = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
    return http_reply.gen_file_as_attachment(t.export_rsopt_config(
        d, filename),
                                             filename,
                                             content_type='application/zip')
Ejemplo n.º 11
0
def api_pythonSource(simulation_type, simulation_id, model=None, title=None):
    req = http_request.parse_params(type=simulation_type,
                                    id=simulation_id,
                                    template=True)
    m = model and req.sim_data.parse_model(model)
    d = simulation_db.read_simulation_json(req.type, sid=req.id)
    return _safe_attachment(
        flask.make_response(req.template.python_source_for_model(d, m), ),
        d.models.simulation.name + ('-' + title if title else ''),
        'madx' if m == 'madx' else 'py')
Ejemplo n.º 12
0
def api_copySimulation():
    """Takes the specified simulation and returns a newly named copy with the suffix ( X)"""
    req = http_request.parse_post(id=True, folder=True, name=True)
    d = simulation_db.read_simulation_json(req.type, sid=req.id)
    d.models.simulation.pkupdate(
        name=req.name,
        folder=req.folder,
        isExample=False,
        outOfSessionSimulationId='',
    )
    return _save_new_and_reply(d)
Ejemplo n.º 13
0
def api_exportJupyterNotebook(simulation_type,
                              simulation_id,
                              model=None,
                              title=None):
    t = sirepo.template.import_module(simulation_type)
    assert hasattr(t, 'export_jupyter_notebook'), 'Jupyter export unavailable'
    d = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
    return http_reply.gen_file_as_attachment(
        t.export_jupyter_notebook(d),
        f"{d.models.simulation.name}{'-' + srschema.parse_name(title) if title else ''}.ipynb",
        content_type='application/json')
Ejemplo n.º 14
0
def _run_tests():
    """Runs the SRW "Undulator Radiation" simulation's initialIntensityReport"""
    _validate_auth_state()
    simulation_type = _SIM_TYPE
    res = uri_router.call_api(
        'findByNameWithAuth',
        dict(
            simulation_type=simulation_type,
            application_mode='default',
            simulation_name=_SIM_NAME,
        ),
    )
    m = re.search(r'\/source\/(\w+)"', pkcompat.from_bytes(res.data))
    if not m:
        raise RuntimeError('failed to find sid in resp={}'.format(res.data))
    i = m.group(1)
    d = simulation_db.read_simulation_json(simulation_type, sid=i)
    try:
        d.models.electronBeam.current = d.models.electronBeam.current + (
            random.random() / 10)
    except AttributeError:
        assert _SIM_TYPE == 'myapp', \
            f'{_SIM_TYPE} should be myapp or have models.electronBeam.current'
        pass
    d.simulationId = i
    d.report = _SIM_REPORT
    r = None
    try:
        resp = uri_router.call_api('runSimulation', data=d)
        for _ in range(_MAX_CALLS):
            r = simulation_db.json_load(resp.data)
            pkdlog('resp={}', r)
            if r.state == 'error':
                raise RuntimeError('simulation error: resp={}'.format(r))
            if r.state == 'completed':
                if 'initialIntensityReport' == d.report:
                    min_size = 50
                    if len(r.z_matrix) < min_size or len(
                            r.z_matrix[0]) < min_size:
                        raise RuntimeError(
                            'received bad report output: resp={}', r)
                return
            d = r.nextRequest
            resp = uri_router.call_api('runStatus', data=d)
            time.sleep(_SLEEP)
        raise RuntimeError(
            'simulation timed out: seconds={} resp='.format(
                _MAX_CALLS * _SLEEP, r), )
    finally:
        try:
            uri_router.call_api('runCancel', data=d)
        except Exception:
            pass
Ejemplo n.º 15
0
def api_copySimulation():
    """Takes the specified simulation and returns a newly named copy with the suffix ( X)"""
    req = http_request.parse_json()
    sim_type = req.simulationType
    name = req.name
    assert name, util.err(req, 'No name in request')
    folder = req.folder if 'folder' in req else '/'
    data = simulation_db.read_simulation_json(sim_type, sid=req.simulationId)
    data.models.simulation.name = name
    data.models.simulation.folder = folder
    data.models.simulation.isExample = False
    data.models.simulation.outOfSessionSimulationId = ''
    return _save_new_and_reply(data)
Ejemplo n.º 16
0
def api_pythonSource(simulation_type, simulation_id, model=None, title=None):
    req = http_request.parse_params(type=simulation_type,
                                    id=simulation_id,
                                    template=True)
    m = model and req.sim_data.parse_model(model)
    d = simulation_db.read_simulation_json(req.type, sid=req.id)
    return http_reply.gen_file_as_attachment(
        req.template.python_source_for_model(d, m),
        '{}.{}'.format(
            d.models.simulation.name + ('-' + title if title else ''),
            'madx' if m == 'madx' else 'py',
        ),
    )
Ejemplo n.º 17
0
def api_pythonSource(simulation_type, simulation_id, model=None, report=None):
    import string
    data = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
    template = sirepo.template.import_module(data)
    sim_name = data.models.simulation.name.lower()
    report_rider = '' if report is None else '-' + report.lower()
    py_name = sim_name + report_rider
    py_name = re.sub(r'[\"&\'()+,/:<>?\[\]\\`{}|]', '', py_name)
    py_name = re.sub(r'\s', '-', py_name)
    return _as_attachment(
        flask.make_response(template.python_source_for_model(data, model)),
        'text/x-python',
        '{}.py'.format(py_name),
    )
Ejemplo n.º 18
0
def import_file(req, test_data=None, **kwargs):
    # input_data is passed by test cases only
    d = test_data
    if 'id' in req:
        d = simulation_db.read_simulation_json(SIM_TYPE, sid=req.id)
    p = pkio.py_path(req.filename)
    res = parse_input_text(
        p,
        pkcompat.from_bytes(req.file_stream.read()),
        d,
    )
    res.models.simulation.name = p.purebasename
    if d and not test_data:
        simulation_db.delete_simulation(
            SIM_TYPE,
            d.models.simulation.simulationId,
        )
    return res
Ejemplo n.º 19
0
def _run_tests():
    """Runs the SRW "Undulator Radiation" simulation's initialIntensityReport"""
    simulation_type = _SIM_TYPE
    res = uri_router.call_api(
        'findByNameWithAuth',
        dict(
            simulation_type=simulation_type,
            application_mode='default',
            simulation_name=_SIM_NAME,
        ),
    )
    m = re.search(r'\/source\/(\w+)"', res.data)
    if not m:
        raise RuntimeError('failed to find sid in resp={}'.format(res.data))
    i = m.group(1)
    d = simulation_db.read_simulation_json(simulation_type, sid=i)
    d.simulationId = i
    d.report = _SIM_REPORT
    r = None
    try:
        uri_router.call_api('runSimulation', data=d)
        for _ in range(_MAX_CALLS):
            time.sleep(_SLEEP)
            resp = uri_router.call_api('runStatus', data=d)
            r = simulation_db.json_load(resp.data)
            if r.state == 'error':
                raise RuntimeError('simulation error: resp={}'.format(r))
            if r.state == 'completed':
                if 'initialIntensityReport' == d.report:
                    min_size = 50
                    if len(r.z_matrix) < min_size or len(
                            r.z_matrix[0]) < min_size:
                        raise RuntimeError(
                            'received bad report output: resp={}', r)
                return
            d = r.nextRequest
        raise RuntimeError(
            'simulation timed out: seconds={} resp='.format(
                _MAX_CALLS * _SLEEP, r), )
    finally:
        try:
            uri_router.call_api('runCancel', data=d)
        except Exception:
            pass
Ejemplo n.º 20
0
def app_simulation_data(simulation_type, simulation_id, pretty):
    #TODO(robnagler) need real type transforms for inputs
    pretty = bool(int(pretty))
    try:
        data = simulation_db.read_simulation_json(simulation_type, sid=simulation_id)
        response = _json_response(
            sirepo.template.import_module(simulation_type).prepare_for_client(data),
            pretty=pretty,
        )
        if pretty:
            _as_attachment(
                response,
                app.config.get('JSONIFY_MIMETYPE', 'application/json'),
                '{}.json'.format(data['models']['simulation']['name']),
            )
    except simulation_db.CopyRedirect as e:
        response = _json_response(e.sr_response)
    _no_cache(response)
    return response
Ejemplo n.º 21
0
def import_file(request, lib_dir=None, tmp_dir=None, test_data=None):
    # input_data is passed by test cases only
    f = request.files['file']
    filename = werkzeug.secure_filename(f.filename)
    input_data = test_data

    if 'simulationId' in request.form:
        input_data = simulation_db.read_simulation_json(elegant_common.SIM_TYPE, sid=request.form['simulationId'])
    if re.search(r'.ele$', filename, re.IGNORECASE):
        data = elegant_command_importer.import_file(f.read())
    elif re.search(r'.lte$', filename, re.IGNORECASE):
        data = elegant_lattice_importer.import_file(f.read(), input_data)
        if input_data:
            _map_commands_to_lattice(data)
    else:
        raise IOError('invalid file extension, expecting .ele or .lte')
    data['models']['simulation']['name'] = re.sub(r'\.(lte|ele)$', '', filename, flags=re.IGNORECASE)
    if input_data and not test_data:
        simulation_db.delete_simulation(elegant_common.SIM_TYPE, input_data['models']['simulation']['simulationId'])
    return data
Ejemplo n.º 22
0
def app_copy_simulation():
    """Takes the specified simulation and returns a newly named copy with the suffix (copy X)"""
    req = _json_input()
    sim_type = req['simulationType']
    name = req['name'] if 'name' in req else None
    data = simulation_db.read_simulation_json(sim_type, sid=req['simulationId'])
    if not name:
        base_name = data['models']['simulation']['name']
        names = simulation_db.iterate_simulation_datafiles(sim_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'] = False
    data['models']['simulation']['outOfSessionSimulationId'] = ''
    return _save_new_and_reply(sim_type, data)
Ejemplo n.º 23
0
def import_file(request, lib_dir=None, tmp_dir=None, test_data=None):
    # input_data is passed by test cases only
    f = request.files["file"]
    filename = werkzeug.secure_filename(f.filename)
    input_data = test_data

    if "simulationId" in request.form:
        input_data = simulation_db.read_simulation_json(_SIMULATION_TYPE, sid=request.form["simulationId"])
    try:
        if re.search(r".ele$", filename, re.IGNORECASE):
            data = elegant_command_importer.import_file(f.read())
        elif re.search(r".lte$", filename, re.IGNORECASE):
            data = elegant_lattice_importer.import_file(f.read(), input_data)
            if input_data:
                _map_commands_to_lattice(data)
        else:
            raise IOError("invalid file extension, expecting .ele or .lte")
        data["models"]["simulation"]["name"] = re.sub(r"\.(lte|ele)$", "", filename, re.IGNORECASE)
        if input_data and not test_data:
            simulation_db.delete_simulation(_SIMULATION_TYPE, input_data["models"]["simulation"]["simulationId"])
        return None, data
    except IOError as e:
        return e.message, None
Ejemplo n.º 24
0
def import_file(req, test_data=None, **kwargs):
    # input_data is passed by test cases only
    input_data = test_data

    if 'simulationId' in req:
        input_data = simulation_db.read_simulation_json(
            elegant_common.SIM_TYPE, sid=req.simulationId)
    if re.search(r'.ele$', req.filename, re.IGNORECASE):
        data = elegant_command_importer.import_file(req.file_stream.read())
    elif re.search(r'.lte$', req.filename, re.IGNORECASE):
        data = elegant_lattice_importer.import_file(req.file_stream.read(),
                                                    input_data)
        if input_data:
            _map_commands_to_lattice(data)
    else:
        raise IOError('invalid file extension, expecting .ele or .lte')
    data.models.simulation.name = re.sub(r'\.(lte|ele)$',
                                         '',
                                         req.filename,
                                         flags=re.IGNORECASE)
    if input_data and not test_data:
        simulation_db.delete_simulation(
            elegant_common.SIM_TYPE, input_data.models.simulation.simulationId)
    return data