Unverified Commit 5ca1144a authored by Nate Coraor's avatar Nate Coraor
Browse files

Don't reuse mulled-search cache indefinitely

parent 5661b6a7
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import logging
import os
import sys
import tempfile
import time
from typing import (
    Dict,
    List,
@@ -48,9 +49,10 @@ class QuaySearch:
    """
    tmp_dir_prefix = "mulled_search.quay."

    def __init__(self, organization):
    def __init__(self, organization, cache_time=900):
        self.index = None
        self.organization = organization
        self.cache_time = cache_time

    def paginator(self):
        # download all information about the repositories from the
@@ -74,6 +76,7 @@ class QuaySearch:
        """
        entries = []
        uid = os.getuid()
        now = time.time()
        for entry in os.scandir(tempfile.gettempdir()):
            if not entry.name.startswith(QuaySearch.tmp_dir_prefix):
                continue
@@ -82,10 +85,13 @@ class QuaySearch:
            entry_st = entry.stat()
            if entry_st.st_uid != uid:
                continue
            if entry_st.st_mtime < (now - self.cache_time):
                continue
            entries.append((entry_st.st_mtime, entry.path))
        if entries:
            tmp_dir = sorted(entries)[-1][1]
            print(f"Using existing quay.io index, remove to regenerate: {tmp_dir}", file=sys.stderr)
            print(f"Using existing quay.io index, remove or decrease --cache-time to rebuild: {tmp_dir}",
                  file=sys.stderr)
            self.index = open_dir(tmp_dir)
        else:
            tmp_dir = tempfile.mkdtemp(prefix=QuaySearch.tmp_dir_prefix)
@@ -414,6 +420,7 @@ def main(argv=None):
        help="Autocorrection of typos activated. Lists more results but can be confusing.\
                        For too many queries quay.io blocks the request and the results can be incomplete.",
    )
    parser.add_argument("--cache-time", type=int, default=900, help="Number of seconds to reuse cached results for")
    parser.add_argument("-j", "--json", dest="json", action="store_true", help="Returns results as JSON.")
    parser.add_argument("-s", "--search", required=True, nargs="+", help="The name of the tool(s) to search for.")

@@ -449,7 +456,7 @@ def main(argv=None):

    if "quay" in args.search_dest:
        quay_results = {}
        quay = QuaySearch(args.organization_string)
        quay = QuaySearch(args.organization_string, cache_time=args.cache_time)
        quay.build_index()

        for item in args.search: