Skip to content

Commit a3fa52e

Browse files
committed
Fixing licenses cache;
1 parent 2e5b46b commit a3fa52e

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

safety/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def review(full_report, bare, file):
123123

124124

125125
@cli.command()
126-
@click.option("--key", default="", envvar="SAFETY_API_KEY",
126+
@click.option("--key", required=True, envvar="SAFETY_API_KEY",
127127
help="API Key for pyup.io's vulnerability database. Can be set as SAFETY_API_KEY "
128128
"environment variable. Default: empty")
129129
@click.option("--db", default="",

safety/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
CACHE_VALID_SECONDS = 60 * 60 * 2 # 2 hours
1515

16+
CACHE_LICENSES_VALID_SECONDS = 60 * 60 * 24 * 7 # one week
17+
1618
CACHE_FILE = os.path.join(
1719
os.path.expanduser("~"),
1820
".safety",

safety/safety.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import requests
99
from packaging.specifiers import SpecifierSet
1010

11-
from .constants import (API_MIRRORS, CACHE_FILE, CACHE_VALID_SECONDS,
12-
OPEN_MIRRORS, REQUEST_TIMEOUT)
11+
from .constants import (API_MIRRORS, CACHE_FILE, CACHE_LICENSES_VALID_SECONDS,
12+
CACHE_VALID_SECONDS, OPEN_MIRRORS, REQUEST_TIMEOUT)
1313
from .errors import (DatabaseFetchError, DatabaseFileNotFoundError,
1414
InvalidKeyError)
1515
from .util import RequirementFile
@@ -27,7 +27,13 @@ def get_from_cache(db_name):
2727
data = json.loads(f.read())
2828
if db_name in data:
2929
if "cached_at" in data[db_name]:
30-
if data[db_name]["cached_at"] + CACHE_VALID_SECONDS > time.time():
30+
if 'licenses.json' in db_name:
31+
# Getting the specific cache time for the licenses db.
32+
cache_valid_seconds = CACHE_LICENSES_VALID_SECONDS
33+
else:
34+
cache_valid_seconds = CACHE_VALID_SECONDS
35+
36+
if data[db_name]["cached_at"] + cache_valid_seconds > time.time():
3137
return data[db_name]["db"]
3238
except json.JSONDecodeError:
3339
pass

0 commit comments

Comments
 (0)