From cb82de3e14cdbdf4b41df7aa077f2977bd368f7f Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Thu, 30 May 2024 23:21:47 +0900 Subject: [PATCH] added package manifest id check --- repogen/downloadipk.py | 7 +++---- repogen/lintpkg.py | 15 ++++++++++++--- repogen/pkg_info.py | 2 +- repogen/pkg_manifest.py | 3 +++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/repogen/downloadipk.py b/repogen/downloadipk.py index bd3a526..f6d3281 100644 --- a/repogen/downloadipk.py +++ b/repogen/downloadipk.py @@ -20,6 +20,9 @@ except (requests.exceptions.JSONDecodeError, json.decoder.JSONDecodeError) as e: print(f'Could not parse manifest: {e}') exit(2) + except requests.RequestException as e: + print(f'Could not download package info: {e}') + exit(5) except IOError as e: print(f'Could not open package info file: {e.strerror}') exit(3) @@ -27,10 +30,6 @@ print(f'Could not parse package info: {e.message}') exit(4) - if pkginfo is None: - print(f'Failed to get manifest for unknown reason') - exit(5) - try: ipk_url = pkginfo['manifest']['ipkUrl'] except KeyError as e: diff --git a/repogen/lintpkg.py b/repogen/lintpkg.py index cfe09a1..63d10e9 100644 --- a/repogen/lintpkg.py +++ b/repogen/lintpkg.py @@ -1,3 +1,4 @@ +import sys from pathlib import Path from typing import Tuple, List from urllib.parse import urlparse @@ -40,6 +41,9 @@ def lint(self, info: PackageInfo) -> Tuple[List[str], List[str]]: if info['pool'] not in ['main', 'non-free']: errors.append('pool property must be `main` or `non-free`') + if info['id'] != info['manifest']['id']: + errors.append('id in manifest must match id in info') + # Process icon icon_uri = urlparse(info['iconUri']) if icon_uri.scheme == 'data' or icon_uri.scheme == 'https': @@ -89,9 +93,14 @@ def _validate_manifest_url(url: str, key: str, e: [str]): parser.add_argument('-f', '--file', required=True) args = parser.parse_args() - lint_pkginfo = pkg_info.from_package_info_file(Path(args.file)) - if lint_pkginfo is None: - raise ValueError('No package info') + try: + lint_pkginfo = pkg_info.from_package_info_file(Path(args.file)) + except requests.exceptions.RequestException as e: + print(f'Could not download package info: {e}', file=sys.stderr) + exit(5) + except IOError as e: + print(f'Could not open package info file: {e.strerror}', file=sys.stderr) + exit(3) linter = PackageInfoLinter() lint_errors, lint_warnings = linter.lint(lint_pkginfo) diff --git a/repogen/pkg_info.py b/repogen/pkg_info.py index fc0f353..1e5afae 100644 --- a/repogen/pkg_info.py +++ b/repogen/pkg_info.py @@ -46,7 +46,7 @@ def load_registry(info_path: Path) -> tuple[str, PackageRegistry]: return pkgid, content -def from_package_info_file(info_path: Path, offline=False) -> PackageInfo | None: +def from_package_info_file(info_path: Path, offline=False) -> PackageInfo: pkgid, content = load_registry(info_path) return from_package_info(pkgid, content, offline) diff --git a/repogen/pkg_manifest.py b/repogen/pkg_manifest.py index 475d435..94c9975 100644 --- a/repogen/pkg_manifest.py +++ b/repogen/pkg_manifest.py @@ -3,6 +3,7 @@ import urllib.parse from datetime import datetime from email.utils import parsedate_to_datetime +from json import JSONDecodeError from typing import TypedDict, NotRequired, Literal from urllib.parse import urljoin from urllib.request import url2pathname @@ -51,6 +52,8 @@ def obtain_manifest(pkgid: str, channel: str, uri: str, offline: bool = False) - raise requests.exceptions.ConnectionError('Offline') uri = url_fixup(uri) resp = requests.get(url=uri, allow_redirects=True) + if resp.status_code != 200: + raise requests.exceptions.HTTPError(f'Failed to fetch manifest: HTTP {resp.status_code}') manifest = resp.json() manifest['ipkUrl'] = urljoin(uri, manifest['ipkUrl']) manifest['ipkSize'] = url_size(manifest['ipkUrl'])