Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
    """
    days = int(days)
    assert days >= 1, \
        '{}: days must be a positive integer'
    server.init()

    uids = auth_db.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;
        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
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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,
        )
Ejemplo n.º 8
0
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,
        )
Ejemplo n.º 9
0
def audit_proprietary_lib_files(*uid):
    """Add/removes proprietary files based on a user's roles

    For example, add the Flash executable if user has the flash role.

    Args:
        *uid: Uid(s) of the user(s) to audit. If None all users will be audited.
    """
    import py

    def _audit_user(uid, proprietary_sim_types):
        with auth.set_user(uid):
            for t in proprietary_sim_types:
                _link_or_unlink_proprietary_files(
                    t,
                    auth.check_user_has_role(
                        auth.role_for_sim_type(t),
                        raise_forbidden=False,
                    ),
                )

    def _link_or_unlink_proprietary_files(sim_type, should_link):
        d = proprietary_code_dir(sim_type)
        for e in simulation_db.examples(sim_type):
            b = sim_data.get_class(sim_type).proprietary_lib_file_basename(e)
            p = simulation_db.simulation_lib_dir(sim_type).join(b)
            if not should_link:
                pkio.unchecked_remove(p)
                continue
            try:
                p.mksymlinkto(
                    d.join(b),
                    absolute=False,
                )
            except py.error.EEXIST:
                pass

    server.init()
    t = feature_config.cfg().proprietary_sim_types
    if not t:
        return
    for u in uid or auth_db.all_uids():
        _audit_user(u, t)
Ejemplo n.º 10
0
def create_examples():
    """Adds missing app examples to all users.
    """
    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)
        auth.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 example in simulation_db.examples(sim_type):
                if example.models.simulation.name not in names:
                    _create_example(example)
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
def create_examples():
    """Adds missing app examples to all users.
    """
    server.init()

    for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*')):
        if _is_src_dir(d):
            continue
        uid = simulation_db.uid_from_dir_name(d)
        auth.set_user_for_utils(uid)
        for sim_type in feature_config.cfg().sim_types:
            simulation_db.verify_app_directory(sim_type)
            names = [
                x.name for x in simulation_db.iterate_simulation_datafiles(
                    sim_type, simulation_db.process_simulation_list, {
                        'simulation.isExample': True,
                    })
            ]
            for example in simulation_db.examples(sim_type):
                if example.models.simulation.name not in names:
                    _create_example(example)
Ejemplo n.º 13
0
def purge_guest_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, list): dirs and uids of removed guest users (or to remove if confirm)
    """

    days = int(days)
    assert days >= 1, \
        '{}: days must be a positive integer'
    server.init()
    from sirepo import srtime

    guest_uids = auth.guest_uids()
    now = srtime.utc_now()
    dirs_and_uids = {}

    for d in pkio.sorted_glob(simulation_db.user_dir_name().join('*')):
        uid = simulation_db.uid_from_dir_name(d)
        if _is_src_dir(d):
            continue
        if uid not in guest_uids:
            continue
        for f in pkio.walk_tree(d):
            if (now - now.fromtimestamp(f.mtime())).days <= days:
                break
        else:

            dirs_and_uids[d] = uid
    if confirm:
        pkio.unchecked_remove(*dirs_and_uids.keys())
        auth_db.UserRegistration.delete_all_for_column_by_values(
            'uid', dirs_and_uids.values())

    return dirs_and_uids
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def http():
    """Starts Flask server in http mode.

    Used for development only.
    """
    from sirepo import server

    with pkio.save_chdir(_run_dir()):
        app = server.init()
        app.run(
            host=cfg.ip,
            port=cfg.port,
            threaded=True,
            use_reloader=pkconfig.channel_in('dev'),
        )
Ejemplo n.º 16
0
def flask():
    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'
        import werkzeug.serving
        werkzeug.serving.click = None
        app.run(
            host=cfg().ip,
            port=cfg().port,
            threaded=True,
            use_reloader=use_reloader,
        )
Ejemplo n.º 17
0
def http():
    """Starts Flask server in http mode.

    Used for development only.
    """
    from sirepo import server

    with pkio.save_chdir(_run_dir()):
        app = server.init()
        # 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=pkconfig.channel_in('dev'),
        )
Ejemplo n.º 18
0
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,
        )
Ejemplo n.º 19
0
def flask():
    from sirepo import server
    import sirepo.pkcli.setup_dev

    with pkio.save_chdir(_run_dir()):
        sirepo.pkcli.setup_dev.default_command()
        # above will throw better assertion, but just in case
        assert pkconfig.channel_in('dev')
        app = server.init(use_reloader=True)
        # avoid WARNING: Do not use the development server in a production environment.
        app.env = 'development'
        import werkzeug.serving
        werkzeug.serving.click = None
        app.run(
            host=cfg().ip,
            port=cfg().port,
            threaded=True,
            use_reloader=True,
        )