1
1
import dataclasses
2
+ import hashlib
2
3
import logging
3
4
import re
4
5
import sys
@@ -213,6 +214,17 @@ def _exactly_one(iterable: Iterable[object]) -> bool:
213
214
return found
214
215
215
216
217
+ def _validate_hashes (hashes : Dict [str , Any ]) -> None :
218
+ if not hashes :
219
+ raise PylockValidationError ("At least one hash must be provided" )
220
+ if not any (algo in hashlib .algorithms_guaranteed for algo in hashes ):
221
+ raise PylockValidationError (
222
+ "At least one hash algorithm must be in haslib.algorithms_guaranteed"
223
+ )
224
+ if not all (isinstance (hash , str ) for hash in hashes .values ()):
225
+ raise PylockValidationError ("Hash values must be strings" )
226
+
227
+
216
228
class PylockValidationError (Exception ):
217
229
pass
218
230
@@ -236,7 +248,6 @@ class PackageVcs:
236
248
subdirectory : Optional [str ]
237
249
238
250
def __post_init__ (self ) -> None :
239
- # TODO validate supported vcs type
240
251
if not self .path and not self .url :
241
252
raise PylockValidationError ("No path nor url set for vcs package" )
242
253
@@ -279,6 +290,7 @@ class PackageArchive:
279
290
def __post_init__ (self ) -> None :
280
291
if not self .path and not self .url :
281
292
raise PylockValidationError ("No path nor url set for archive package" )
293
+ _validate_hashes (self .hashes )
282
294
283
295
@classmethod
284
296
def from_dict (cls , d : Dict [str , Any ]) -> "Self" :
@@ -304,6 +316,7 @@ class PackageSdist:
304
316
def __post_init__ (self ) -> None :
305
317
if not self .path and not self .url :
306
318
raise PylockValidationError ("No path nor url set for sdist package" )
319
+ _validate_hashes (self .hashes )
307
320
308
321
@classmethod
309
322
def from_dict (cls , d : Dict [str , Any ]) -> "Self" :
@@ -329,6 +342,7 @@ class PackageWheel:
329
342
def __post_init__ (self ) -> None :
330
343
if not self .path and not self .url :
331
344
raise PylockValidationError ("No path nor url set for wheel package" )
345
+ _validate_hashes (self .hashes )
332
346
333
347
@classmethod
334
348
def from_dict (cls , d : Dict [str , Any ]) -> "Self" :
0 commit comments