error handling for webservice issues and unpublished packages
This commit is contained in:
parent
78988233f4
commit
403cf48502
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
# https://www.npmjs.com/package/darcyclarke-manifest-pkg/v/2.1.15/index
|
# https://www.npmjs.com/package/darcyclarke-manifest-pkg/v/2.1.15/index
|
||||||
# hex checksum = file name
|
# hex checksum = file name
|
||||||
@ -13,7 +14,11 @@ def parse_manifest(pkg):
|
|||||||
parsed = json.loads(requests.get(url).text)
|
parsed = json.loads(requests.get(url).text)
|
||||||
|
|
||||||
# extract the interesting bits
|
# extract the interesting bits
|
||||||
|
try:
|
||||||
latest_ver = parsed['dist-tags']['latest']
|
latest_ver = parsed['dist-tags']['latest']
|
||||||
|
except KeyError:
|
||||||
|
print(f'Failed to find latest version for {pkg} - might have been unpublished?')
|
||||||
|
return None, None, None, None
|
||||||
latest_manifest = parsed['versions'][latest_ver]
|
latest_manifest = parsed['versions'][latest_ver]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -23,7 +28,7 @@ def parse_manifest(pkg):
|
|||||||
try:
|
try:
|
||||||
scripts = latest_manifest['scripts']
|
scripts = latest_manifest['scripts']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
scripts = None
|
scripts = json.loads('{}')
|
||||||
name = latest_manifest['name']
|
name = latest_manifest['name']
|
||||||
|
|
||||||
return latest_ver, dependencies, scripts, name
|
return latest_ver, dependencies, scripts, name
|
||||||
@ -33,12 +38,27 @@ def get_actual_manifest(pkg, ver):
|
|||||||
# get and parse the manifest as it would be installed
|
# get and parse the manifest as it would be installed
|
||||||
# first, we need to find the package.json delivered with the package:
|
# first, we need to find the package.json delivered with the package:
|
||||||
index_url = 'https://www.npmjs.com/package/{}/v/{}/index'.format(pkg, ver)
|
index_url = 'https://www.npmjs.com/package/{}/v/{}/index'.format(pkg, ver)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
index = json.loads(requests.get(index_url).text)
|
index = json.loads(requests.get(index_url).text)
|
||||||
|
break
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
print('Failed to get index from webservice, retrying...')
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
hexsum = index['files']['/package.json']['hex']
|
hexsum = index['files']['/package.json']['hex']
|
||||||
manifest_url = 'https://www.npmjs.com/package/{}/file/{}'.format(pkg, hexsum)
|
manifest_url = 'https://www.npmjs.com/package/{}/file/{}'.format(pkg, hexsum)
|
||||||
|
|
||||||
# now we can parse it
|
# Sometimes the webservice seems to respond with an empty json - so this kludge got made:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
manifest = json.loads(requests.get(manifest_url).text)
|
manifest = json.loads(requests.get(manifest_url).text)
|
||||||
|
break
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
print('Failed getting manifest JSON from webserver, retrying...')
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# now we can parse it
|
||||||
version = manifest['version']
|
version = manifest['version']
|
||||||
try:
|
try:
|
||||||
dependencies = manifest['dependencies']
|
dependencies = manifest['dependencies']
|
||||||
@ -47,7 +67,7 @@ def get_actual_manifest(pkg, ver):
|
|||||||
try:
|
try:
|
||||||
scripts = manifest['scripts']
|
scripts = manifest['scripts']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
scripts = None
|
scripts = json.loads('{}')
|
||||||
name = manifest['name']
|
name = manifest['name']
|
||||||
|
|
||||||
return version, dependencies, scripts, name
|
return version, dependencies, scripts, name
|
||||||
@ -58,6 +78,9 @@ def main():
|
|||||||
mismatch = False
|
mismatch = False
|
||||||
pkg = sys.argv[1]
|
pkg = sys.argv[1]
|
||||||
reported_ver, reported_dependencies, reported_scripts, reported_name = parse_manifest(pkg)
|
reported_ver, reported_dependencies, reported_scripts, reported_name = parse_manifest(pkg)
|
||||||
|
|
||||||
|
if reported_ver == None:
|
||||||
|
sys.exit(2)
|
||||||
actual_ver, actual_dependencies, actual_scripts, actual_name = get_actual_manifest(pkg, reported_ver)
|
actual_ver, actual_dependencies, actual_scripts, actual_name = get_actual_manifest(pkg, reported_ver)
|
||||||
|
|
||||||
if actual_ver != reported_ver:
|
if actual_ver != reported_ver:
|
||||||
|
Loading…
Reference in New Issue
Block a user