Python pkg_resources.Requirement() Examples

The following are 28 code examples of pkg_resources.Requirement(). 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: proofpoint_common.py    From resilient-community-apps with MIT License 6 votes vote down vote up
def _get_template_file_path(options, default_path):
    """
    Get template file path.
    :return:
    """
    forensics_path = options.get("forensics_template", default_path)
    if forensics_path and not os.path.exists(forensics_path):
        log.warning(u"Template file '%s' not found.", forensics_path)
        forensics_path = None
    if not forensics_path:
        # Use the template file installed by this package
        forensics_path = resource_filename(Requirement("fn-proofpoint_tap"),
                                           "fn_proofpoint_tap/data/templates/pp_threat_forensics.jinja")
    if not os.path.exists(forensics_path):
        raise Exception(u"Template file '{}' not found".format(forensics_path))

    return forensics_path 
Example #2
Source File: utils.py    From requirementslib with MIT License 6 votes vote down vote up
def strip_extras_markers_from_requirement(req):
    # type: (TRequirement) -> TRequirement
    """
    Strips extras markers from requirement instances.

    Given a :class:`~packaging.requirements.Requirement` instance with markers defining
    *extra == 'name'*, strip out the extras from the markers and return the cleaned
    requirement

    :param PackagingRequirement req: A packaging requirement to clean
    :return: A cleaned requirement
    :rtype: PackagingRequirement
    """
    if req is None:
        raise TypeError("Must pass in a valid requirement, received {0!r}".format(req))
    if getattr(req, "marker", None) is not None:
        marker = req.marker  # type: TMarker
        marker._markers = _strip_extras_markers(marker._markers)
        if not marker._markers:
            req.marker = None
        else:
            req.marker = marker
    return req 
Example #3
Source File: pundle.py    From pundler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, line, env, source=None):
        self.line = line
        self.egg = None
        if isinstance(line, pkg_resources.Requirement):
            self.req = line
        elif test_vcs(line):
            res = parse_vcs_requirement(line)
            if not res:
                raise PundleException('Bad url %r' % line)
            egg, req, version = res
            self.egg = egg
            self.req = None  # pkg_resources.Requirement.parse(res)
        else:
            self.req = pkg_resources.Requirement.parse(line)
        self.sources = set([source])
        self.envs = set()
        self.add_env(env) 
Example #4
Source File: test_get_cmap.py    From terracotta with MIT License 6 votes vote down vote up
def test_get_cmap_filesystem(monkeypatch):
    import pkg_resources
    import importlib

    import terracotta.cmaps.get_cmaps

    def throw_error(*args, **kwargs):
        raise pkg_resources.DistributionNotFound('monkeypatched')

    with monkeypatch.context() as m:
        m.setattr(pkg_resources.Requirement, 'parse', throw_error)

        with pytest.raises(pkg_resources.DistributionNotFound):
            pkg_resources.Requirement.parse('terracotta')

        importlib.reload(terracotta.cmaps.get_cmaps)

        cmap = terracotta.cmaps.get_cmaps.get_cmap('jet')
        assert cmap.shape == (255, 4)
        assert cmap.dtype == np.uint8 
Example #5
Source File: setup.py    From timesketch with Apache License 2.0 6 votes vote down vote up
def parse_requirements_from_file(path):
    """Parses requirements from a requirements file.

    Args:
      path (str): path to the requirements file.

    Yields:
      pkg_resources.Requirement: package resource requirement.
    """
    with open(path, 'r') as file_object:
        file_contents = file_object.read()
    for req in pkg_resources.parse_requirements(file_contents):
        try:
            requirement = str(req.req)
        except AttributeError:
            requirement = str(req)
        yield requirement 
Example #6
Source File: utils.py    From pipenv with MIT License 6 votes vote down vote up
def strip_extras_markers_from_requirement(req):
    # type: (TRequirement) -> TRequirement
    """
    Strips extras markers from requirement instances.

    Given a :class:`~packaging.requirements.Requirement` instance with markers defining
    *extra == 'name'*, strip out the extras from the markers and return the cleaned
    requirement

    :param PackagingRequirement req: A packaging requirement to clean
    :return: A cleaned requirement
    :rtype: PackagingRequirement
    """
    if req is None:
        raise TypeError("Must pass in a valid requirement, received {0!r}".format(req))
    if getattr(req, "marker", None) is not None:
        marker = req.marker  # type: TMarker
        marker._markers = _strip_extras_markers(marker._markers)
        if not marker._markers:
            req.marker = None
        else:
            req.marker = marker
    return req 
Example #7
Source File: pundle.py    From pundler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def cmd_info():
    "prints info about Pundle state"
    parser_kw = create_parser_parameters()
    suite = create_parser(**parser_kw).create_suite()
    if suite.need_freeze():
        print('frozen.txt is outdated')
    else:
        print('frozen.txt is up to date')
    for state in suite.required_states():
        print(
            'Requirement "{}", frozen {}, {}'.format(
                state.key,
                state.frozen,
                state.requirement.line if state.requirement else 'None'
            )
        )
        print('Installed versions:')
        for dist in state.installed:
            print('    ', repr(dist))
        if not state.installed:
            print('     None') 
Example #8
Source File: poet.py    From homebrew-pypi-poet with MIT License 6 votes vote down vote up
def formula_for(package, also=None):
    also = also or []

    req = pkg_resources.Requirement.parse(package)
    package_name = req.project_name

    nodes = merge_graphs(make_graph(p) for p in [package] + also)
    resources = [value for key, value in nodes.items()
                 if key.lower() != package_name.lower()]

    if package_name in nodes:
        root = nodes[package_name]
    elif package_name.lower() in nodes:
        root = nodes[package_name.lower()]
    else:
        raise Exception("Could not find package {} in nodes {}".format(package, nodes.keys()))

    python = "python" if sys.version_info.major == 2 else "python3"
    return FORMULA_TEMPLATE.render(package=root,
                                   resources=resources,
                                   python=python,
                                   ResourceTemplate=RESOURCE_TEMPLATE) 
Example #9
Source File: __init__.py    From setupmeta with MIT License 5 votes vote down vote up
def pkg_req(text):
    """
    :param str|None text: Text to parse
    :return pkg_resources.Requirement|None: Corresponding parsed requirement, if valid
    """
    if text:
        try:
            return pkg_resources.Requirement(text)

        except Exception:
            return None 
Example #10
Source File: fn_pp_threat_polling.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def _parseopts(self, opts):
        """Parse and process configuration options, called from __init__ and _reload"""
        self.opts = opts
        self.options = opts.get("fn_proofpoint_tap", {})

        # Proofpoint score threshold
        self.score_threshold = float(self.options.get("score_threshold")) \
            if self.options.get("score_threshold") else None

        # Filter - Set types of incidents to import to Resilient
        self.id_type_filter = self._get_type_filter(self.options.get("type_filter", None))

        # Create a new Resilient incident from this event
        # using an optional JSON (JINJA2) template file
        threat_path = self.options.get("threat_template", self.default_path)
        if threat_path and not os.path.exists(threat_path):
            log.warning(u"Template file '%s' not found.", threat_path)
            threat_path = None
        if not threat_path:
            # Use the template file installed by this package
            threat_path = resource_filename(Requirement("fn-proofpoint_tap"), "fn_proofpoint_tap/data/templates/pp_threat_description.jinja")
            if not os.path.exists(threat_path):
                raise Exception(u"Template file '{}' not found".format(threat_path))

        log.info(u"Template file: %s", threat_path)
        with open(threat_path, "r") as threat_file:
            self.threat_template = threat_file.read()

        # initialize last update to startup interval if present, otherwise update interval
        interval = self.options.get('startup_interval')
        if interval is None or interval.strip() == "":
            self.lastupdate = None
        else:
            self.lastupdate = 60 * int(interval) 
Example #11
Source File: utils.py    From requirementslib with MIT License 5 votes vote down vote up
def init_requirement(name):
    # type: (AnyStr) -> TRequirement

    if not isinstance(name, six.string_types):
        raise TypeError("must supply a name to generate a requirement")
    from pkg_resources import Requirement

    req = Requirement.parse(name)
    req.vcs = None
    req.local_file = None
    req.revision = None
    req.path = None
    return req 
Example #12
Source File: rest_query.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def config_section_data():
    """sample config data for use in app.config"""
    section_config_fn = resource_filename(Requirement("rc-query-rest"), "query_runner/data/app.config.rest")
    query_dir = resource_filename(Requirement("rc-query-rest"), "query_runner/data/queries_rest")

    with open(section_config_fn, 'r') as section_config_file:
        section_config = Template(section_config_file.read())
        return section_config.safe_substitute(directory=query_dir) 
Example #13
Source File: poet.py    From homebrew-pypi-poet with MIT License 5 votes vote down vote up
def recursive_dependencies(package):
    if not isinstance(package, pkg_resources.Requirement):
        raise TypeError("Expected a Requirement; got a %s" % type(package))

    discovered = {package.project_name.lower()}
    visited = set()

    def walk(package):
        if not isinstance(package, pkg_resources.Requirement):
            raise TypeError("Expected a Requirement; got a %s" % type(package))
        if package in visited:
            return
        visited.add(package)
        extras = package.extras
        if package == "requests":
            extras += ("security",)
        try:
            reqs = pkg_resources.get_distribution(package).requires(extras)
        except pkg_resources.DistributionNotFound:
            return
        discovered.update(req.project_name.lower() for req in reqs)
        for req in reqs:
            walk(req)

    walk(package)
    return sorted(discovered) 
Example #14
Source File: ariel_query.py    From resilient-community-apps with MIT License 5 votes vote down vote up
def config_section_data():
    """sample config data for use in app.config"""
    section_config_fn = resource_filename(Requirement("rc-qradar-search"), "query_runner/data/app.config.qradar")
    query_dir = resource_filename(Requirement("rc-qradar-search"), "query_runner/data/queries_ariel")

    with open(section_config_fn, 'r') as section_config_file:
        section_config = Template(section_config_file.read())
        return section_config.safe_substitute(directory=query_dir) 
Example #15
Source File: poet.py    From homebrew-pypi-poet with MIT License 5 votes vote down vote up
def make_graph(pkg):
    """Returns a dictionary of information about pkg & its recursive deps.

    Given a string, which can be parsed as a requirement specifier, return a
    dictionary where each key is the name of pkg or one of its recursive
    dependencies, and each value is a dictionary returned by research_package.
    (No, it's not really a graph.)
    """
    ignore = ['argparse', 'pip', 'setuptools', 'wsgiref']
    pkg_deps = recursive_dependencies(pkg_resources.Requirement.parse(pkg))

    dependencies = {key: {} for key in pkg_deps if key not in ignore}
    installed_packages = pkg_resources.working_set
    versions = {package.key: package.version for package in installed_packages}
    for package in dependencies:
        try:
            dependencies[package]['version'] = versions[package]
        except KeyError:
            warnings.warn("{} is not installed so we cannot compute "
                          "resources for its dependencies.".format(package),
                          PackageNotInstalledWarning)
            dependencies[package]['version'] = None

    for package in dependencies:
        package_data = research_package(package, dependencies[package]['version'])
        dependencies[package].update(package_data)

    return OrderedDict(
        [(package, dependencies[package]) for package in sorted(dependencies.keys())]
    ) 
Example #16
Source File: utils.py    From requirementslib with MIT License 5 votes vote down vote up
def _requirement_to_str_lowercase_name(requirement):
    """Formats a packaging.requirements.Requirement with a lowercase name.

    This is simply a copy of
    https://github.com/pypa/packaging/blob/16.8/packaging/requirements.py#L109-L124
    modified to lowercase the dependency name.

    Previously, we were invoking the original Requirement.__str__ method and
    lower-casing the entire result, which would lowercase the name, *and* other,
    important stuff that should not be lower-cased (such as the marker). See
    this issue for more information: https://github.com/pypa/pipenv/issues/2113.
    """
    parts = [requirement.name.lower()]

    if requirement.extras:
        parts.append("[{0}]".format(",".join(sorted(requirement.extras))))

    if requirement.specifier:
        parts.append(str(requirement.specifier))

    if requirement.url:
        parts.append("@ {0}".format(requirement.url))

    if requirement.marker:
        parts.append("; {0}".format(requirement.marker))

    return "".join(parts) 
Example #17
Source File: utils.py    From requirementslib with MIT License 5 votes vote down vote up
def get_setuptools_version():
    # type: () -> Optional[STRING_TYPE]
    import pkg_resources

    setuptools_dist = pkg_resources.get_distribution(
        pkg_resources.Requirement("setuptools")
    )
    return getattr(setuptools_dist, "version", None) 
Example #18
Source File: utils.py    From requirementslib with MIT License 5 votes vote down vote up
def parse_extras(extras_str):
    # type: (AnyStr) -> List[AnyStr]
    """Turn a string of extras into a parsed extras list

    :param str extras_str: An extras string
    :return: A sorted list of extras
    :rtype: List[str]
    """

    from pkg_resources import Requirement

    extras = Requirement.parse("fakepkg{0}".format(extras_to_string(extras_str))).extras
    return sorted(dedup([extra.lower() for extra in extras])) 
Example #19
Source File: cli.py    From dwave-cloud-client with Apache License 2.0 5 votes vote down vote up
def _get_dist(dist_spec):
    """Returns `pkg_resources.Distribution` object for matching `dist_spec`,
    which can be given as `pkg_resources.Requirement`, or an unparsed string
    requirement.
    """
    return pkg_resources.get_distribution(dist_spec) 
Example #20
Source File: wheel.py    From rules_python_external with Apache License 2.0 5 votes vote down vote up
def dependencies(self, extras_requested: Optional[Set[str]] = None) -> Set[str]:
        dependency_set = set()

        for wheel_req in self.metadata.requires_dist:
            req = pkg_resources.Requirement(wheel_req)  # type: ignore

            if req.marker is None or any(
                req.marker.evaluate({"extra": extra})
                for extra in extras_requested or [""]
            ):
                dependency_set.add(req.name)  # type: ignore

        return dependency_set 
Example #21
Source File: threat_webservice.py    From resilient-python-api with MIT License 5 votes vote down vote up
def config_section_data():
    """sample config data for use in app.config"""
    section_config_fn = resource_filename(Requirement("rc-cts"), "rc_cts/data/app.config.cts")
    with open(section_config_fn, 'r') as section_config_file:
        return section_config_file.read() 
Example #22
Source File: utils.py    From pipenv with MIT License 5 votes vote down vote up
def _requirement_to_str_lowercase_name(requirement):
    """Formats a packaging.requirements.Requirement with a lowercase name.

    This is simply a copy of
    https://github.com/pypa/packaging/blob/16.8/packaging/requirements.py#L109-L124
    modified to lowercase the dependency name.

    Previously, we were invoking the original Requirement.__str__ method and
    lower-casing the entire result, which would lowercase the name, *and* other,
    important stuff that should not be lower-cased (such as the marker). See
    this issue for more information: https://github.com/pypa/pipenv/issues/2113.
    """
    parts = [requirement.name.lower()]

    if requirement.extras:
        parts.append("[{0}]".format(",".join(sorted(requirement.extras))))

    if requirement.specifier:
        parts.append(str(requirement.specifier))

    if requirement.url:
        parts.append("@ {0}".format(requirement.url))

    if requirement.marker:
        parts.append("; {0}".format(requirement.marker))

    return "".join(parts) 
Example #23
Source File: utils.py    From pipenv with MIT License 5 votes vote down vote up
def get_setuptools_version():
    # type: () -> Optional[STRING_TYPE]
    import pkg_resources

    setuptools_dist = pkg_resources.get_distribution(
        pkg_resources.Requirement("setuptools")
    )
    return getattr(setuptools_dist, "version", None) 
Example #24
Source File: utils.py    From pipenv with MIT License 5 votes vote down vote up
def parse_extras(extras_str):
    # type: (AnyStr) -> List[AnyStr]
    """Turn a string of extras into a parsed extras list

    :param str extras_str: An extras string
    :return: A sorted list of extras
    :rtype: List[str]
    """

    from pkg_resources import Requirement

    extras = Requirement.parse("fakepkg{0}".format(extras_to_string(extras_str))).extras
    return sorted(dedup([extra.lower() for extra in extras])) 
Example #25
Source File: utils.py    From pipenv with MIT License 5 votes vote down vote up
def init_requirement(name):
    # type: (AnyStr) -> TRequirement

    if not isinstance(name, six.string_types):
        raise TypeError("must supply a name to generate a requirement")
    from pkg_resources import Requirement

    req = Requirement.parse(name)
    req.vcs = None
    req.local_file = None
    req.revision = None
    req.path = None
    return req 
Example #26
Source File: libraryversions.py    From recipy with Apache License 2.0 5 votes vote down vote up
def _get_version_from_pkg_resources(modulename):
    ws = pkg_resources.working_set
    package = ws.find(pkg_resources.Requirement(modulename))
    try:
        version = package.version
    except AttributeError:
        version = '?'
    return version 
Example #27
Source File: pundle.py    From pundler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def adjust_with_req(self, req):
        if not self.req:
            return
            raise PundleException('VCS')
        versions = ','.join(''.join(t) for t in set(self.req.specs + req.req.specs))
        self.requirement = pkg_resources.Requirement.parse('{} {}'.format(
            self.req.project_name, versions
        ))
        self.sources.update(req.sources)
        self.add_env(req.envs) 
Example #28
Source File: bit9_poll.py    From resilient-community-apps with MIT License 4 votes vote down vote up
def _process_approval_request(self, event):
        # Process one approval request
        log = self.log
        request = event.request
        request_id = request["id"]

        # special "test the process by escalating a single request" mode
        test_single_request = self.options.get("test_single_request")
        if test_single_request:
            if str(request_id) not in str(test_single_request).split(","):
                log.info(u"Skipping request %s, test", request_id)
                return

        # Find the Resilient incident corresponding to this CbProtect approval request (if available)
        resilient_incident = self._find_resilient_incident_for_req(request_id)
        if resilient_incident:
            log.info(u"Skipping request %s, already escalated", request_id)
            return

        log.info(u"Processing request %s", request_id)
        try:
            # Create a new Resilient incident from this approval request
            # using a JSON (JINJA2) template file
            template_file_path = self.options.get("template_file")
            if template_file_path and not os.path.exists(template_file_path):
                log.warn(u"Template file '%s' not found.", template_file_path)
                template_file_path = None
            if not template_file_path:
                # Use the template file installed by this package
                template_file_path = resource_filename(Requirement("fn-cb-protection"),
                                                       "fn_cb_protection/data/template.jinja")
                if not os.path.exists(template_file_path):
                    raise Exception(u"Template file '{}' not found".format(template_file_path))

            log.info(u"Template file: %s", template_file_path)
            with open(template_file_path, "r") as definition:
                escalate_template = definition.read()

            # Render the template.  Be sure to set the CbProtect ID in the result!
            new_resilient_inc = render_json(escalate_template, request)
            new_resilient_inc["properties"][REQUEST_ID_FIELDNAME] = request_id

            log.debug(new_resilient_inc)
            inc = self.rest_client().post("/incidents", new_resilient_inc)
            rs_inc_id = inc["id"]
            message = u"Created incident {} for CbProtect {}".format(rs_inc_id, request_id)
            log.info(message)

        except Exception as exc:
            log.exception(exc)
            raise