11from __future__ import annotations
22
33import argparse
4+ import logging
45import os
56import subprocess
67import zipfile
8+ import zlib
79from collections .abc import Generator , Iterable
810from datetime import datetime , timezone
911from pathlib import Path
1012from typing import Any , TypeVar
1113
1214_T = TypeVar ("_T" )
1315
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+
1425
1526def unique_by_index (sequence : Iterable [_T ]) -> list [_T ]:
1627 """unique elements in `sequence` in the order in which they occur
@@ -90,6 +101,7 @@ def zip2dir(zip_fname: Path, out_dir: Path) -> None:
90101 out_dir : str
91102 Directory path containing files to go in the zip archive
92103 """
104+ start = datetime .now ()
93105 with zipfile .ZipFile (zip_fname , "r" ) as z :
94106 for name in z .namelist ():
95107 member = z .getinfo (name )
@@ -102,6 +114,9 @@ def zip2dir(zip_fname: Path, out_dir: Path) -> None:
102114 attr &= 511 # only keep permission bits
103115 attr |= 6 << 6 # at least read/write for current user
104116 os .chmod (extracted_path , attr )
117+ logger .debug (
118+ "zip2dir from %s to %s takes %s" , zip_fname , out_dir , datetime .now () - start
119+ )
105120
106121
107122def 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) ->
120135 date_time : Optional[datetime]
121136 Time stamp to set on each file in the archive
122137 """
138+ start = datetime .now ()
123139 in_dir = in_dir .resolve (strict = True )
124140 if date_time is None :
125141 st = in_dir .stat ()
@@ -140,7 +156,10 @@ def dir2zip(in_dir: Path, zip_fname: Path, date_time: datetime | None = None) ->
140156 zinfo .date_time = date_time_args
141157 zinfo .compress_type = compression
142158 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+ )
144163
145164
146165def tarbz2todir (tarbz2_fname : Path , out_dir : Path ) -> None :
@@ -157,11 +176,14 @@ def __init__(
157176 required : bool = True ,
158177 default : str | None = None ,
159178 choices : Iterable [str ] | None = None ,
179+ type : type | None = None ,
160180 ** kwargs : Any ,
161181 ) -> None :
162182 self .env_default = os .environ .get (env )
163183 self .env = env
164184 if self .env_default :
185+ if type :
186+ self .env_default = type (self .env_default )
165187 default = self .env_default
166188 if default :
167189 required = False
0 commit comments