Skip to content

Commit

Permalink
修复编码错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
topcss committed Feb 5, 2025
1 parent 3c2de08 commit 5347ce3
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions docker_image_puller.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ def download_layers(session, registry, repository, layers, auth_head, imgdir, re
confresp.raise_for_status()
with open(f'{imgdir}/{config[7:]}.json', 'wb') as file:
shutil.copyfileobj(confresp.raw, file)
except requests.exceptions.RequestException as e:
# 检查文件是否为 gzip 压缩格式
with open(f'{imgdir}/{config[7:]}.json', 'rb') as file:
first_bytes = file.read(4)
if first_bytes == b'\x1f\x8b\x08\x00':
# 解压缩 gzip 文件
with gzip.open(f'{imgdir}/{config[7:]}.json', 'rb') as gz, open(f'{imgdir}/{config[7:]}1.json', 'wb') as file:
shutil.copyfileobj(gz, file)
if os.path.exists(f'{imgdir}/{config[7:]}1.json'):
os.remove(f'{imgdir}/{config[7:]}.json')
os.rename(f'{imgdir}/{config[7:]}1.json', f'{imgdir}/{config[7:]}.json')
except Exception as e:
logger.error(f'请求配置失败: {e}')
raise

Expand Down Expand Up @@ -190,7 +200,9 @@ def download_layers(session, registry, repository, layers, auth_head, imgdir, re

# 生成层元数据
if layers[-1]['digest'] == layer['digest']:
json_obj = json.loads(open(f'{imgdir}/{config[7:]}.json').read())
with open(f'{imgdir}/{config[7:]}.json', 'rb') as file:
json_data = file.read()
json_obj = json.loads(json_data.decode('utf-8'))
json_obj.pop('history', None)
json_obj.pop('rootfs', None)
else:
Expand All @@ -202,7 +214,7 @@ def download_layers(session, registry, repository, layers, auth_head, imgdir, re

with open(f'{layerdir}/json', 'w') as file:
json.dump(json_obj, file)
except requests.exceptions.RequestException as e:
except Exception as e:
logger.error(f'请求层失败: {e}')
raise

Expand All @@ -214,6 +226,7 @@ def download_layers(session, registry, repository, layers, auth_head, imgdir, re
with open(f'{imgdir}/repositories', 'w') as file:
json.dump({repo_tag: {tag: fake_layerid}}, file)


def create_image_tar(imgdir, repo, img, arch):
"""将镜像打包为 tar 文件"""
docker_tar = f'{repo.replace("/", "_")}_{img}_{arch}.tar'
Expand Down

0 comments on commit 5347ce3

Please sign in to comment.