18
18
import os
19
19
import re
20
20
import subprocess
21
- from typing import Any , Dict , List , NamedTuple , Optional , Sequence , Tuple , Union , cast
21
+ import warnings
22
+ from typing import (
23
+ Any ,
24
+ Dict ,
25
+ Iterable ,
26
+ List ,
27
+ NamedTuple ,
28
+ Optional ,
29
+ Sequence ,
30
+ Tuple ,
31
+ Union ,
32
+ cast ,
33
+ )
22
34
23
35
import importlib_metadata
24
36
import pkginfo
@@ -95,7 +107,8 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
95
107
for ext , dtype in DIST_EXTENSIONS .items ():
96
108
if filename .endswith (ext ):
97
109
try :
98
- meta = DIST_TYPES [dtype ](filename )
110
+ with warnings .catch_warnings (record = True ) as captured :
111
+ meta = DIST_TYPES [dtype ](filename )
99
112
except EOFError :
100
113
raise exceptions .InvalidDistribution (
101
114
"Invalid distribution file: '%s'" % os .path .basename (filename )
@@ -107,19 +120,25 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
107
120
"Unknown distribution format: '%s'" % os .path .basename (filename )
108
121
)
109
122
110
- # If pkginfo encounters a metadata version it doesn't support, it may give us
123
+ supported_metadata = list (pkginfo .distribution .HEADER_ATTRS )
124
+ if cls ._is_unknown_metadata_version (captured ):
125
+ raise exceptions .InvalidDistribution (
126
+ "Make sure the distribution is using a supported Metadata-Version: "
127
+ f"{ ', ' .join (supported_metadata )} ."
128
+ )
129
+ # If pkginfo <1.11 encounters a metadata version it doesn't support, it may give
111
130
# back empty metadata. At the very least, we should have a name and version,
112
131
# which could also be empty if, for example, a MANIFEST.in doesn't include
113
132
# setup.cfg.
114
133
missing_fields = [
115
134
f .capitalize () for f in ["name" , "version" ] if not getattr (meta , f )
116
135
]
117
136
if missing_fields :
118
- supported_metadata = list (pkginfo .distribution .HEADER_ATTRS )
119
137
raise exceptions .InvalidDistribution (
120
138
"Metadata is missing required fields: "
121
- f"{ ', ' .join (missing_fields )} .\n "
122
- "Make sure the distribution includes the files where those fields "
139
+ f"{ ', ' .join (missing_fields )} ."
140
+ # TODO: Remove this section after requiring pkginfo>=1.11
141
+ "\n Make sure the distribution includes the files where those fields "
123
142
"are specified, and is using a supported Metadata-Version: "
124
143
f"{ ', ' .join (supported_metadata )} ."
125
144
)
@@ -137,6 +156,13 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
137
156
138
157
return cls (filename , comment , meta , py_version , dtype )
139
158
159
+ @staticmethod
160
+ def _is_unknown_metadata_version (
161
+ captured : Iterable [warnings .WarningMessage ],
162
+ ) -> bool :
163
+ NMV = getattr (pkginfo .distribution , "NewMetadataVersion" , None )
164
+ return any (warning .category is NMV for warning in captured )
165
+
140
166
def metadata_dictionary (self ) -> Dict [str , MetadataValue ]:
141
167
"""Merge multiple sources of metadata into a single dictionary.
142
168
0 commit comments