Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
robnagler committed May 31, 2017
1 parent 942ceb8 commit 4a60ab8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion sirepo/exporter.py
Expand Up @@ -89,7 +89,7 @@ def _create_zip(sim_type, sim_id, want_python):
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(sim_type, sim_id))
files.insert(0, simulation_db.sim_data_file(data.simulationType, sim_id))
if want_python:
files.append(_python(data))
with zipfile.ZipFile(
Expand Down
2 changes: 2 additions & 0 deletions sirepo/feature_config.py
Expand Up @@ -12,6 +12,8 @@

#: All possible codes
_ALL_CODES = ('srw', 'warppba', 'elegant', 'shadow', 'hellweg', 'fete')
assert [] == [x for x in _ALL_CODES if len(x) >= 8], \
'codes must be less than 8 characters (simulation_db._ID_LEN)'

#: Codes on test and prod
_NON_DEV_CODES = _ALL_CODES
Expand Down
Expand Up @@ -65,7 +65,7 @@
"zMin": 0
},
"simulation": {
"name": "WARP PBA example electron beam simulation",
"name": "Electron Beam",
"simulationId": "XH1MO4II",
"sourceType": "electronBeam"
},
Expand Down Expand Up @@ -99,5 +99,5 @@
},
"report": "beamPreviewReport",
"simulationType": "warppba",
"version": "20170529"
"version": "20000101.000000"
}
4 changes: 2 additions & 2 deletions sirepo/package_data/template/warppba/examples/example.json
Expand Up @@ -65,7 +65,7 @@
"zMin": 0
},
"simulation": {
"name": "WARP PBA example laser simulation",
"name": "Laser Pulse",
"simulationId": "h3a5iMzC",
"sourceType": "laserPulse"
},
Expand Down Expand Up @@ -99,5 +99,5 @@
},
"report": "laserPreviewReport",
"simulationType": "warppba",
"version": "20170529"
"version": "20000101.000000"
}
26 changes: 14 additions & 12 deletions sirepo/server.py
Expand Up @@ -86,7 +86,7 @@ def api_copyNonSessionSimulation():
)
data['models']['simulation']['isExample'] = False
data['models']['simulation']['outOfSessionSimulationId'] = req['simulationId']
res = _save_new_and_reply(sim_type, data)
res = _save_new_and_reply(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)
Expand All @@ -112,7 +112,7 @@ def api_copySimulation():
data['models']['simulation']['name'] = name
data['models']['simulation']['isExample'] = False
data['models']['simulation']['outOfSessionSimulationId'] = ''
return _save_new_and_reply(sim_type, data)
return _save_new_and_reply(data)
app_copy_simulation = api_copySimulation


Expand All @@ -125,7 +125,7 @@ def api_deleteSimulation():

def api_downloadDataFile(simulation_type, simulation_id, model, frame, suffix=None):
data = {
'simulationType': simulation_type,
'simulationType': sirepo.template.assert_sim_type(simulation_type),
'simulationId': simulation_id,
'modelName': model,
}
Expand Down Expand Up @@ -230,7 +230,7 @@ def api_findByName(simulation_type, application_mode, simulation_name):
if len(rows) == 0:
for s in simulation_db.examples(simulation_type):
if s['models']['simulation']['name'] == simulation_name:
simulation_db.save_new_example(simulation_type, s)
simulation_db.save_new_example(s)
rows = simulation_db.iterate_simulation_datafiles(simulation_type, simulation_db.process_simulation_list, {
'simulation.name': simulation_name,
})
Expand Down Expand Up @@ -325,7 +325,7 @@ def api_importFile(simulation_type=None):
)
#TODO(robnagler) need to validate folder
data.models.simulation.folder = flask.request.form['folder']
return _save_new_and_reply(data.simulationType, data)
return _save_new_and_reply(data)
except Exception as e:
pkdlog('{}: exception: {}', f and f.filename, pkdexc())
error = e.message if hasattr(e, 'message') else str(e)
Expand All @@ -347,7 +347,7 @@ def api_newSimulation():
data['models']['simulation']['name'] = new_simulation_data['name']
data['models']['simulation']['folder'] = new_simulation_data['folder']
sirepo.template.import_module(sim_type).new_simulation(data, new_simulation_data)
return _save_new_and_reply(sim_type, data)
return _save_new_and_reply(data)
app_new_simulation = api_newSimulation


Expand Down Expand Up @@ -468,7 +468,6 @@ def api_saveSimulationData():
return res
simulation_type = data['simulationType']
data = simulation_db.save_simulation_json(
simulation_type,
sirepo.template.import_module(simulation_type).prepare_for_save(data),
)
return app_simulation_data(
Expand Down Expand Up @@ -537,7 +536,7 @@ def api_listSimulations():


def api_simulationSchema():
sim_type = flask.request.form['simulationType']
sim_type = sirepo.template.assert_sim_type(flask.request.form['simulationType'])
return _json_response(simulation_db.get_schema(sim_type))
app_simulation_schema = api_simulationSchema

Expand All @@ -562,7 +561,7 @@ def api_updateFolder():
folder = row['models']['simulation']['folder']
if folder.startswith(old_name):
row['models']['simulation']['folder'] = re.sub(re.escape(old_name), new_name, folder, 1)
simulation_db.save_simulation_json(data['simulationType'], row)
simulation_db.save_simulation_json(row)
return _json_response_ok()
app_update_folder = api_updateFolder

Expand Down Expand Up @@ -789,7 +788,7 @@ def _cfg_time_limit(value):
return v


def _json_input():
def _json_input(assert_sim_type=True):
req = flask.request
if req.mimetype != 'application/json':
pkdlog('{}: req.mimetype is not application/json', req.mimetype)
Expand All @@ -801,7 +800,10 @@ def _json_input():
# and strict in what we send out.
charset = req.mimetype_params.get('charset')
data = req.get_data(cache=False)
return simulation_db.json_load(data, encoding=charset)
res = simulation_db.json_load(data, encoding=charset)
if assert_sim_type and 'simulationType' in res:
res.simulationType = sirepo.template.assert_sim_type(res.simulationType)
return res


def _json_response(value, pretty=False):
Expand Down Expand Up @@ -849,7 +851,7 @@ def _no_cache(response):


def _parse_data_input(validate=False):
data = _json_input()
data = _json_input(assert_sim_type=False)
return simulation_db.fixup_old_data(data)[0] if validate else data


Expand Down
39 changes: 26 additions & 13 deletions sirepo/simulation_db.py
Expand Up @@ -193,6 +193,8 @@ def fixup_old_data(data, force=False):
else:
pkdlog('simulationType: not found; data={}', data)
raise AssertionError('must have simulationType')
elif data['simulationType'] == 'warp':
data['simulationType'] = 'warppba'
if not 'simulationSerial' in data['models']['simulation']:
data['models']['simulation']['simulationSerial'] = 0
sirepo.template.import_module(data['simulationType']).fixup_old_data(data)
Expand Down Expand Up @@ -284,8 +286,10 @@ def hack_nfs_write_status(status, run_dir):

def iterate_simulation_datafiles(simulation_type, op, search=None):
res = []
sim_dir = simulation_dir(simulation_type)
simulation_type = simulation_type_from_dir_name(sim_dir)
for path in glob.glob(
str(simulation_dir(simulation_type).join('*', SIMULATION_DATA_FILE)),
str(sim_dir.join('*', SIMULATION_DATA_FILE)),
):
path = py.path.local(path)
try:
Expand Down Expand Up @@ -568,7 +572,7 @@ def read_simulation_json(sim_type, *args, **kwargs):
data = open_json_file(sim_type, fixup=False, *args, **kwargs)
new, changed = fixup_old_data(data)
if changed:
return save_simulation_json(sim_type, new)
return save_simulation_json(new)
return data


Expand Down Expand Up @@ -631,22 +635,22 @@ def report_info(data):
return rep


def save_new_example(simulation_type, data):
def save_new_example(data):
data.models.simulation.isExample = True
return save_new_simulation(simulation_type, data)
return save_new_simulation(data)


def save_new_simulation(simulation_type, data):
sid = _random_id(simulation_dir(data.simulationType), simulation_type).id
def save_new_simulation(data):
d = simulation_dir(data.simulationType)
sid = _random_id(d, data.simulationType).id
data.models.simulation.simulationId = sid
return save_simulation_json(data.simulationType, data)
return save_simulation_json(data)


def save_simulation_json(simulation_type, data):
def save_simulation_json(data):
"""Prepare data and save to json db
Args:
simulation_type (str): srw, warppba, ...
data (dict): what to write (contains simulationId)
"""
try:
Expand All @@ -657,7 +661,7 @@ def save_simulation_json(simulation_type, data):
pass
data = fixup_old_data(data)[0]
s = data.models.simulation
fn = sim_data_file(simulation_type, s.simulationId)
fn = sim_data_file(data.simulationType, s.simulationId)
with _global_lock:
need_validate = True
try:
Expand Down Expand Up @@ -731,6 +735,14 @@ def simulation_run_dir(data, remove_dir=False):
return d


def simulation_type_from_dir_name(d):
"""Extract simulation_type from simulation_dir"""
res = d.basename
if _ID_RE.search(res) or res == _LIB_DIR:
res = py.path.local(d.dirname).basename
return sirepo.template.assert_sim_type(res)


def tmp_dir():
"""Generates new, temporary directory
Expand Down Expand Up @@ -780,7 +792,7 @@ def validate_serial(req_data):
object: None if all ok, or json response (bad)
"""
with _global_lock:
sim_type = req_data['simulationType']
sim_type = sirepo.template.assert_sim_type(req_data['simulationType'])
sid = parse_sid(req_data)
req_ser = req_data['models']['simulation']['simulationSerial']
curr = read_simulation_json(sim_type, sid=sid)
Expand All @@ -805,7 +817,7 @@ def verify_app_directory(simulation_type):
d = simulation_dir(simulation_type)
if d.exists():
return
_create_example_and_lib_files(simulation_type)
_create_example_and_lib_files(simulation_type_from_dir_name(d))


def write_json(filename, data):
Expand Down Expand Up @@ -849,9 +861,10 @@ def write_status(status, run_dir):

def _create_example_and_lib_files(simulation_type):
d = simulation_dir(simulation_type)
simulation_type = simulation_type_from_dir_name(d)
pkio.mkdir_parent(d)
for s in examples(simulation_type):
save_new_example(simulation_type, s)
save_new_example(s)
d = simulation_lib_dir(simulation_type)
pkio.mkdir_parent(d)
for f in sirepo.template.import_module(simulation_type).resource_files():
Expand Down
4 changes: 2 additions & 2 deletions sirepo/sr_unit.py
Expand Up @@ -9,7 +9,7 @@
from pykern import pkconfig
from pykern import pkio
from pykern import pkunit
from pykern.pkdebug import pkdp, pkdc, pkdexc, pkdlog
from pykern.pkdebug import pkdp, pkdc, pkdexc, pkdlog, pkdpretty
import flask
import flask.testing
import json
Expand Down Expand Up @@ -129,7 +129,7 @@ def sr_sim_data(self, sim_type, sim_name):
if d['name'] == sim_name:
break
else:
pkfail('{}: not found in ', sim_name, pkdpretty(data))
pkunit.pkfail('{}: not found in ', sim_name, pkdpretty(data))
return self.sr_get(
'simulationData',
{
Expand Down
4 changes: 2 additions & 2 deletions sirepo/template/srw.py
Expand Up @@ -763,11 +763,11 @@ def prepare_for_client(data):
model['id'] = _unique_name(user_model_list, 'id', data['models']['simulation']['simulationId'] + ' {}')
user_model_list.append(_create_user_model(data, model_name))
_save_user_model_list(model_name, user_model_list)
simulation_db.save_simulation_json(SIM_TYPE, data)
simulation_db.save_simulation_json(data)

if pluralKey in data['models']:
del data['models'][pluralKey]
simulation_db.save_simulation_json(SIM_TYPE, data)
simulation_db.save_simulation_json(data)
return data


Expand Down
2 changes: 1 addition & 1 deletion tests/exporter_test.py
Expand Up @@ -23,7 +23,7 @@ def test_create_zip():
for sim_type, sim_name, expect in imported + [
('elegant', 'bunchComp - fourDipoleCSR', ['WAKE-inputfile.knsl45.liwake.sdds', 'run.py', 'sirepo-data.json']),
('srw', 'Tabulated Undulator Example', ['magnetic_measurements.zip', 'run.py', 'sirepo-data.json']),
('warppba', 'WARP PBA example laser simulation', ['run.py', 'sirepo-data.json']),
('warppba', 'Laser Pulse', ['run.py', 'sirepo-data.json']),
]:
sim_id = fc.sr_sim_data(sim_type, sim_name)['models']['simulation']['simulationId']
resp = fc.sr_get(
Expand Down

0 comments on commit 4a60ab8

Please sign in to comment.