MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / fetch_newer_version

Function fetch_newer_version

src/scancode/outdated.py:158–218  ·  view source on GitHub ↗

Return a version string if there is an updated version of scancode-toolkit newer than the installed version and available on PyPI. Return None otherwise. Limit the frequency of update checks to once per week. State is stored in the scancode_cache_dir. If `force` is True, red

(
    installed_version=scancode_version,
    new_version_url='https://pypi.org/pypi/scancode-toolkit/json',
    force=False,
)

Source from the content-addressed store, hash-verified

156
157
158def fetch_newer_version(
159 installed_version=scancode_version,
160 new_version_url='https://pypi.org/pypi/scancode-toolkit/json',
161 force=False,
162):
163 """
164 Return a version string if there is an updated version of scancode-toolkit
165 newer than the installed version and available on PyPI. Return None
166 otherwise.
167 Limit the frequency of update checks to once per week.
168 State is stored in the scancode_cache_dir.
169 If `force` is True, redo a PyPI remote check.
170 """
171 # If installed version is from git describe, only use the first part of it
172 # i.e. `v31.2.1-343-gd9d2e19e32` -> `v31.2.1`
173 if "-" in installed_version:
174 installed_version = installed_version.split("-")[0]
175
176 try:
177 installed_version = packaging_version.parse(installed_version)
178 state = VersionCheckState()
179
180 current_time = datetime.datetime.utcnow()
181 # Determine if we need to refresh the state
182 if ('last_check' in state.state and 'latest_version' in state.state):
183 last_check = datetime.datetime.strptime(
184 state.state['last_check'],
185 SELFCHECK_DATE_FMT
186 )
187 seconds_since_last_check = total_seconds(current_time - last_check)
188 one_week = 7 * 24 * 60 * 60
189 if seconds_since_last_check < one_week:
190 latest_version = state.state['latest_version']
191
192 if force:
193 latest_version = None
194
195 # Refresh the version if we need to or just see if we need to warn
196 if latest_version is None:
197 try:
198 latest_version = fetch_latest_version(new_version_url)
199 state.save(latest_version, current_time)
200 except Exception:
201 # save an empty version to avoid checking more than once a week
202 state.save(None, current_time)
203 raise
204
205 latest_version = packaging_version.parse(latest_version)
206
207 if TRACE:
208 logger_debug(f"installed_version: {installed_version}, latest version: {latest_version}")
209 logger_debug(f"installed_version < latest_version: {installed_version < latest_version}")
210
211 # Determine if our latest_version is older
212 if (installed_version < latest_version
213 and installed_version.base_version != latest_version.base_version):
214 return str(latest_version)
215

Callers 1

check_scancode_versionFunction · 0.85

Calls 8

saveMethod · 0.95
VersionCheckStateClass · 0.85
total_secondsFunction · 0.85
fetch_latest_versionFunction · 0.85
logger_debugFunction · 0.70
splitMethod · 0.45
parseMethod · 0.45
debugMethod · 0.45

Tested by

no test coverage detected