From 02c955c702723236d34f01d4e6fe4c9b5da22a3a Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Tue, 12 Mar 2013 19:47:20 +0100 Subject: [PATCH] Check for cache freshness --- yolk/pypi.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/yolk/pypi.py b/yolk/pypi.py index 3ba3ba0..eecb8ac 100644 --- a/yolk/pypi.py +++ b/yolk/pypi.py @@ -1,4 +1,3 @@ - """ pypi.py @@ -26,6 +25,7 @@ import xmlrpc.client as xmlrpclib import pickle import urllib.request as urllib2 +import sys import os import time import logging @@ -208,9 +208,18 @@ def query_cached_package_list(self): def fetch_pkg_list(self): """Fetch and cache master list of package names from PYPI""" self.logger.debug("DEBUG: Fetching package name list from PyPI") - package_list = self.list_packages() - cPickle.dump(package_list, open(self.pkg_cache_file, "w")) - self.pkg_list = package_list + + if os.path.exists(self.pkg_cache_file): + st = os.stat(self.pkg_cache_file) + age = time.time() - st.st_mtime + else: + age = sys.maxint + + # Only fetch from PyPI if cache older than an hour + if age > 60 * 60: + package_list = self.list_packages() + cPickle.dump(package_list, open(self.pkg_cache_file, "w")) + self.pkg_list = package_list def search(self, spec, operator): '''Query PYPI via XMLRPC interface using search spec'''