Skip to content

clear_download_cache() does not work in Python 2.7 #4427

Closed

Description

@taldcroft

In Python 2.7, clearing the download cache runs into two problems, related to #4426. First:

In [1]: astro
1.1

In [2]: from astropy.utils import data

In [3]: !rm -rf /Users/aldcroft/.astropy/cache

In [4]: f = data.download_file('http://google.com', cache=True)
Downloading http://google.com [Done]

In [5]: data.clear_download_cache()

In [6]: f = data.download_file('http://google.com', cache=True)

In [7]: import os

In [8]: os.path.exists(f)
Out[8]: False

This is because the shelve file name without the .db extension is removed if it exists (which it doesn't, but that is not caught in the code). This relates to a fundamental issue with shelve on Python 2, which is that you don't know (and cannot determine in general) the name of the backing database file(s). This could be fixed by putting them into a sub-directory with a known name and then clobbering that.

Next, if I try harder (with the actual URL) it generates an exception:

In [9]: data.clear_download_cache('http://google.com')
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-9-1e289daec783> in <module>()
----> 1 data.clear_download_cache('http://google.com')

/Users/aldcroft/anaconda/lib/python2.7/site-packages/astropy/utils/data.pyc in clear_download_cache(hashorurl)
   1268                     filepath = url2hash[hash_key]
   1269                     del url2hash[hash_key]
-> 1270                     os.unlink(filepath)
   1271                 else:
   1272                     msg = 'Could not find file or url {0}'

OSError: [Errno 2] No such file or directory: '/Users/aldcroft/.astropy/cache/download/42afb277772da0f1a943e37fd64ec317'

cc: @eteq

Activity

  1. embray commented on Jan 4, 2016

    @embray
    Member

    The subdirectory idea sounds viable.

  2. taldcroft commented on Jan 4, 2016

    @taldcroft
    MemberAuthor

    Yes, and maybe having something like download_db/py2/ and download_db/py3/ directories would work for #4426. Unfortunately this means that files can get downloaded twice (as previously mentioned in discussions of using a different persistence method for the cache names / data).

  3. embray commented on Jan 4, 2016

    @embray
    Member

    I think that for now the double download problem will only be an issue for a minority of users though. It's mostly just those of us doing heavy development that are using multiple Python versions on a regular basis, I imagine...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions