6
6
from ..wheelfile import WheelFile
7
7
from .pack import read_tags , set_build_number
8
8
9
- try :
10
- from typing import Iterator
11
- except ImportError :
12
- pass
13
-
14
9
15
10
def compute_tags (original_tags : list [str ], new_tags : list [str ] | None ) -> list [str ]:
16
11
"""Add or replace tags."""
@@ -25,13 +20,13 @@ def compute_tags(original_tags: list[str], new_tags: list[str] | None) -> list[s
25
20
26
21
27
22
def tags (
28
- wheels : list [ str ] ,
23
+ wheel : str ,
29
24
python_tags : list [str ] | None = None ,
30
25
abi_tags : list [str ] | None = None ,
31
26
platform_tags : list [str ] | None = None ,
32
27
build_number : int | None = None ,
33
28
remove : bool = False ,
34
- ) -> Iterator [ str ] :
29
+ ) -> str :
35
30
"""Change the tags on a wheel file.
36
31
37
32
The tags are left unchanged if they are not specified. To specify "none",
@@ -44,93 +39,89 @@ def tags(
44
39
:param build_number: The build number to set.
45
40
:param remove: Remove the original wheel.
46
41
"""
47
-
48
- for wheel in wheels :
49
- with WheelFile (wheel , "r" ) as f :
50
- wheel_info = f .read (f .dist_info_path + "/WHEEL" )
51
-
52
- original_wheel_name = os .path .basename (f .filename )
53
- namever = f .parsed_filename .group ("namever" )
54
- build = f .parsed_filename .group ("build" )
55
- original_python_tags = f .parsed_filename .group ("pyver" ).split ("." )
56
- original_abi_tags = f .parsed_filename .group ("abi" ).split ("." )
57
- orignial_plat_tags = f .parsed_filename .group ("plat" ).split ("." )
58
-
59
- tags , existing_build_number = read_tags (wheel_info )
60
-
61
- impls = {tag .split ("-" )[0 ] for tag in tags }
62
- abivers = {tag .split ("-" )[1 ] for tag in tags }
63
- platforms = {tag .split ("-" )[2 ] for tag in tags }
64
-
65
- if impls != set (original_python_tags ):
66
- raise AssertionError (f"{ impls } != { original_python_tags } " )
67
-
68
- if abivers != set (original_abi_tags ):
69
- raise AssertionError (f"{ abivers } != { original_abi_tags } " )
70
-
71
- if platforms != set (orignial_plat_tags ):
72
- raise AssertionError (f"{ platforms } != { orignial_plat_tags } " )
73
-
74
- if existing_build_number != build :
75
- raise AssertionError (
76
- f"Incorrect filename '{ build } ' & "
77
- f"*.dist-info/WHEEL '{ existing_build_number } ' build numbers"
42
+ with WheelFile (wheel , "r" ) as f :
43
+ wheel_info = f .read (f .dist_info_path + "/WHEEL" )
44
+
45
+ original_wheel_name = os .path .basename (f .filename )
46
+ namever = f .parsed_filename .group ("namever" )
47
+ build = f .parsed_filename .group ("build" )
48
+ original_python_tags = f .parsed_filename .group ("pyver" ).split ("." )
49
+ original_abi_tags = f .parsed_filename .group ("abi" ).split ("." )
50
+ orignial_plat_tags = f .parsed_filename .group ("plat" ).split ("." )
51
+
52
+ tags , existing_build_number = read_tags (wheel_info )
53
+
54
+ impls = {tag .split ("-" )[0 ] for tag in tags }
55
+ abivers = {tag .split ("-" )[1 ] for tag in tags }
56
+ platforms = {tag .split ("-" )[2 ] for tag in tags }
57
+
58
+ if impls != set (original_python_tags ):
59
+ raise AssertionError (f"{ impls } != { original_python_tags } " )
60
+
61
+ if abivers != set (original_abi_tags ):
62
+ raise AssertionError (f"{ abivers } != { original_abi_tags } " )
63
+
64
+ if platforms != set (orignial_plat_tags ):
65
+ raise AssertionError (f"{ platforms } != { orignial_plat_tags } " )
66
+
67
+ if existing_build_number != build :
68
+ raise AssertionError (
69
+ f"Incorrect filename '{ build } ' & "
70
+ f"*.dist-info/WHEEL '{ existing_build_number } ' build numbers"
71
+ )
72
+
73
+ # Start changing as needed
74
+ if build_number is not None :
75
+ build = str (build_number )
76
+
77
+ final_python_tags = compute_tags (original_python_tags , python_tags )
78
+ final_abi_tags = compute_tags (original_abi_tags , abi_tags )
79
+ final_plat_tags = compute_tags (orignial_plat_tags , platform_tags )
80
+
81
+ final_tags = [
82
+ "." .join (sorted (final_python_tags )),
83
+ "." .join (sorted (final_abi_tags )),
84
+ "." .join (sorted (final_plat_tags )),
85
+ ]
86
+
87
+ if build :
88
+ final_tags .insert (0 , build )
89
+ final_tags .insert (0 , namever )
90
+
91
+ final_wheel_name = "-" .join (final_tags ) + ".whl"
92
+
93
+ if original_wheel_name != final_wheel_name :
94
+ tags = [
95
+ f"{ a } -{ b } -{ c } "
96
+ for a , b , c in itertools .product (
97
+ final_python_tags , final_abi_tags , final_plat_tags
78
98
)
79
-
80
- # Start changing as needed
81
- if build_number is not None :
82
- build = str (build_number )
83
-
84
- final_python_tags = compute_tags (original_python_tags , python_tags )
85
- final_abi_tags = compute_tags (original_abi_tags , abi_tags )
86
- final_plat_tags = compute_tags (orignial_plat_tags , platform_tags )
87
-
88
- final_tags = [
89
- "." .join (sorted (final_python_tags )),
90
- "." .join (sorted (final_abi_tags )),
91
- "." .join (sorted (final_plat_tags )),
92
99
]
93
100
94
- if build :
95
- final_tags .insert (0 , build )
96
- final_tags .insert (0 , namever )
97
-
98
- final_wheel_name = "-" .join (final_tags ) + ".whl"
99
-
100
- if original_wheel_name != final_wheel_name :
101
- tags = [
102
- f"{ a } -{ b } -{ c } "
103
- for a , b , c in itertools .product (
104
- final_python_tags , final_abi_tags , final_plat_tags
105
- )
106
- ]
107
-
108
- original_wheel_path = os .path .join (
109
- os .path .dirname (f .filename ), original_wheel_name
110
- )
111
- final_wheel_path = os .path .join (
112
- os .path .dirname (f .filename ), final_wheel_name
113
- )
114
-
115
- with WheelFile (original_wheel_path , "r" ) as fin , WheelFile (
116
- final_wheel_path , "w"
117
- ) as fout :
118
- fout .comment = fin .comment # preserve the comment
119
- for item in fin .infolist ():
120
- if item .filename == f .dist_info_path + "/RECORD" :
121
- continue
122
- if item .filename == f .dist_info_path + "/WHEEL" :
123
- content = fin .read (item )
124
- content = set_tags (content , tags )
125
- content = set_build_number (content , build )
126
- fout .writestr (item , content )
127
- else :
128
- fout .writestr (item , fin .read (item ))
129
-
130
- if remove :
131
- os .remove (original_wheel_path )
132
-
133
- yield final_wheel_name
101
+ original_wheel_path = os .path .join (
102
+ os .path .dirname (f .filename ), original_wheel_name
103
+ )
104
+ final_wheel_path = os .path .join (os .path .dirname (f .filename ), final_wheel_name )
105
+
106
+ with WheelFile (original_wheel_path , "r" ) as fin , WheelFile (
107
+ final_wheel_path , "w"
108
+ ) as fout :
109
+ fout .comment = fin .comment # preserve the comment
110
+ for item in fin .infolist ():
111
+ if item .filename == f .dist_info_path + "/RECORD" :
112
+ continue
113
+ if item .filename == f .dist_info_path + "/WHEEL" :
114
+ content = fin .read (item )
115
+ content = set_tags (content , tags )
116
+ content = set_build_number (content , build )
117
+ fout .writestr (item , content )
118
+ else :
119
+ fout .writestr (item , fin .read (item ))
120
+
121
+ if remove :
122
+ os .remove (original_wheel_path )
123
+
124
+ return final_wheel_name
134
125
135
126
136
127
def set_tags (in_string : bytes , tags : list [str ]) -> bytes :
0 commit comments