Files
quality_analyse/deploy/docker/fetch_runtimes.py
T

28 lines
1.3 KiB
Python

"""构建时下载并校验固定 Linux x86_64 运行时,运行阶段不再下载。"""
import hashlib
import json
from pathlib import Path
import shutil
import tarfile
import tempfile
import urllib.request
def install(lock, destination):
destination = Path(destination)
for asset in json.loads(Path(lock).read_text()):
with tempfile.TemporaryDirectory() as temporary:
temporary = Path(temporary); archive = temporary / 'runtime.tar'
with urllib.request.urlopen(asset['url'], timeout=120) as source, archive.open('wb') as output:
shutil.copyfileobj(source, output)
with archive.open('rb') as source: digest = hashlib.file_digest(source, 'sha256').hexdigest()
if digest != asset['sha256']: raise ValueError('Runtime checksum mismatch: ' + asset['name'])
unpacked = temporary / 'unpacked'; unpacked.mkdir()
with tarfile.open(archive) as source: source.extractall(unpacked, filter='data')
children = list(unpacked.iterdir())
if len(children) != 1 or not children[0].is_dir(): raise ValueError('Unexpected archive layout')
shutil.copytree(children[0], destination / asset['name'], symlinks=True)
if __name__ == '__main__': install('/build/assets.lock.json', '/runtimes')