-
Notifications
You must be signed in to change notification settings - Fork 5
/
wheelfile.py
1199 lines (959 loc) · 43.3 KB
/
wheelfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# We do not target python2.
# Which python3 versions should we target? 3.6+ seems like a good idea.
import csv
import io
import hashlib
from string import ascii_letters, digits
from pathlib import Path
from collections import namedtuple
from inspect import signature
from packaging.tags import parse_tag
from packaging.utils import canonicalize_name
from packaging.version import Version, InvalidVersion
from email.message import EmailMessage
from email import message_from_string
from zipfile import ZipFile, ZipInfo
from typing import Optional, Union, List, Dict, IO, BinaryIO
__version__ = '0.0.1'
# TODO: change AssertionErrors to custom exceptions?
# TODO: idea - install wheel - w/ INSTALLER file
# TODO: idea - wheel from an installed distribution?
# TODO: module docstring
# TODO: fix inconsistent referencing style of symbols in docstrings
# TODO: parameters for path-like values should accept bytes
# TODO: idea - wheeldata -> wheelinfo, but it contradicts the idea below
# TODO: idea - might be better to provide WheelInfo objects via a getinfo(),
# which would inherit from ZipInfo but also cointain the hash from the RECORD.
# It would simplify the whole implementation.
# TODO: fix usage of UnnamedDistributionError and ValueError - it is ambiguous
def _slots_from_params(func):
"""List out slot names based on the names of parameters of func
Usage: __slots__ = _slots_from_signature(__init__)
"""
funcsig = signature(func)
slots = list(funcsig.parameters)
slots.remove('self')
return slots
# TODO: accept packaging.requirements.Requirement in requires_dist, fix this in
# example, ensure such objects are converted on __str__
# TODO: reimplement using dataclasses
# TODO: add version to the class name, reword the "Note"
# name regex for validation: ^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$
# TODO: helper-function or consts for description_content_type
# TODO: parse version using packaging.version.parse?
# TODO: values validation
# TODO: validate provides_extras ↔ requires_dists?
# TODO: validate values charset-wise
# TODO: as_json?
# TODO: as_dict?
# TODO: ensure name is the same as wheelfile namepath
# TODO: PEP-643 - v2.2
class MetaData:
"""Implements Wheel Metadata format v2.1.
Descriptions of parameters based on
https://packaging.python.org/specifications/core-metadata/. All parameters
are keyword only. Attributes of objects of this class follow parameter
names.
All parameters except "name" and "version" are optional.
Note
----
Metadata-Version, the metadata format version specifier, is unchangable.
Version "2.1" is used.
Parameters
----------
name
Primary identifier for the distribution that uses this metadata. Must
start and end with a letter or number, and consists only of ASCII
alphanumerics, hyphen, underscore, and period.
version
A string that contains PEP-440 compatible version identifier.
Can be specified using packaging.version.Version object, or a string,
where the latter is always converted to the former.
summary
A one-line sentence describing this distribution.
description
Longer text that describes this distribution in detail. Can be written
using plaintext, reStructuredText, or Markdown (see
"description_content_type" parameter below).
The string given for this field should not include RFC 822 indentation
followed by a "|" symbol. Newline characters are permitted
description_content_type
Defines content format of the text put in the "description" argument.
The field value should follow the following structure:
<type/subtype>; charset=<charset>[; <param_name>=<param value> ...]
Valid type/subtype strings are:
- text/plain
- text/x-rst
- text/markdown
For charset parameter, the only legal value is UTF-8.
For text/markdown, parameter "variant=<variant>" specifies variant of
the markdown used. Currently recognized variants include "GFM" and
"CommonMark".
Examples:
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Description-Content-Type: text/markdown
keywords
List of search keywords for this distribution. Optionally a single
string literal with keywords separated by commas.
Note: despite the name being a plural noun, the specification defines
this field as a single-use field. In this implementation however, the
value of the attribute after instance initialization is a list of
strings, and conversions to and from string follow the spec - they
require a comma-separated list.
classifiers
List PEP-301 classification values for this distribution, optionally
followed by a semicolon and an environmental marker.
Example of a classifier:
Operating System :: Microsoft :: Windows :: Windows 10
author
Name and, optionally, contact information of the original author of the
distribution.
author_email
Email address of the person specified in the "author" parameter. Format
of this field must follow the format of RFC-822 "From:" header field.
maintainer
Name and, optionally, contact information of person currently
maintaining the project to which this distribution belongs to.
Omit this parameter if the author and current maintainer is the same
person.
maintainer_email
Email address of the person specified in the "maintainer" parameter.
Format of this field must follow the format of RFC-822 "From:" header
field.
Omit this parameter if the author and current maintainer is the same
person.
license
Text of the license that covers this distribution. If license
classifier is used, this parameter may be omitted or used to specify the
particular version of the intended legal text.
home_page
URL of the home page for this distribution (project).
download_url
URL from which this distribution (in this version) can be downloaded.
project_urls
List of URLs with labels for them, in the following format:
<label>, <url>
The label must be at most 32 characters.
Example of an item of this list:
Repository, https://github.com/MrMino/wheelfile
platforms
List of strings that signify supported operating systems. Use only if
an OS cannot be listed by using a classifier.
supported_platforms
In binary distributions list of strings, each defining an operating
system and a CPU for which the distribution was compiled.
Semantics of this field aren't formalized by metadata specifications.
requires_python
PEP-440 version identifier, that specifies the set Python language
versions that this distribution is compatible with.
Some package management tools (most notably pip) use the value of this
field to filter out installation candidates.
Example:
~=3.5,!=3.5.1,!=3.5.0
requires_dists
List of PEP-508 dependency specifiers (think line-split contents of
requirements.txt).
requires_externals
List of system dependencies that this distribution requires.
Each item is a string with a name of the dependency optionally followed
by a version (in the same way items in "requires_dists") are specified.
Each item may end with a semicolon followed by a PEP-496 environment
markers.
provides_extras
List of names of optional features provided by a distribution. Used to
specify which dependencies should be installed depending on which of
these optional features are requested.
For example, if you specified "network" and "ssh" as optional features,
the following requirement specifier can be used in "requires_externals"
list to indicate, that the "paramiko" dependency should only be
installed when "ssh" feature is requested:
paramiko; extra == "ssh"
or
paramiko[ssh]
If a dependency is required by multiple features, the features can be
specified in a square brackets, separated by commas:
ipython[repl, jupyter_kernel]
Specifying an optional feature without using it in "requires_externals"
is considered invalid.
Feature names "tests" and "doc" are reserved in their semantics. They
can be used for dependencies of automated testing or documentation
generation.
provides_dists
List of names of other distributions contained within this one. Each
entry must follow the same format that entries in "requires_dists" list
do.
Different distributions may use a name that does not correspond to any
particular project, to indicate a capability to provide a certain
feature, e.g. "relational_db" may be used to say that a project
provides relational database capabilities
obsoletes_dists
List of names of distributions obsoleted by installing this one,
indicating that they should not coexist in a single environment with
this one. Each entry must follow the same format that entries in
"requires_dists" list do.
"""
def __init__(self, *, name: str, version: Union[str, Version],
summary: Optional[str] = None,
description: Optional[str] = None,
description_content_type: Optional[str] = None,
keywords: Union[List[str], str, None] = None,
classifiers: Optional[List[str]] = None,
author: Optional[str] = None,
author_email: Optional[str] = None,
maintainer: Optional[str] = None,
maintainer_email: Optional[str] = None,
license: Optional[str] = None,
home_page: Optional[str] = None,
download_url: Optional[str] = None,
project_urls: Optional[List[str]] = None,
platforms: Optional[List[str]] = None,
supported_platforms: Optional[List[str]] = None,
requires_python: Optional[str] = None,
requires_dists: Optional[List[str]] = None,
requires_externals: Optional[List[str]] = None,
provides_extras: Optional[List[str]] = None,
provides_dists: Optional[List[str]] = None,
obsoletes_dists: Optional[List[str]] = None
):
# self.metadata_version = '2.1' by property
self.name = name
self.version = Version(version) if isinstance(version, str) else version
self.summary = summary
self.description = description
self.description_content_type = description_content_type
self.keywords = keywords or []
self.classifiers = classifiers or []
self.author = author
self.author_email = author_email
self.maintainer = maintainer
self.maintainer_email = maintainer_email
self.license = license
self.home_page = home_page
self.download_url = download_url
self.project_urls = project_urls or []
self.platforms = platforms or []
self.supported_platforms = supported_platforms or []
self.requires_python = requires_python
self.requires_dists = requires_dists or []
self.requires_externals = requires_externals or []
self.provides_extras = provides_extras or []
self.provides_dists = provides_dists or []
self.obsoletes_dists = obsoletes_dists or []
__slots__ = _slots_from_params(__init__)
@property
def metadata_version(self):
return self._metadata_version
_metadata_version = '2.1'
@classmethod
def field_is_multiple_use(cls, field_name: str) -> bool:
field_name = field_name.lower().replace('-', '_').rstrip('s')
if field_name in cls.__slots__ or field_name == 'keyword':
return False
if field_name + 's' in cls.__slots__:
return True
else:
raise ValueError(f"Unknown field: {field_name}")
@classmethod
def _field_name(cls, attribute_name: str) -> str:
if cls.field_is_multiple_use(attribute_name):
attribute_name = attribute_name[:-1]
field_name = attribute_name.title()
field_name = field_name.replace('_', '-')
field_name = field_name.replace('Url', 'URL')
field_name = field_name.replace('-Page', '-page')
field_name = field_name.replace('-Email', '-email')
return field_name
@classmethod
def _attr_name(cls, field_name: str) -> str:
if cls.field_is_multiple_use(field_name):
field_name += 's'
return field_name.lower().replace('-', '_')
def __str__(self) -> str:
m = EmailMessage()
m.add_header("Metadata-Version", self.metadata_version)
for attr_name in self.__slots__:
content = getattr(self, attr_name)
if not content:
continue
field_name = self._field_name(attr_name)
if field_name == 'Keywords':
content = ','.join(content)
elif field_name == "Version":
content = str(content)
if self.field_is_multiple_use(field_name):
assert not isinstance(content, str), (
f"Single string in multiple use attribute: {attr_name}"
)
for value in content:
m.add_header(field_name, value)
elif field_name == 'Description':
m.set_payload(content)
else:
assert isinstance(content, str), (
f"Expected string, got {type(content)} instead: {attr_name}"
)
m.add_header(field_name, content)
return str(m)
def __eq__(self, other):
if isinstance(other, MetaData):
return all(getattr(self, f) == getattr(other, f)
for f in self.__slots__)
else:
return NotImplemented
@classmethod
def from_str(cls, s: str) -> 'MetaData':
m = message_from_string(s)
assert m['Metadata-Version'] == cls._metadata_version
del m['Metadata-Version']
args = {}
for field_name in m.keys():
attr = cls._attr_name(field_name)
if not attr.endswith('s'):
args[attr] = m.get(field_name)
else:
args[attr] = m.get_all(field_name)
args['description'] = m.get_payload()
return cls(**args)
# TODO: reimplement using dataclasses?
# TODO: add version to the class name, reword the "Note"
# TODO: values validation
# TODO: to_json?
# TODO: as_dict?
class WheelData:
"""Implements .dist-info/WHEEL file format.
Descriptions of parameters based on PEP-427. All parameters are keyword
only. Attributes of objects of this class follow parameter names.
Note
----
Wheel-Version, the wheel format version specifier, is unchangeable. Version
"1.0" is used.
Parameters
----------
generator
Name and (optionally) version of the generator that generated the wheel
file. By default, "wheelfile {__version__}" is used.
root_is_purelib
Defines whether the root of the wheel file should be first unpacked into
purelib directory (see distutils.command.install.INSTALL_SCHEMES).
tags
See PEP-425 - "Compatibility Tags for Built Distributions". Either a
single string denoting one tag or a list of tags. Tags may contain
compressed tag sets, in which case they will be expanded.
By default, "py2.py3-none-any" (the universal tag) is used.
build
Optional build number. Used as a tie breaker when two wheels have the
same version.
"""
def __init__(self, *,
generator: str = 'wheelfile ' + __version__,
root_is_purelib: bool = True,
tags: Union[List[str], str] = 'py2.py3-none-any',
build: Optional[int] = None):
# self.wheel_version = '1.0' by property
self.generator = generator
self.root_is_purelib = root_is_purelib
self.tags = self._extend_tags(
tags if isinstance(tags, list) else [tags]
)
self.build = build
__slots__ = _slots_from_params(__init__)
@property
def wheel_version(self) -> str:
return '1.0'
def _extend_tags(self, tags: List[str]) -> List[str]:
extended_tags = []
for tag in tags:
extended_tags.extend([str(t) for t in parse_tag(tag)])
return extended_tags
def __str__(self) -> str:
# TODO Custom exception? Exception message?
assert isinstance(self.generator, str), (
f"'generator' must be a string, got {type(self.generator)} instead"
)
assert isinstance(self.root_is_purelib, bool), (
f"'root_is_purelib' must be a boolean, got"
f"{type(self.root_is_purelib)} instead"
)
assert isinstance(self.tags, list), (
f"Expected a list in 'tags', got {type(self.tags)} instead"
)
assert self.tags, "'tags' cannot be empty"
assert isinstance(self.build, int) or self.build is None, (
f"'build' must be an int, got {type(self.build)} instead"
)
m = EmailMessage()
m.add_header("Wheel-Version", self.wheel_version)
m.add_header("Generator", self.generator)
m.add_header("Root-Is-Purelib", "true"
if self.root_is_purelib else "false")
for tag in self.tags:
m.add_header("Tag", tag)
if self.build is not None:
m.add_header("Build", str(self.build))
return str(m)
@classmethod
def from_str(cls, s: str) -> 'WheelData':
m = message_from_string(s)
assert m['Wheel-Version'] == '1.0'
args = {
'generator': m.get('Generator'),
'root_is_purelib': bool(m.get('Root-Is-Purelib')),
'tags': m.get_all('Tag'),
}
if 'build' in m:
args['build'] = int(m.get('build'))
return cls(**args)
def __eq__(self, other):
if isinstance(other, WheelData):
return all(getattr(self, f) == getattr(other, f)
for f in self.__slots__)
else:
return NotImplemented
# TODO: leave out hashes of *.pyc files?
class WheelRecord:
"""Contains logic for creation and modification of RECORD files.
Keeps track of files in the wheel and their hashes.
For the full spec, see PEP-376 "RECORD" section, PEP-627,
"The .dist-info directory" section of PEP-427, and
https://packaging.python.org/specifications/recording-installed-packages/.
"""
HASH_ALGO = hashlib.sha256
HASH_BUF_SIZE = 65536
_RecordEntry = namedtuple('Record', 'path hash size')
def __init__(self):
self._records: Dict[str, self._RecordEntry] = {}
def hash_of(self, arcpath) -> str:
"""Return the hash of a file in the archive this RECORD describes
Parameters
----------
arcpath
Location of the file inside the archive.
Returns
-------
str
String in the form <algorithm>=<hexstr>, where algorithm is the
name of the hashing agorithm used to generate the hash (see
HASH_ALGO), and hexstr is a string containing a hexified version of
the hash.
"""
return self._records[arcpath].hash
def __str__(self) -> str:
buf = io.StringIO()
records = csv.DictWriter(buf, fieldnames=self._RecordEntry._fields)
for entry in self._records.values():
records.writerow(entry._asdict())
return buf.getvalue()
@classmethod
def from_str(self, s) -> 'WheelRecord':
record = WheelRecord()
buf = io.StringIO(s)
reader = csv.DictReader(buf, self._RecordEntry._fields)
for row in reader:
entry = self._RecordEntry(**row)
record._records[entry.path] = entry
return record
def update(self, arcpath: str, buf: IO[bytes]):
"""Add a record entry for a file in the archive.
Parameters
----------
buf
Buffer from which the data will be read in HASH_BUF_SIZE chunks.
Must be fresh, i.e. seek(0)-ed.
"""
assert buf.tell() == 0, (
f"Stale buffer given - current position: {buf.tell()}."
)
assert not arcpath.endswith('.dist-info/RECORD'), (
f"Attempt to add an entry for a RECORD file to the RECORD: "
f"{arcpath}."
)
self._records[arcpath] = self._entry(arcpath, buf)
def remove(self, arcpath: str):
del self._records[arcpath]
@classmethod
def _entry(cls, arcpath: str, buf: IO[bytes]) -> _RecordEntry:
size = 0
hasher = cls.HASH_ALGO()
while True:
data = buf.read(cls.HASH_BUF_SIZE)
size += len(data)
if not data:
break
hasher.update(data)
hash_hex = hasher.name + '=' + hasher.hexdigest()
return cls._RecordEntry(arcpath, hash_hex, size)
def __eq__(self, other):
if isinstance(other, WheelRecord):
return str(self) == str(other)
else:
return NotImplemented
def __contains__(self, path):
return path in self._records
class BadWheelFileError(ValueError):
"""The given file cannot be interpreted as a wheel nor fixed."""
class UnnamedDistributionError(BadWheelFileError):
"""Distribution name cannot be deduced from arguments."""
# TODO: prevent arbitrary writes to METADATA, WHEEL, and RECORD - or make sure
# the writes are reflected internally
# TODO: prevent adding .dist-info directories if there's one already there
# TODO: add attributes docstrings: distribution, tags, etc
# TODO: ensure distname and varsion have no weird characters (!slashes!)
# TODO: properties for build tag, python tag, abi tag, platform tag?
# TODO: debug propery, as with ZipFile.debug
# TODO: comment property
# TODO: compression level arguments - is compression even supported by the spec?
# TODO: append mode
# TODO: writing inexistent metadata in lazy mode
# TODO: better repr
# TODO: docstrings
# TODO: properties for rest of the naming convention parts
class WheelFile:
"""An archive that follows the wheel specification.
Used to read, create, validate, or modify *.whl files.
Attributes
----------
filename : str
Path to the file, if the instance was initialized with one, otherwise
None.
distname : str
Name of the distribution (project). Either given to __init__()
explicitly or inferred from its file_or_path argument.
version : packaging.version.Version
Version of the distribution. Either given to __init__() explicitly or
inferred from its file_or_path argument.
record : Optional[WheelRecord]
Current state of .dist-info/RECORD file.
When modifying other files in the archive, the record is written on
each operation & on close().
In lazy mode, this is only the case when using refresh_record() and
write_record(), and if the file does not exist or is misformatted, this
attribute becomes None. In such cases, calling refresh_record() creates
it from scratch().
In non-lazy modes, this file is always created/read & validated on
initialization.
metadata : Optional[MetaData]
Current state of .dist-info/METADATA file.
Follows the same semantics as 'record'. Values from 'distname' and
'version' are used to provide required arguments when the file is
created from scratch by __init__().
In standard modes, when changing data contained by the object this
property returns, the file is being written only on close(). In order
to write it beforehand, use write_metadata().
When using lazy mode, the data is not being written to the archive on
close().
wheeldata : Optional[WheelData]
Current state of .dist-info/WHEELDATA file.
Follows the same semantics as 'metadata'. Consult docstring of
WheelData class for the default values used.
Use write_wheeldata() after editting it, in order to write it before
close(), or - in lazy mode - to write the archive data at all.
"""
VALID_DISTNAME_CHARS = set(ascii_letters + digits + '._')
# TODO: implement lazy mode
# TODO: in lazy mode, log reading/missing metadata errors
# TODO: expand compatibility tags, put them into wheeldata
# TODO: warn on 'w' modes if filename does not end with .whl
# TODO: arguments for build, tags, pyver, and abi, with sensible defaults
# TODO: The defaults for tags should be the same as the ones in WheelData
def __init__(
self,
file_or_path: Union[str, Path, BinaryIO] = './',
mode: str = 'r',
*,
distname: Optional[str] = None,
version: Optional[Union[str, Version]] = None,
) -> None:
"""Open or create a wheel file.
If lazy mode is not specified:
- In read and append modes, the file is validated using validate().
Contents of metadata files inside .dist-info directory are read and
converted into their respective object representations (see
"metadata", "wheeldata", and "record" attributes).
- In write and exclusive write modes, object representations for
each metadata file are created from scratch. They will be written
to each of their respective .dist-info/ files on close().
To skip the validation, e.g. if you wish to fix a misformated wheel,
use lazy mode ('l' - see description of the "mode" parameter).
In lazy mode, if the opened file does not contain WHEEL, METADATA, or
RECORD (which is optional as per PEP-627), the attributes corresponding
to the missing data structures will be set to None. In order to create
them, either set these attributes yourself and call their respective
write methods, or use fix().
If any of the metadata files cannot be read due to a wrong format, they
are considered missing.
If the archive root contains a directory with a name ending with
'.dist-info', it is considered to be _the_ metadata directory for the
wheel, even if the given/inferred distname and version do not match its
name.
If the archive already contains either one of the aforementioned files,
they are read, but are not checked for consistency. Use validate() to
check whether there are errors, and fix() to fix them.
There are currently 2 classes of errors which completely prevent a well
formatted zip file from being read by this class:
- Unknown/incorrect distribution name/version - when the naming
scheme is violated in a way that prevents inferring these values
and the user hasn't provided these values, or provided ones that
do not conform to the specifications. In such case, the scope of
functioning features of this class would be limited to that of a
standard ZipFile, and is therefore unsupported.
- When there are multiple .data or .dist-info directories. This
would mean that the class would have to guess which are the genuine
ones - and we refuse the temptation to do that (see "The Zen of
Python").
In other words, this class is liberal in what it accepts, but very
conservative in what it does (A.K.A. the robustness principle).
Note
----
Despite all of this, THERE ARE NO GUARANTEES being made as to whether a
misformatted file can be read or fixed by this class, and even if it is
currently, whether it will still be the case in the future versions.
Parameters
----------
file_or_path
Path to the file to open/create or a file-like object to use.
mode
See zipfile.ZipFile docs for the list of available modes.
In the read and append modes, the file given has to contain proper
PKZIP-formatted data.
Adding "l" to the mode string turns on the "lazy mode". This
changes the behavior on initialization (see above), the behavior of
close() (see its docstring for more info), makes the archive
modifying methods refrain from refreshing the record & writing it
to the archive.
Lazy mode should only be used in cases where a misformatted wheels
have to be read or fixed.
distname
Name of the distribution for this wheelfile.
If omitted, the name will be inferred from the filename given in
the path. If a file-like object is given instead of a path, it will
be inferred from its "name" attribute.
The class requires this information, as it's used to infer the name
of the directory in the archive in which metadata should reside.
This argument should be understood as an override for the values
calculated from the object given in "file_or_path" argument. It
should only be necessary when a file is read from memory or has a
misformatted name.
Should be composed of alphanumeric characters and underscores only.
Must not be an empty string.
See the description of "distname" attribute for more information.
version
Version of the distribution in this wheelfile. Follows the same
semantics as "distname".
The given value must be compliant with PEP-440 version identifier
specification.
See the description of "version" attribute for more information.
Raises
------
UnnamedDistributionError
Raised if the distname or version cannot be inferred from the
given arguments.
E.g. when the path does not contain the version, or the
file-like object has no "name" attribute to get the filename from,
and the information wasn't provided via other arguments.
BadWheelFileError
Raised if the archive contains multiple '.dist-info' or '.data'
directories.
zipfile.BadZipFile
If given file is not a proper zip.
"""
assert not isinstance(file_or_path, io.TextIOBase), (
"Text buffer given where a binary one was expected."
)
if 'a' in mode:
# Requires rewrite feature
raise NotImplementedError(
"Append mode is not supported yet"
)
if 'r' in mode:
raise NotImplementedError(
"Nontruncating modes are not supported yet"
)
if 'l' in mode:
raise NotImplementedError(
"Lazy modes are not supported yet"
)
# These might be none in case a corrupted wheel is read in lazy mode
self.wheeldata: Optional[WheelData] = None
self.metadata: Optional[MetaData] = None
self.record: Optional[WheelRecord] = None
if isinstance(file_or_path, str):
file_or_path = Path(file_or_path)
# TODO: come up with argument names for these
# FIXME: this should not be hardcoded
build = None
langver = 'py3'
abi = 'none'
platform = 'any'
# TODO: mention this functionality in the docstring
if isinstance(file_or_path, Path):
if file_or_path.is_dir():
filename = self._pick_a_filename(
distname, version, build, langver, abi, platform
)
file_or_path /= filename
# TODO: This should happen only after initialization of metas is done
self._pick_a_distname(file_or_path, given_distname=distname)
self._pick_a_version(file_or_path, given_version=version)
self._pick_tags(build, langver, abi, platform)
self._zip = ZipFile(file_or_path, mode)
if 'w' in mode or 'x' in mode:
self._initialize_dist_info()
else:
self._read_dist_info()
@staticmethod
def _pick_a_filename(
distname, version, build, langver, abi, platform
) -> Path:
# TODO: Walrus a witness up and put it into exception message
if any(value is None
for param, value in locals().items()
if param != 'build'):
raise ValueError(
"Missing arguments: if no path is specified, or given path is "
"a directory, all arguments corresponding to the name scheme "
"must be given."
)
if build is not None:
return Path(
f'{distname}-{version}-{build}-{langver}-{abi}-{platform}.whl'
)
else:
return Path(f'{distname}-{version}-{langver}-{abi}-{platform}.whl')
def _pick_a_distname(self, file_or_path: Union[Path, BinaryIO],
given_distname: Union[None, str]):
if given_distname is not None:
distname = given_distname
else:
filename = getattr(file_or_path, 'name', None)
if filename is None:
raise UnnamedDistributionError(
"No distname provided and an unnamed object given."
)
if not isinstance(file_or_path, Path):
filename = Path(filename).name
distname = filename.split('-')[0]
if distname == '':
raise UnnamedDistributionError(
f"No distname provided and the inferred filename does not "
f"contain a proper distname substring: {filename}"
)
if distname == '':
raise ValueError("Empty string given as a distname.")
distname_valid = set(distname) <= self.VALID_DISTNAME_CHARS
if not distname_valid:
raise ValueError(
f"Invalid distname: {repr(distname)}. Distnames should "
f"contain only ASCII letters, numbers, underscores, and "
f"periods."
)
self._distname = distname
def _pick_a_version(self, file_or_path: Union[str, Path, BinaryIO],
given_version: Union[None, str, Version]):
if isinstance(given_version, Version):
# We've got a valid object here, nothing else to do
self._version = given_version
return
if isinstance(given_version, str):
version = given_version
else:
filename = getattr(file_or_path, 'name', None)
if filename is None:
raise UnnamedDistributionError(
"No version provided and an unnamed object given."
)
# Ensure we're getting a filename, not a path
filename = Path(filename).name
name_segments = filename.split('-')
if len(name_segments) < 2 or name_segments[1] == '':
raise UnnamedDistributionError(
f"No version provided and the inferred filename does not "
f"contain a version segment: {filename}"
)
version = name_segments[1]
try:
self._version = Version(version)
except InvalidVersion as e:
raise ValueError(f"Invalid version: {repr(version)}.") from e
# TODO: infer from filename or given args instead of hardcoded value
# TODO: properties for these?
def _pick_tags(self,
given_build: Optional[str],
given_python: str,
given_abi: str,
given_platform: str):
self._build_tag = given_build
self._python_tag = given_python
self._abi_tag = given_abi
self._platform_tag = given_platform
# TODO: initialize tags
def _initialize_dist_info(self):
self.metadata = MetaData(name=self.distname, version=self.version)
self.wheeldata = WheelData()
self.record = WheelRecord()
# FIXME: don't hardcode this
self.wheeldata.tags = ['py3-none-any']