diff --git a/support/scripts/pkg-stats b/support/scripts/pkg-stats index d0b06b1e74..edc78b827b 100755 --- a/support/scripts/pkg-stats +++ b/support/scripts/pkg-stats @@ -25,11 +25,19 @@ import re import subprocess import sys import requests # URL checking +import json +import certifi +from urllib3 import HTTPSConnectionPool +from urllib3.exceptions import HTTPError from multiprocessing import Pool INFRA_RE = re.compile("\$\(eval \$\(([a-z-]*)-package\)\)") URL_RE = re.compile("\s*https?://\S*\s*$") +RM_API_STATUS_ERROR = 1 +RM_API_STATUS_FOUND_BY_DISTRO = 2 +RM_API_STATUS_FOUND_BY_PATTERN = 3 +RM_API_STATUS_NOT_FOUND = 4 class Package: all_licenses = list() @@ -49,6 +57,7 @@ class Package: self.url = None self.url_status = None self.url_worker = None + self.latest_version = (RM_API_STATUS_ERROR, None, None) def pkgvar(self): return self.name.upper().replace("-", "_") @@ -298,6 +307,73 @@ def check_package_urls(packages): pkg.url_status = pkg.url_worker.get(timeout=3600) +def release_monitoring_get_latest_version_by_distro(pool, name): + try: + req = pool.request('GET', "/api/project/Buildroot/%s" % name) + except HTTPError: + return (RM_API_STATUS_ERROR, None, None) + + if req.status != 200: + return (RM_API_STATUS_NOT_FOUND, None, None) + + data = json.loads(req.data) + + if 'version' in data: + return (RM_API_STATUS_FOUND_BY_DISTRO, data['version'], data['id']) + else: + return (RM_API_STATUS_FOUND_BY_DISTRO, None, data['id']) + + +def release_monitoring_get_latest_version_by_guess(pool, name): + try: + req = pool.request('GET', "/api/projects/?pattern=%s" % name) + except HTTPError: + return (RM_API_STATUS_ERROR, None, None) + + if req.status != 200: + return (RM_API_STATUS_NOT_FOUND, None, None) + + data = json.loads(req.data) + + projects = data['projects'] + projects.sort(key=lambda x: x['id']) + + for p in projects: + if p['name'] == name and 'version' in p: + return (RM_API_STATUS_FOUND_BY_PATTERN, p['version'], p['id']) + + return (RM_API_STATUS_NOT_FOUND, None, None) + + +def check_package_latest_version(packages): + """ + Fills in the .latest_version field of all Package objects + + This field has a special format: + (status, version, id) + with: + - status: one of RM_API_STATUS_ERROR, + RM_API_STATUS_FOUND_BY_DISTRO, RM_API_STATUS_FOUND_BY_PATTERN, + RM_API_STATUS_NOT_FOUND + - version: string containing the latest version known by + release-monitoring.org for this package + - id: string containing the id of the project corresponding to this + package, as known by release-monitoring.org + """ + pool = HTTPSConnectionPool('release-monitoring.org', port=443, + cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(), + timeout=30) + count = 0 + for pkg in packages: + v = release_monitoring_get_latest_version_by_distro(pool, pkg.name) + if v[0] == RM_API_STATUS_NOT_FOUND: + v = release_monitoring_get_latest_version_by_guess(pool, pkg.name) + + pkg.latest_version = v + print("[%d/%d] Package %s" % (count, len(packages), pkg.name)) + count += 1 + + def calculate_stats(packages): stats = defaultdict(int) for pkg in packages: @@ -322,6 +398,16 @@ def calculate_stats(packages): stats["hash"] += 1 else: stats["no-hash"] += 1 + if pkg.latest_version[0] == RM_API_STATUS_FOUND_BY_DISTRO: + stats["rmo-mapping"] += 1 + else: + stats["rmo-no-mapping"] += 1 + if not pkg.latest_version[1]: + stats["version-unknown"] += 1 + elif pkg.latest_version[1] == pkg.current_version: + stats["version-uptodate"] += 1 + else: + stats["version-not-uptodate"] += 1 stats["patches"] += pkg.patch_count return stats @@ -354,6 +440,7 @@ td.somepatches { td.lotsofpatches { background: #ff9a69; } + td.good_url { background: #d2ffc4; } @@ -363,6 +450,20 @@ td.missing_url { td.invalid_url { background: #ff9a69; } + +td.version-good { + background: #d2ffc4; +} +td.version-needs-update { + background: #ff9a69; +} +td.version-unknown { + background: #ffd870; +} +td.version-error { + background: #ccc; +} +