예제 #1
0
파일: srdb.py 프로젝트: yeeon/sirepo
def _init_root(*args):
    global _root

    if args:
        assert not cfg.root, \
            'Cannot set both SIREPO_SRDB_ROOT ({}) and SIREPO_SERVER_DB_DIR ({})'.format(
                cfg.root,
                args[0],
            )
        cfg.root = args[0]
    v = cfg.root
    if v:
        assert os.path.isabs(v), \
            '{}: SIREPO_SRDB_ROOT must be absolute'.format(v)
        assert os.path.isdir(v), \
            '{}: SIREPO_SRDB_ROOT must be a directory and exist'.format(v)
        v = pkio.py_path(v)
    else:
        assert pkconfig.channel_in('dev'), \
            'SIREPO_SRDB_ROOT must be configured except in DEV'
        fn = sys.modules[pkinspect.root_package(_init_root)].__file__
        root = pkio.py_path(pkio.py_path(pkio.py_path(fn).dirname).dirname)
        # Check to see if we are in our dev directory. This is a hack,
        # but should be reliable.
        if not root.join('requirements.txt').check():
            # Don't run from an install directory
            root = pkio.py_path.local('.')
        v = pkio.mkdir_parent(root.join(_DEFAULT_ROOT))
    _root = v
예제 #2
0
파일: srdb.py 프로젝트: cchall/sirepo
def _init_root():
    global _cfg, _root

    def _cfg_root(v):
        """Config value or root package's parent or cwd with `_DEFAULT_ROOT`"""
        if not os.path.isabs(v):
            pkconfig.raise_error(f'{v}: SIREPO_SRDB_ROOT must be absolute')
        if not os.path.isdir(v):
            pkconfig.raise_error(f'{v}: SIREPO_SRDB_ROOT must be a directory and exist')
        return pkio.py_path(v)

    _cfg = pkconfig.init(
        root=(None, _cfg_root, 'where database resides'),
    )
    _root = _cfg.root
    if _root:
        return _root
    assert pkconfig.channel_in('dev'), \
        'SIREPO_SRDB_ROOT must be configured except in dev'
    r = pkio.py_path(
        sys.modules[pkinspect.root_package(_init_root)].__file__,
    ).dirpath().dirpath()
    # Check to see if we are in our dev directory. This is a hack,
    # but should be reliable.
    if not r.join('requirements.txt').check():
        # Don't run from an install directory
        r = pkio.py_path('.')
    _root = pkio.mkdir_parent(r.join(_DEFAULT_ROOT))
    return _root
예제 #3
0
def prepare_simulation(data, run_dir):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``)
    Updates the parameters in ``data`` and save.
    Generate the pkcli command.

    Args:
        data (dict): report and model parameters
        run_dir (py.path.local): dir simulation will be run in
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    import sirepo.sim_data
    sim_type = data.simulationType
    template = sirepo.template.import_module(data)
    s = sirepo.sim_data.get_class(sim_type)
    s.lib_files_to_run_dir(data, run_dir)
    update_rsmanifest(data)
    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = s.is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
예제 #4
0
def init(**kwargs):
    """Declares and initializes config params for calling module.

    Args:
        kwargs (dict): param name to (default, parser, docstring)

    Returns:
        Params: `pkcollections.OrderedMapping` populated with param values
    """
    if '_caller_module' in kwargs:
        # Internal use only: _values() calls init() to initialize pkconfig.cfg
        m = kwargs['_caller_module']
        del kwargs['_caller_module']
    else:
        if pkinspect.is_caller_main():
            print(
                'pkconfig.init() called from __main__; cannot configure, ignoring',
                file=sys.stderr)
            return None
        m = pkinspect.caller_module()
    assert pkinspect.root_package(m) in _load_path, \
        '{}: module root not in load_path ({})'.format(m.__name__, _load_path)
    mnp = m.__name__.split('.')
    for k in reversed(mnp):
        kwargs = {k: kwargs}
    decls = {}
    _flatten_keys([], kwargs, decls)
    _coalesce_values()
    res = pkcollections.OrderedMapping()
    _iter_decls(decls, res)
    for k in mnp:
        res = res[k]
    return res
예제 #5
0
def _cfg_db_dir(value):
    """Config value or root package's parent or cwd with _DEFAULT_SUBDIR"""
    if not value:
        fn = sys.modules[pkinspect.root_package(http)].__file__
        root = py.path.local(py.path.local(py.path.local(fn).dirname).dirname)
        # Check to see if we are in our dev directory. This is a hack,
        # but should be reliable.
        if not root.join('requirements.txt').check():
            # Don't run from an install directory
            root = py.path.local('.')
        value = root.join(_DEFAULT_DB_SUBDIR)
    return value
예제 #6
0
def _cfg_db_dir(value):
    """Config value or root package's parent or cwd with _DEFAULT_SUBDIR"""
    if not value:
        fn = sys.modules[pkinspect.root_package(http)].__file__
        root = py.path.local(py.path.local(py.path.local(fn).dirname).dirname)
        # Check to see if we are in our dev directory. This is a hack,
        # but should be reliable.
        if not root.join('requirements.txt').check():
            # Don't run from an install directory
            root = py.path.local('.')
        value = root.join(_DEFAULT_DB_SUBDIR)
    return value
예제 #7
0
def prepare_simulation(data, tmp_dir=None):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``), or (if
    specified) a ``tmp_dir``.
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
        tmp_dir (py.path.local):
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    if tmp_dir is None:
        # This is the legacy (pre-runner-daemon) code path
        run_dir = simulation_run_dir(data, remove_dir=True)
        #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
        #   probably can only do with celery.
        pkio.mkdir_parent(run_dir)
        out_dir = run_dir
        # Only done on the legacy path, because the runner daemon owns the
        # status file.
        write_status('pending', out_dir)
    else:
        # This is the runner-daemon code path -- tmp_dir is always given, as a
        # new temporary directory we have to create.
        run_dir = simulation_run_dir(data)
        pkio.mkdir_parent(tmp_dir)
        out_dir = tmp_dir
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, out_dir)

    write_json(out_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=out_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
예제 #8
0
def filename(relative_filename, caller_context=None):
    """Return the filename to the resource

    Args:
        relative_filename (str): file name relative to package_data directory.
        caller_context (object): Any object from which to get the `root_package`

    Returns:
        str: absolute path of the resource file
    """
    pkg = pkinspect.root_package(
        caller_context if caller_context else pkinspect.caller_module())
    fn = os.path.join(pksetup.PACKAGE_DATA, relative_filename)
    res = pkg_resources.resource_filename(pkg, fn)
    if not os.path.exists(res):
        raise IOError((errno.ENOENT, 'resource does not exist', res))
    return res
예제 #9
0
def prepare_simulation(data, run_dir=None):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``)
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
        run_dir (py.path.local): defaults to `simulation_run_dir`
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    import sirepo.sim_data
    if run_dir is None:
        # This is the legacy (pre-runner-daemon) code path
        run_dir = simulation_run_dir(data, remove_dir=True)
        #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
        #   probably can only do with celery.
        pkio.mkdir_parent(run_dir)
        write_status('pending', run_dir)
    sim_type = data.simulationType
    template = sirepo.template.import_module(data)
    s = sirepo.sim_data.get_class(sim_type)
    s.lib_files_to_run_dir(data, run_dir)
    update_rsmanifest(data)
    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = s.is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
예제 #10
0
def filename(relative_filename, caller_context=None):
    """Return the filename to the resource

    Args:
        relative_filename (str): file name relative to package_data directory.
        caller_context (object): Any object from which to get the `root_package`

    Returns:
        str: absolute path of the resource file
    """
    pkg = pkinspect.root_package(
        caller_context if caller_context else pkinspect.caller_module())
    assert not os.path.isabs(relative_filename), \
        'must not be an absolute file name={}'.format(relative_filename)
    fn = os.path.join(pksetup.PACKAGE_DATA, relative_filename)
    res = pkg_resources.resource_filename(pkg, fn)
    if not os.path.exists(res):
        msg = 'resource does not exist for pkg=' + pkg
        if pkg == '__main__':
            msg += '; do not call module as a program'
        raise IOError((errno.ENOENT, msg, res))
    return res
예제 #11
0
파일: server.py 프로젝트: pir8aye/sirepo
def _cfg_db_dir(value):
    """Config value or root package's parent or cwd with _DEFAULT_SUBDIR"""
    from pykern import pkinspect

    if value:
        assert os.path.isabs(value), \
            '{}: SIREPO_SERVER_DB_DIR must be absolute'.format(value)
        assert os.path.isdir(value), \
            '{}: SIREPO_SERVER_DB_DIR must be a directory and exist'.format(value)
        value = py.path.local(value)
    else:
        assert pkconfig.channel_in('dev'), \
            'SIREPO_SERVER_DB_DIR must be configured except in DEV'
        fn = sys.modules[pkinspect.root_package(_cfg_db_dir)].__file__
        root = py.path.local(py.path.local(py.path.local(fn).dirname).dirname)
        # Check to see if we are in our dev directory. This is a hack,
        # but should be reliable.
        if not root.join('requirements.txt').check():
            # Don't run from an install directory
            root = py.path.local('.')
        value = pkio.mkdir_parent(root.join(_DEFAULT_DB_SUBDIR))
    return value
예제 #12
0
파일: srdb.py 프로젝트: JiayangY/sirepo
def _init_root():
    global cfg, _root
    cfg = pkconfig.init(root=(None, _cfg_root, 'where database resides'), )
    v = cfg.root
    if v:
        assert os.path.isabs(v), \
            '{}: SIREPO_SRDB_ROOT must be absolute'.format(v)
        assert os.path.isdir(v), \
            '{}: SIREPO_SRDB_ROOT must be a directory and exist'.format(v)
        v = pkio.py_path(v)
    else:
        assert pkconfig.channel_in('dev'), \
            'SIREPO_SRDB_ROOT must be configured except in DEV'
        fn = sys.modules[pkinspect.root_package(_init_root)].__file__
        root = pkio.py_path(pkio.py_path(pkio.py_path(fn).dirname).dirname)
        # Check to see if we are in our dev directory. This is a hack,
        # but should be reliable.
        if not root.join('requirements.txt').check():
            # Don't run from an install directory
            root = pkio.py_path('.')
        v = pkio.mkdir_parent(root.join(_DEFAULT_ROOT))
    _root = v
    return v
예제 #13
0
def prepare_simulation(data):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``).
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    run_dir = simulation_run_dir(data, remove_dir=True)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    pkio.mkdir_parent(run_dir)
    write_status('pending', run_dir)
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, run_dir)

    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
예제 #14
0
def prepare_simulation(data):
    """Create and install files, update parameters, and generate command.

    Copies files into the simulation directory (``run_dir``).
    Updates the parameters in ``data`` and save.
    Generate the pkcli command to pass to task runner.

    Args:
        data (dict): report and model parameters
    Returns:
        list, py.path: pkcli command, simulation directory
    """
    run_dir = simulation_run_dir(data, remove_dir=True)
    #TODO(robnagler) create a lock_dir -- what node/pid/thread to use?
    #   probably can only do with celery.
    pkio.mkdir_parent(run_dir)
    write_status('pending', run_dir)
    sim_type = data['simulationType']
    sid = parse_sid(data)
    template = sirepo.template.import_module(data)
    template_common.copy_lib_files(data, None, run_dir)

    write_json(run_dir.join(template_common.INPUT_BASE_NAME), data)
    #TODO(robnagler) encapsulate in template
    is_p = is_parallel(data)
    template.write_parameters(
        data,
        run_dir=run_dir,
        is_parallel=is_p,
    )
    cmd = [
        pkinspect.root_package(template),
        pkinspect.module_basename(template),
        'run-background' if is_p else 'run',
        str(run_dir),
    ]
    return cmd, run_dir
예제 #15
0
파일: db.py 프로젝트: radiasoft/rsconf
def _cfg_root(value):
    """Parse root directory"""
    #TODO(robnagler) encapsulate this sirepo.server is the same thing
    from pykern import pkio, pkinspect
    import os, sys

    if value:
        assert os.path.isabs(value), \
            '{}: must be absolute'.format(value)
        value = pkio.py_path(value)
        assert value.check(dir=1), \
            '{}: must be a directory and exist'.format(value)
    else:
        assert pkconfig.channel_in('dev'), \
            'must be configured except in DEV'
        fn = pkio.py_path(sys.modules[pkinspect.root_package(_cfg_root)].__file__)
        root = pkio.py_path(pkio.py_path(fn.dirname).dirname)
        # Check to see if we are in our ~/src/radiasoft/<pkg> dir. This is a hack,
        # but should be reliable.
        if not root.join('setup.py').check():
            # Don't run from an install directorya
            root = pkio.py_path('.')
        value = root.join(DEFAULT_ROOT_SUBDIR)
    return value
예제 #16
0
def test_root_pkg():
    m2 = pkunit.import_module_from_data_dir('p1.p2.m2')
    assert pkinspect.root_package(m2) == 'p1'
예제 #17
0
def test_root_pkg():
    m2 = pkunit.import_module_from_data_dir('p1.p2.m2')
    assert pkinspect.root_package(m2) == 'p1'