def locate(self, requirement, prereleases=False):
result = None<EOL>r = parse_requirement(requirement)<EOL>if r is None:<EOL><INDENT>raise DistlibException('<STR_LIT>' % requirement)<EOL><DEDENT>scheme = get_scheme(self.scheme)<EOL>self.matcher = matcher = scheme.matcher(r.requirement)<EOL>logger.debug('<STR_LIT>', matcher, type(matcher).__name__)<EOL>versions = self.get_project(r.name)<EOL>if len(versions) > <NUM_LIT:2>:   <EOL><INDENT>slist = []<EOL>vcls = matcher.version_class<EOL>for k in versions:<EOL><INDENT>if k in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>if not matcher.match(k):<EOL><INDENT>logger.debug('<STR_LIT>', matcher, k)<EOL><DEDENT>else:<EOL><INDENT>if prereleases or not vcls(k).is_prerelease:<EOL><INDENT>slist.append(k)<EOL><DEDENT>else:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>', k, matcher.name)<EOL><DEDENT><DEDENT><DEDENT>except Exception:  <EOL><INDENT>logger.warning('<STR_LIT>', matcher, k)<EOL>pass <EOL><DEDENT><DEDENT>if len(slist) > <NUM_LIT:1>:<EOL><INDENT>slist = sorted(slist, key=scheme.key)<EOL><DEDENT>if slist:<EOL><INDENT>logger.debug('<STR_LIT>', slist)<EOL>version = slist[-<NUM_LIT:1>]<EOL>result = versions[version]<EOL><DEDENT><DEDENT>if result:<EOL><INDENT>if r.extras:<EOL><INDENT>result.extras = r.extras<EOL><DEDENT>result.download_urls = versions.get('<STR_LIT>', {}).get(version, set())<EOL>d = {}<EOL>sd = versions.get('<STR_LIT>', {})<EOL>for url in result.download_urls:<EOL><INDENT>if url in sd:<EOL><INDENT>d[url] = sd[url]<EOL><DEDENT><DEDENT>result.digests = d<EOL><DEDENT>self.matcher = None<EOL>return result<EOL>
Find the most recent distribution which matches the given
requirement.

:param requirement: A requirement of the form 'foo (1.0)' or perhaps
                    'foo (>= 1.0, < 2.0, != 1.3)'
:param prereleases: If ``True``, allow pre-release versions
                    to be located. Otherwise, pre-release versions
                    are not returned.
:return: A :class:`Distribution` instance, or ``None`` if no such
         distribution could be located.