pythondistdeps.py: Detect and error when metadata is corrupted

This commit is contained in:
Tomas Orsava 2021-05-25 12:46:21 +02:00
parent 20f8b2c775
commit 27d363833e
4 changed files with 33 additions and 1 deletions

View File

@ -73,6 +73,14 @@ class Requirement(Requirement_):
class Distribution(PathDistribution):
def __init__(self, path):
super(Distribution, self).__init__(Path(path))
# Check that the initialization went well and metadata are not missing or corrupted
# name is the most important attribute, if it doesn't exist, import failed
if not self.name or not isinstance(self.name, str):
print("*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***")
print('Error: Python metadata at `{}` are missing or corrupted.'.format(path), file=stderr)
exit(65) # os.EX_DATAERR
self.normalized_name = normalize_name(self.name)
self.legacy_normalized_name = legacy_normalize_name(self.name)
self.requirements = [Requirement(r) for r in self.requires or []]

View File

@ -0,0 +1 @@
Corrupted dist-info metadata

View File

@ -1207,6 +1207,15 @@
python3.9dist(zope-testing)
--requires --normalized-names-format pep503 --require-extras-subpackages:
--provides --normalized-names-format pep503:
corrupted.dist-info:
stderr:
provides: |-
Error: Python metadata at `*/corrupted.dist-info` are missing or corrupted.
requires: |-
Error: Python metadata at `*/corrupted.dist-info` are missing or corrupted.
stdout:
provides: '*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***'
requires: '*** PYTHON_METADATA_FAILED_TO_PARSE_ERROR___SEE_STDERR ***'
pyreq2rpm.tests-2020.04.07.024dab0-py3.9.egg-info:
provides: python3.9dist(pyreq2rpm-tests) = 2020.04.07.024dab0
requires: |-

View File

@ -29,6 +29,7 @@
from pathlib import Path
from fnmatch import fnmatch
import pytest
import shlex
import shutil
@ -224,8 +225,21 @@ def fixture_check_and_install_test_data():
def test_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expected):
"""Runs pythondistdeps with the given parameters and dist-info/egg-info
path, compares the results with the expected results"""
expect_failure = "stderr" in expected
assert expected == run_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expect_failure)
tested = run_pythondistdeps(provides_params, requires_params, dist_egg_info_path, expect_failure)
if expect_failure:
for k1, k2 in ((k1, k2) for k1 in expected.keys() for k2 in expected[k1].keys()):
if k1 == "stderr":
# Some stderr messages contain full file paths. To get around
# this, asterisk is used in the test-data and we compare with
# fnmatch that understands Unix-style wildcards.
assert fnmatch(tested[k1][k2], expected[k1][k2])
else:
assert expected[k1][k2] == tested[k1][k2]
else:
assert expected == tested
if __name__ == "__main__":