def create_examples(): """Adds missing app examples to all users. """ from pykern import pkio from sirepo import feature_config from sirepo import server from sirepo import simulation_db from sirepo import cookie server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; uid = simulation_db.uid_from_dir_name(d) cookie.init_mock(uid) for sim_type in feature_config.cfg.sim_types: simulation_db.verify_app_directory(sim_type) names = map( lambda x: x['name'], simulation_db.iterate_simulation_datafiles(sim_type, simulation_db.process_simulation_list, { 'simulation.isExample': True, })) for s in simulation_db.examples(sim_type): if s.models.simulation.name not in names: simulation_db.save_new_example(s)
def http(): """Starts Flask server""" from sirepo import server db_dir = _db_dir() with pkio.save_chdir(_run_dir()): server.init(db_dir) server.app.run(host=cfg.ip, port=cfg.port, debug=1, threaded=True)
def upgrade(): """Upgrade the database""" from pykern import pkio from sirepo import simulation_db from sirepo import server import re def _inc(m): return m.group(1) + str(int(m.group(2)) + 1) server.init() for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*/warppba')): for fn in pkio.sorted_glob(d.join('*/sirepo-data.json')): with open(str(fn)) as f: t = f.read() for old, new in ( ('"WARP example laser simulation"', '"Laser-Plasma Wakefield"'), ('"Laser Pulse"', '"Laser-Plasma Wakefield"'), ('"WARP example electron beam simulation"', '"Electron Beam"'), ): if not old in t: continue t = t.replace(old, new) t = re.sub(r'(simulationSerial":\s+)(\d+)', _inc, t) break with open(str(fn), 'w') as f: f.write(t)
def http(): """Starts Flask server in http mode. Used for development only. """ from sirepo import server db_dir = _db_dir() with pkio.save_chdir(_run_dir()): server.init(db_dir) server.app.run( host=cfg.ip, port=cfg.port, threaded=True, use_reloader=1, )
def flask_client(): """Return FlaskClient with easy access methods. Creates a new run directory every test file so can assume sharing of state on the server within a file (module). Two methods of interest: `sr_post` and `sr_get`. Returns: FlaskClient: for local requests to Flask server """ a = 'sr_unit_flask_client' if not hasattr(server.app, a): with pkio.save_chdir(pkunit.work_dir()): db = pkio.mkdir_parent('db') server.app.config['TESTING'] = True server.app.test_client_class = _TestClient server.init(db) setattr(server.app, a, server.app.test_client()) return getattr(server.app, a)
def purge_users(days=180, confirm=False): """Remove old users from db which have not registered. Args: days (int): maximum days of untouched files (old is mtime > days) confirm (bool): delete the directories if True (else don't delete) [False] Returns: list: directories removed (or to remove if confirm) """ from pykern import pkio from sirepo import server from sirepo import simulation_db from sirepo import api_auth import datetime days = int(days) assert days >= 1, \ '{}: days must be a positive integer' server.init() uids = api_auth.all_uids() now = datetime.datetime.utcnow() to_remove = [] for d in pkio.sorted_glob(simulation_db.user_dir_name('*')): if _is_src_dir(d): continue; #TODO(pjm): need to skip special "src" user if simulation_db.uid_from_dir_name(d) in uids: continue for f in pkio.walk_tree(d): if (now - now.fromtimestamp(f.mtime())).days <= days: break else: to_remove.append(d) if confirm: pkio.unchecked_remove(*to_remove) return to_remove
def http(): """Starts Flask server in http mode. Used for development only. """ from sirepo import server with pkio.save_chdir(_run_dir()): use_reloader = pkconfig.channel_in('dev') app = server.init(use_reloader=use_reloader) # avoid WARNING: Do not use the development server in a production environment. app.env = 'development' app.run( host=cfg.ip, port=cfg.port, threaded=True, use_reloader=use_reloader, )