|
7 | 7 | import requests |
8 | 8 | from requests.adapters import HTTPAdapter |
9 | 9 | from urllib3.util.retry import Retry |
| 10 | +from tqdm import tqdm |
10 | 11 | import tarfile |
11 | 12 | import urllib3 |
12 | 13 | urllib3.disable_warnings() |
@@ -130,13 +131,20 @@ def download_layers(session, registry, repository, layers, auth_head, imgdir, re |
130 | 131 | try: |
131 | 132 | bresp = session.get(f'https://{registry}/v2/{repository}/blobs/{ublob}', headers=auth_head, stream=True, verify=False, timeout=30) |
132 | 133 | bresp.raise_for_status() |
| 134 | + |
| 135 | + # 使用 tqdm 显示下载进度 |
| 136 | + total_size = int(bresp.headers.get('content-length', 0)) |
| 137 | + with tqdm(total=total_size, unit='B', unit_scale=True, desc=f'Downloading {ublob[:12]}') as pbar: |
| 138 | + with open(f'{layerdir}/layer_gzip.tar', 'wb') as file: |
| 139 | + for chunk in bresp.iter_content(chunk_size=1024): |
| 140 | + if chunk: |
| 141 | + file.write(chunk) |
| 142 | + pbar.update(len(chunk)) |
| 143 | + |
133 | 144 | except requests.exceptions.RequestException as e: |
134 | 145 | print(f'下载层错误:{e}') |
135 | 146 | exit(1) |
136 | 147 |
|
137 | | - with open(f'{layerdir}/layer_gzip.tar', 'wb') as file: |
138 | | - shutil.copyfileobj(bresp.raw, file) |
139 | | - |
140 | 148 | with open(f'{layerdir}/layer.tar', 'wb') as file: |
141 | 149 | with gzip.open(f'{layerdir}/layer_gzip.tar', 'rb') as gz: |
142 | 150 | shutil.copyfileobj(gz, file) |
@@ -229,6 +237,7 @@ def main(): |
229 | 237 | if not os.path.exists(imgdir): |
230 | 238 | os.makedirs(imgdir) |
231 | 239 |
|
| 240 | + print('开始下载层...') |
232 | 241 | download_layers(session, registry, repository, layers, auth_head, imgdir, resp_json, imgparts, img, tag) |
233 | 242 |
|
234 | 243 | create_image_tar(imgdir, repo, img) |
|
0 commit comments