def expand_requirement(request, paths=None):
if '<STR_LIT:*>' not in request:<EOL><INDENT>return request<EOL><DEDENT>from rez.vendor.version.version import VersionRange<EOL>from rez.vendor.version.requirement import Requirement<EOL>from rez.packages_ import get_latest_package<EOL>from uuid import uuid4<EOL>wildcard_map = {}<EOL>expanded_versions = {}<EOL>request_ = request<EOL>while "<STR_LIT>" in request_:<EOL><INDENT>uid = "<STR_LIT>" % uuid4().hex<EOL>request_ = request_.replace("<STR_LIT>", uid, <NUM_LIT:1>)<EOL>wildcard_map[uid] = "<STR_LIT>"<EOL><DEDENT>while '<STR_LIT:*>' in request_:<EOL><INDENT>uid = "<STR_LIT>" % uuid4().hex<EOL>request_ = request_.replace('<STR_LIT:*>', uid, <NUM_LIT:1>)<EOL>wildcard_map[uid] = '<STR_LIT:*>'<EOL><DEDENT>req = Requirement(request_, invalid_bound_error=False)<EOL>def expand_version(version):<EOL><INDENT>rank = len(version)<EOL>wildcard_found = False<EOL>while version and str(version[-<NUM_LIT:1>]) in wildcard_map:<EOL><INDENT>token = wildcard_map[str(version[-<NUM_LIT:1>])]<EOL>version = version.trim(len(version) - <NUM_LIT:1>)<EOL>if token == "<STR_LIT>":<EOL><INDENT>if wildcard_found:  <EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>wildcard_found = True<EOL>rank = <NUM_LIT:0><EOL>break<EOL><DEDENT><DEDENT>wildcard_found = True<EOL><DEDENT>if not wildcard_found:<EOL><INDENT>return None<EOL><DEDENT>range_ = VersionRange(str(version))<EOL>package = get_latest_package(name=req.name, range_=range_, paths=paths)<EOL>if package is None:<EOL><INDENT>return version<EOL><DEDENT>if rank:<EOL><INDENT>return package.version.trim(rank)<EOL><DEDENT>else:<EOL><INDENT>return package.version<EOL><DEDENT><DEDENT>def visit_version(version):<EOL><INDENT>for v, expanded_v in expanded_versions.iteritems():<EOL><INDENT>if version == v.next():<EOL><INDENT>return expanded_v.next()<EOL><DEDENT><DEDENT>version_ = expand_version(version)<EOL>if version_ is None:<EOL><INDENT>return None<EOL><DEDENT>expanded_versions[version] = version_<EOL>return version_<EOL><DEDENT>if req.range_ is not None:<EOL><INDENT>req.range_.visit_versions(visit_version)<EOL><DEDENT>result = str(req)<EOL>for uid, token in wildcard_map.iteritems():<EOL><INDENT>result = result.replace(uid, token)<EOL><DEDENT>expanded_req = Requirement(result)<EOL>return str(expanded_req)<EOL>
Expands a requirement string like 'python-2.*', 'foo-2.*+<*', etc.

    Wildcards are expanded to the latest version that matches. There is also a
    special wildcard '**' that will expand to the full version, but it cannot
    be used in combination with '*'.

    Wildcards MUST placehold a whole version token, not partial - while 'foo-2.*'
    is valid, 'foo-2.v*' is not.

    Wildcards MUST appear at the end of version numbers - while 'foo-1.*.*' is
    valid, 'foo-1.*.0' is not.

    It is possible that an expansion will result in an invalid request string
    (such as 'foo-2+<2'). The appropriate exception will be raised if this
    happens.

    Examples:

        >>> print expand_requirement('python-2.*')
        python-2.7
        >>> print expand_requirement('python==2.**')
        python==2.7.12
        >>> print expand_requirement('python<**')
        python<3.0.5

    Args:
        request (str): Request to expand, eg 'python-2.*'
        paths (list of str, optional): paths to search for package families,
            defaults to `config.packages_path`.

    Returns:
        str: Expanded request string.