1
1
from __future__ import annotations
2
2
3
3
import argparse
4
+ import logging
4
5
import os
5
6
import subprocess
6
7
import zipfile
8
+ import zlib
7
9
from collections .abc import Generator , Iterable
8
10
from datetime import datetime , timezone
9
11
from pathlib import Path
10
12
from typing import Any , TypeVar
11
13
12
14
_T = TypeVar ("_T" )
13
15
16
+ logger = logging .getLogger (__name__ )
17
+
18
+ # Default: zlib.Z_DEFAULT_COMPRESSION (-1 aka. level 6) balances speed and size.
19
+ # Maintained for typical builds where iteration speed outweighs distribution savings.
20
+ # Override via AUDITWHEEL_ZIP_LEVEL/--zip-level for:
21
+ # - some test builds that needs no compression at all (0)
22
+ # - bandwidth-constrained or large amount of downloads (9)
23
+ _COMPRESS_LEVEL = zlib .Z_DEFAULT_COMPRESSION
24
+
14
25
15
26
def unique_by_index (sequence : Iterable [_T ]) -> list [_T ]:
16
27
"""unique elements in `sequence` in the order in which they occur
@@ -90,6 +101,7 @@ def zip2dir(zip_fname: Path, out_dir: Path) -> None:
90
101
out_dir : str
91
102
Directory path containing files to go in the zip archive
92
103
"""
104
+ start = datetime .now ()
93
105
with zipfile .ZipFile (zip_fname , "r" ) as z :
94
106
for name in z .namelist ():
95
107
member = z .getinfo (name )
@@ -102,6 +114,9 @@ def zip2dir(zip_fname: Path, out_dir: Path) -> None:
102
114
attr &= 511 # only keep permission bits
103
115
attr |= 6 << 6 # at least read/write for current user
104
116
os .chmod (extracted_path , attr )
117
+ logger .debug (
118
+ "zip2dir from %s to %s takes %s" , zip_fname , out_dir , datetime .now () - start
119
+ )
105
120
106
121
107
122
def dir2zip (in_dir : Path , zip_fname : Path , date_time : datetime | None = None ) -> None :
@@ -120,6 +135,7 @@ def dir2zip(in_dir: Path, zip_fname: Path, date_time: datetime | None = None) ->
120
135
date_time : Optional[datetime]
121
136
Time stamp to set on each file in the archive
122
137
"""
138
+ start = datetime .now ()
123
139
in_dir = in_dir .resolve (strict = True )
124
140
if date_time is None :
125
141
st = in_dir .stat ()
@@ -140,7 +156,10 @@ def dir2zip(in_dir: Path, zip_fname: Path, date_time: datetime | None = None) ->
140
156
zinfo .date_time = date_time_args
141
157
zinfo .compress_type = compression
142
158
with open (fname , "rb" ) as fp :
143
- z .writestr (zinfo , fp .read ())
159
+ z .writestr (zinfo , fp .read (), compresslevel = _COMPRESS_LEVEL )
160
+ logger .debug (
161
+ "dir2zip from %s to %s takes %s" , in_dir , zip_fname , datetime .now () - start
162
+ )
144
163
145
164
146
165
def tarbz2todir (tarbz2_fname : Path , out_dir : Path ) -> None :
@@ -157,11 +176,14 @@ def __init__(
157
176
required : bool = True ,
158
177
default : str | None = None ,
159
178
choices : Iterable [str ] | None = None ,
179
+ type : type | None = None ,
160
180
** kwargs : Any ,
161
181
) -> None :
162
182
self .env_default = os .environ .get (env )
163
183
self .env = env
164
184
if self .env_default :
185
+ if type :
186
+ self .env_default = type (self .env_default )
165
187
default = self .env_default
166
188
if default :
167
189
required = False
0 commit comments