# Buggy code
def packages_from_requirement_cached(
    local_iterator, ttl, iterator, requirement, *args, **kw
):
    packages = packages_from_requirement(local_iterator, requirement, *args, **kw)

    if packages:
        # match with exact requirement, always accept.
        if requirement_is_exact(requirement):
            TRACER.log("Package cache hit: %s" % requirement, V=3)
            return packages

        # match with inexact requirement, consider if ttl supplied.
        if ttl:
            now = time.time()
            packages = [
                package
                for package in packages
                if (now - os.path.getmtime(package.path)) < ttl
            ]
            if packages:
                TRACER.log("Package cache hit (inexact): %s" % requirement, V=3)
                return packages

    # no matches in the local cache
    TRACER.log("Package cache miss: %s" % requirement, V=3)
    return packages_from_requirement(iterator, requirement, *args, **kw)


# Traceback (OSError)
mba=pex=; pex -r pytest -r setuptools -r py==1.4.25 -o /tmp/pt.pex --cache-ttl=3600 -v -v -v -v -v
pex: Package cache hit (inexact): pytest
pex: Package cache hit (inexact): setuptools
pex: Package cache miss: py==1.4.25
pex: Resolving distributions :: Fetching https://pypi.python.org/packages/source/p/py/py-1.4.25.tar.gz#md5pex: Validated py-1.4.25.tar.gz (md5=1f0b84dab1681fc1bf07c0528e091efb)
pex: Validated py-1.4.25.tar.gz (md5=1f0b84dab1681fc1bf07c0528e091efb)
pex: Resolving distributions :: Packaging py :: Installing /var/folders/rd/_tjz8zts3g14md1kmf38z6w80000gn/pex: Resolving distributions :: Packaging pytest                                                          pex: Resolving distributions :: Packaging pytest :: Installing /var/folders/rd/_tjz8zts3g14md1kmf38z6w8000pex: Resolving distributions :: Resolving py>=1.4.25                                                      Traceback (most recent call last):
File "/Users/wickman/Local/bin/pex/.bootstrap/_pex/pex.py", line 272, in execute
self.execute_entry(entry_point, args)
File "/Users/wickman/Local/bin/pex/.bootstrap/_pex/pex.py", line 320, in execute_entry
runner(entry_point)
File "/Users/wickman/Local/bin/pex/.bootstrap/_pex/pex.py", line 343, in execute_pkg_resources
runner()
File "/Users/wickman/.pex/install/pex-0.8.0.dev0-py2.py3-none-any.whl.0d5be588022f0aad6de2b84b77a8a890c43ad942/pex-0.8.0.dev0-py2.py3-none-any.whl/pex/bin/pex.py", line 325, in main
pex_builder = build_pex(args, options)
File "/Users/wickman/.pex/install/pex-0.8.0.dev0-py2.py3-none-any.whl.0d5be588022f0aad6de2b84b77a8a890c43ad942/pex-0.8.0.dev0-py2.py3-none-any.whl/pex/bin/pex.py", line 303, in build_pex
cache_ttl=options.cache_ttl)
File "/Users/wickman/.pex/install/pex-0.8.0.dev0-py2.py3-none-any.whl.0d5be588022f0aad6de2b84b77a8a890c43ad942/pex-0.8.0.dev0-py2.py3-none-any.whl/pex/resolver.py", line 215, in resolve
existing=distribution_set.get(requirement.key))
File "/Users/wickman/.pex/install/pex-0.8.0.dev0-py2.py3-none-any.whl.0d5be588022f0aad6de2b84b77a8a890c43ad942/pex-0.8.0.dev0-py2.py3-none-any.whl/pex/resolver.py", line 94, in packages_from_requirement_cached
packages = [package for package in packages if (now - os.path.getmtime(package.path)) < ttl]
File "/Users/wickman/Python/CPython-2.7.8/lib/python2.7/genericpath.py", line 54, in getmtime
return os.stat(filename).st_mtime
OSError: [Errno 2] No such file or directory: '/packages/source/p/py/py-1.4.25.tar.gz'

# Fixed code
def packages_from_requirement_cached(
    local_iterator, ttl, iterator, requirement, *args, **kw
):
    packages = packages_from_requirement(local_iterator, requirement, *args, **kw)

    if packages:
        # match with exact requirement, always accept.
        if requirement_is_exact(requirement):
            TRACER.log("Package cache hit: %s" % requirement, V=3)
            return packages

        # match with inexact requirement, consider if ttl supplied.
        if ttl:
            now = time.time()
            packages = [
                package
                for package in packages
                if package.remote
                or package.local
                and (now - os.path.getmtime(package.path)) < ttl
            ]
            if packages:
                TRACER.log("Package cache hit (inexact): %s" % requirement, V=3)
                return packages

    # no matches in the local cache
    TRACER.log("Package cache miss: %s" % requirement, V=3)
    return packages_from_requirement(iterator, requirement, *args, **kw)