Python pkg_resources.resource_filename() Examples

The following are 30 code examples of pkg_resources.resource_filename(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: test_schema_validation.py    From drydock with Apache License 2.0 7 votes vote down vote up
def _test_validate(self, schema, expect_failure, input_files, input):
        """validates input yaml against schema.

        :param schema: schema yaml file
        :param expect_failure: should the validation pass or fail.
        :param input_files: pytest fixture used to access the test input files
        :param input: test input yaml doc filename"""
        schema_dir = pkg_resources.resource_filename('drydock_provisioner',
                                                     'schemas')
        schema_filename = os.path.join(schema_dir, schema)
        schema_file = open(schema_filename, 'r')
        schema = yaml.safe_load(schema_file)

        input_file = input_files.join(input)
        instance_file = open(str(input_file), 'r')
        instance = yaml.safe_load(instance_file)

        if expect_failure:
            with pytest.raises(ValidationError):
                jsonschema.validate(instance['spec'], schema['data'])
        else:
            jsonschema.validate(instance['spec'], schema['data']) 
Example #2
Source File: test_basic.py    From jbox with MIT License 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example #3
Source File: corruptions.py    From robustness with Apache License 2.0 6 votes vote down vote up
def frost(x, severity=1):
    c = [(1, 0.4),
         (0.8, 0.6),
         (0.7, 0.7),
         (0.65, 0.7),
         (0.6, 0.75)][severity - 1]
    idx = np.random.randint(5)
    filename = [resource_filename(__name__, 'frost/frost1.png'),
                resource_filename(__name__, 'frost/frost2.png'),
                resource_filename(__name__, 'frost/frost3.png'),
                resource_filename(__name__, 'frost/frost4.jpg'),
                resource_filename(__name__, 'frost/frost5.jpg'),
                resource_filename(__name__, 'frost/frost6.jpg')][idx]
    frost = cv2.imread(filename)
    # randomly crop and convert to rgb
    x_start, y_start = np.random.randint(0, frost.shape[0] - 224), np.random.randint(0, frost.shape[1] - 224)
    frost = frost[x_start:x_start + 224, y_start:y_start + 224][..., [2, 1, 0]]

    return np.clip(c[0] * np.array(x) + c[1] * frost, 0, 255) 
Example #4
Source File: compute_build_graph.py    From staged-recipes with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def git_changed_submodules(git_rev='HEAD@{1}', stop_rev=None, git_root='.'):
    if stop_rev is not None:
        git_rev = "{0}..{1}".format(git_rev, stop_rev)
    diff_script = pkg_resources.resource_filename('conda_concourse_ci', 'diff-script.sh')

    diff = subprocess.check_output(['bash', diff_script, git_rev],
                                    cwd=git_root, universal_newlines=True)

    submodule_changed_files = [line.split() for line in diff.splitlines()]

    submodules_with_recipe_changes = []
    for submodule in submodule_changed_files:
        for file in submodule:
            if 'recipe/' in file and submodule[0] not in submodules_with_recipe_changes:
                submodules_with_recipe_changes.append(submodule[0])

    return submodules_with_recipe_changes 
Example #5
Source File: package.py    From pearl with GNU General Public License v3.0 6 votes vote down vote up
def create_package(pearl_env: PearlEnvironment, args: Namespace):
    """
    Creates package from template.
    """
    static = Path(pkg_resources.resource_filename('pearllib', 'static/'))
    pearl_config_template = static / 'templates/pearl-config.template'
    dest_pearl_config = args.dest_dir / 'pearl-config'
    if dest_pearl_config.exists():
        raise PackageCreateError('The pearl-config directory already exists in {}'.format(args.dest_dir))
    shutil.copytree(str(pearl_config_template), str(dest_pearl_config))

    messenger.info('Updating {} to add package in local repository...'.format(pearl_env.config_filename))
    with pearl_env.config_filename.open('a') as pearl_conf_file:
        pearl_conf_file.write('PEARL_PACKAGES["{}"] = {{"url": "{}"}}\n'.format(args.name, args.dest_dir))

    messenger.info('Run "pearl search local" to see your new local package available') 
Example #6
Source File: utils.py    From pearl with GNU General Public License v3.0 6 votes vote down vote up
def run_pearl_bash(
        script: str, pearl_env,
        capture_stdout: bool = False, capture_stderr: bool = False,
        check: bool = True,
        input: str = None,
        enable_xtrace: bool = False,
        enable_errexit: bool = True,
):
    """Runs a bash script within the Pearl ecosystem."""

    bash_header = _BASH_SCRIPT_HEADER_TEMPLATE.format(
        pearlhome=pearl_env.home,
        static=pkg_resources.resource_filename('pearllib', 'static/'),
    )
    script_template = '{bashheader}\nset -x\n{script}' if enable_xtrace else '{bashheader}\n{script}'
    if enable_errexit:
        script_template = 'set -e\n{}'.format(script_template)

    script = script_template.format(
        bashheader=bash_header,
        script=script,
    )
    return run_bash(script, capture_stdout=capture_stdout, capture_stderr=capture_stderr, check=check, input=input) 
Example #7
Source File: bids.py    From smriprep with Apache License 2.0 6 votes vote down vote up
def get_outputnode_spec():
    """
    Generate outputnode's fields from I/O spec file.

    Examples
    --------
    >>> get_outputnode_spec()  # doctest: +NORMALIZE_WHITESPACE
    ['t1w_preproc', 't1w_mask', 't1w_dseg', 't1w_tpms',
    'std_preproc', 'std_mask', 'std_dseg', 'std_tpms',
    'anat2std_xfm', 'std2anat_xfm',
    't1w_aseg', 't1w_aparc',
    't1w2fsnative_xfm', 'fsnative2t1w_xfm',
    'surfaces']

    """
    spec = loads(Path(pkgrf('smriprep', 'data/io_spec.json')).read_text())["queries"]
    fields = ['_'.join((m, s)) for m in ('t1w', 'std') for s in spec["baseline"].keys()]
    fields += [s for s in spec["std_xfms"].keys()]
    fields += [s for s in spec["surfaces"].keys()]
    return fields 
Example #8
Source File: chute.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def validate(ctx):
    """
    Validate the paradrop.yaml file.

    A note about versions: this command validates the chute configuration
    against the current rules for the installed version of pdtools. If
    the chute is to be installed on a Paradrop node running a different
    version, then this command may not be reliable for determining
    compatibility.
    """
    with open('paradrop.yaml', 'r') as source:
        chute = yaml.safe_load(source)

    schema_path = pkg_resources.resource_filename('pdtools', 'schemas/chute.json')
    with open(schema_path, 'r') as source:
        schema = json.load(source)

    validator = jsonschema.Draft4Validator(schema)
    for error in sorted(validator.iter_errors(chute), key=str):
        click.echo(error.message) 
Example #9
Source File: core.py    From mqttwarn with Eclipse Public License 2.0 6 votes vote down vote up
def load_services(services):
    for service in services:
        service_plugins[service] = {}

        service_config = cf.config('config:' + service)
        if service_config is None:
            logger.error("Service `%s' has no config section" % service)
            sys.exit(1)

        service_plugins[service]['config'] = service_config

        module = cf.g('config:' + service, 'module', service)
        modulefile = resource_filename('mqttwarn.services', module + '.py')

        try:
            service_plugins[service]['module'] = load_module(modulefile)
            logger.info('Successfully loaded service "{}"'.format(service))
        except Exception as ex:
            logger.exception('Unable to load service "{}" from file "{}": {}'.format(service, modulefile, ex)) 
Example #10
Source File: mmbot.py    From MaliciousMacroBot with MIT License 6 votes vote down vote up
def __init__(self, benign_path=None, malicious_path=None,
                 model_path=pkg_resources.resource_filename('mmbot', 'model'), retain_sample_contents=False):
        """
        Constructor to setup path variables for model and sample data and initialize object.
        :param benign_path: directory path (relative or absolute) to benign documents for the machine learning model to learn from.
        :param malicious_path: directory path (relative or absolute) to malicious documents for the machine learning model to learn from.
        :param model_path: directory where modeldata.pickle and vocab.txt files are kept.
        :param retain_sample_contents: this relates to level of detail saved in the model data.  If True, potentially sensitive
        information like extracted vba will be stored in the model's pickle file.  The benefit is that incremental
        models can be built, where adding a new file to the training set will result in only reprocessing that one new
        file.  Otherwise all files in the benign_path and malicious_path will be reprocessed each time the model is
        rebuilt.  If you are experimenting with building many models and comparing results, set this to True,
        otherwise keep it to False.
        """
        # os.path.join(os.path.dirname(__file__), 'model')
        self.clear_state()
        self.set_model_paths(benign_path, malicious_path, model_path)
        self.retain_sample_contents = retain_sample_contents 
Example #11
Source File: app.py    From gnocchi with Apache License 2.0 6 votes vote down vote up
def load_app(conf, not_implemented_middleware=True):
    global APPCONFIGS

    # Build the WSGI app
    cfg_path = conf.api.paste_config
    if not os.path.isabs(cfg_path):
        cfg_path = conf.find_file(cfg_path)

    if cfg_path is None or not os.path.exists(cfg_path):
        LOG.debug("No api-paste configuration file found! Using default.")
        cfg_path = os.path.abspath(pkg_resources.resource_filename(
            __name__, "api-paste.ini"))

    config = dict(conf=conf,
                  not_implemented_middleware=not_implemented_middleware)
    configkey = str(uuid.uuid4())
    APPCONFIGS[configkey] = config

    LOG.info("WSGI config used: %s", cfg_path)

    appname = "gnocchi+" + conf.api.auth_mode
    app = deploy.loadapp("config:" + cfg_path, name=appname,
                         global_conf={'configkey': configkey})
    return http_proxy_to_wsgi.HTTPProxyToWSGI(
        cors.CORS(app, conf=conf), conf=conf) 
Example #12
Source File: test_interrogate.py    From calmjs with GNU General Public License v2.0 6 votes vote down vote up
def test_identifier_extract_unusual(self):
        answers = [
            '\'"\'',
            '\'\"\'',
            "\'\"\'",
            "\n",
            "\t",
            r'\\ ',
            "\u3042",
            "\u3042",
            "    ",
        ]
        with open(resource_filename('calmjs.testing', join(
                'names', 'unusual.js')), encoding='utf-8') as fd:
            tree = es5(fd.read())
        for obj_node, answer in zip(interrogate.shallow_filter(
                tree, lambda node: isinstance(node, Object)), answers):
            self.assertEqual(answer, interrogate.to_identifier(
                obj_node.properties[0].left)) 
Example #13
Source File: test_sandbox.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def test_setup_py_with_BOM(self):
        """
        It should be possible to execute a setup.py with a Byte Order Mark
        """
        target = pkg_resources.resource_filename(__name__,
            'script-with-bom.py')
        namespace = types.ModuleType('namespace')
        setuptools.sandbox.execfile(target, vars(namespace))
        assert namespace.result == 'passed' 
Example #14
Source File: themefile.py    From alienfx with GNU General Public License v3.0 5 votes vote down vote up
def set_default_theme(self):
        """ Sets the theme contents to a default value."""
        default_themefile = pkg_resources.resource_filename(
            "alienfx", "data/themes/default.json")
        self._load_from_file(default_themefile)
        self.theme_name = "" 
Example #15
Source File: resources.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _get_pkg_resource(filename, package, prefix):
    """Query pkg_resources for the location of the filename."""
    requirement = pkg_resources.Requirement.parse(package)
    target = os.path.join(prefix, filename)
    try:
        return pkg_resources.resource_filename(requirement, target)
    except pkg_resources.DistributionNotFound:
        # It may be that the working set is not in sync (e.g. if sys.path was
        # manipulated). Try to reload it just in case.
        pkg_resources.working_set = pkg_resources.WorkingSet()
        try:
            return pkg_resources.resource_filename(requirement, target)
        except pkg_resources.DistributionNotFound:
            return None 
Example #16
Source File: test_integration.py    From tedana with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_integration_four_echo(skip_integration):
    """ Integration test of the full tedana workflow using four-echo test data
    """

    if skip_integration:
        pytest.skip('Skipping four-echo integration test')
    out_dir = '/tmp/data/four-echo/TED.four-echo'
    if os.path.exists(out_dir):
        shutil.rmtree(out_dir)

    # download data and run the test
    download_test_data('https://osf.io/gnj73/download',
                       os.path.dirname(out_dir))
    prepend = '/tmp/data/four-echo/'
    prepend += 'sub-PILOT_ses-01_task-localizerDetection_run-01_echo-'
    suffix = '_space-sbref_desc-preproc_bold+orig.HEAD'
    datalist = [prepend + str(i + 1) + suffix for i in range(4)]
    tedana_cli.tedana_workflow(
        data=datalist,
        tes=[11.8, 28.04, 44.28, 60.52],
        out_dir=out_dir,
        tedpca='kundu-stabilize',
        gscontrol=['gsr', 't1c'],
        png_cmap='bone',
        debug=True,
        verbose=True)

    # compare the generated output files
    fn = resource_filename('tedana', 'tests/data/fiu_four_echo_outputs.txt')
    check_integration_outputs(fn, out_dir) 
Example #17
Source File: conftest.py    From lisc with Apache License 2.0 5 votes vote down vote up
def check_db():
    """Once, prior to session, this will clear and re-initialize the test file database."""

    # Create the test database directory
    tests_dir = pkg.resource_filename('lisc', 'tests')
    test_db_name = 'test_db'

    # If the directories already exist, clear them
    if os.path.exists(os.path.join(tests_dir, test_db_name)):
        shutil.rmtree(os.path.join(tests_dir, test_db_name))

    tdb = create_file_structure(tests_dir, test_db_name)
    create_files(tdb) 
Example #18
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def test_unpack():
    """
    Make sure 'wheel unpack' works.
    This also verifies the integrity of our testing wheel files.
    """
    for dist in test_distributions:
        distdir = pkg_resources.resource_filename('wheel.test',
                                                  os.path.join(dist, 'dist'))
        for wheelfile in (w for w in os.listdir(distdir) if w.endswith('.whl')):
            wheel.tool.unpack(os.path.join(distdir, wheelfile), distdir) 
Example #19
Source File: utils.py    From lisc with Apache License 2.0 5 votes vote down vote up
def __init__(self):

        # Initialize from normal database object
        base = pkg.resource_filename(__name__, 'test_db')
        SCDB.__init__(self, base=base) 
Example #20
Source File: ssl_support.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def find_ca_bundle():
    """Return an existing CA bundle path, or None"""
    if os.name=='nt':
        return get_win_certfile()
    else:
        for cert_path in cert_paths:
            if os.path.isfile(cert_path):
                return cert_path
    try:
        return pkg_resources.resource_filename('certifi', 'cacert.pem')
    except (ImportError, ResolutionError, ExtractionError):
        return None 
Example #21
Source File: bdist_egg.py    From python-netsurv with MIT License 5 votes vote down vote up
def write_stub(resource, pyfile):
    _stub_template = textwrap.dedent("""
        def __bootstrap__():
            global __bootstrap__, __loader__, __file__
            import sys, pkg_resources, imp
            __file__ = pkg_resources.resource_filename(__name__, %r)
            __loader__ = None; del __bootstrap__, __loader__
            imp.load_dynamic(__name__,__file__)
        __bootstrap__()
        """).lstrip()
    with open(pyfile, 'w') as f:
        f.write(_stub_template % resource) 
Example #22
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def test_no_scripts():
    """Make sure entry point scripts are not generated."""
    dist = "complex-dist"
    basedir = pkg_resources.resource_filename('wheel.test', dist)
    for (dirname, subdirs, filenames) in os.walk(basedir):
        for filename in filenames:
            if filename.endswith('.whl'):
                whl = ZipFile(os.path.join(dirname, filename))
                for entry in whl.infolist():
                    assert not '.data/scripts/' in entry.filename 
Example #23
Source File: cython_bbox.py    From FastMaskRCNN with Apache License 2.0 5 votes vote down vote up
def __bootstrap__():
  global __bootstrap__, __loader__, __file__
  __file__ = pkg_resources.resource_filename(__name__, 'cython_bbox.so')
  __loader__ = None
  del __bootstrap__, __loader__
  imp.load_dynamic(__name__, __file__) 
Example #24
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def test_convert_egg():
    base = pkg_resources.resource_filename('wheel.test', '')
    for dist in test_distributions:
        distdir = os.path.join(base, dist, 'dist')
        eggs = [e for e in os.listdir(distdir) if e.endswith('.egg')]
        wheel.tool.convert(eggs, distdir, verbose=False) 
Example #25
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def test_egg_re():
    """Make sure egg_info_re matches."""
    egg_names = open(pkg_resources.resource_filename('wheel', 'eggnames.txt'))
    for line in egg_names:
        line = line.strip()
        if not line:
            continue
        assert egg2wheel.egg_info_re.match(line), line 
Example #26
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def build_wheel():
    """Build wheels from test distributions."""
    for dist in test_distributions:
        pwd = os.path.abspath(os.curdir)
        distdir = pkg_resources.resource_filename('wheel.test', dist)
        os.chdir(distdir)
        try:
            sys.argv = ['', 'bdist_wheel']
            exec(compile(open('setup.py').read(), 'setup.py', 'exec'))
        finally:
            os.chdir(pwd) 
Example #27
Source File: test_basic.py    From jbox with MIT License 5 votes vote down vote up
def teardown_module():
    """Delete eggs/wheels created by tests."""
    base = pkg_resources.resource_filename('wheel.test', '')
    for dist in test_distributions:
        for subdir in ('build', 'dist'):
            try:
                rmtree(os.path.join(base, dist, subdir))
            except OSError:
                pass 
Example #28
Source File: pyfiles.py    From jbox with MIT License 5 votes vote down vote up
def coerce_resource_to_filename(fname):
    """Interpret a filename as either a filesystem location or as a package
    resource.

    Names that are non absolute paths and contain a colon
    are interpreted as resources and coerced to a file location.

    """
    if not os.path.isabs(fname) and ":" in fname:
        import pkg_resources
        fname = pkg_resources.resource_filename(*fname.split(':'))
    return fname 
Example #29
Source File: bdist_egg.py    From jbox with MIT License 5 votes vote down vote up
def write_stub(resource, pyfile):
    _stub_template = textwrap.dedent("""
        def __bootstrap__():
            global __bootstrap__, __loader__, __file__
            import sys, pkg_resources, imp
            __file__ = pkg_resources.resource_filename(__name__, %r)
            __loader__ = None; del __bootstrap__, __loader__
            imp.load_dynamic(__name__,__file__)
        __bootstrap__()
        """).lstrip()
    with open(pyfile, 'w') as f:
        f.write(_stub_template % resource) 
Example #30
Source File: ssl_support.py    From jbox with MIT License 5 votes vote down vote up
def find_ca_bundle():
    """Return an existing CA bundle path, or None"""
    if os.name=='nt':
        return get_win_certfile()
    else:
        for cert_path in cert_paths:
            if os.path.isfile(cert_path):
                return cert_path
    try:
        return pkg_resources.resource_filename('certifi', 'cacert.pem')
    except (ImportError, ResolutionError, ExtractionError):
        return None