download Contents as per Release file

This commit is contained in:
z4yx 2020-04-19 19:04:59 +08:00
parent 1780541c36
commit 52c0154928

View File

@ -106,11 +106,7 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
return 1 return 1
check_and_download(f"{base_url}/dists/{dist}/Release.gpg",dist_tmp_dir / "Release.gpg") check_and_download(f"{base_url}/dists/{dist}/Release.gpg",dist_tmp_dir / "Release.gpg")
# download Contents files
comp_dir,comp_tmp_dir = mkdir_with_dot_tmp(dist_dir / repo) comp_dir,comp_tmp_dir = mkdir_with_dot_tmp(dist_dir / repo)
check_and_download(f"{base_url}/dists/{dist}/{repo}/Contents-{arch}", comp_tmp_dir/f"Contents-{arch}")
check_and_download(f"{base_url}/dists/{dist}/{repo}/Contents-{arch}.gz", comp_tmp_dir/f"Contents-{arch}.gz")
check_and_download(f"{base_url}/dists/{dist}/{repo}/Contents-{arch}.bz2", comp_tmp_dir/f"Contents-{arch}.bz2")
# load Package Index URLs from the Release file # load Package Index URLs from the Release file
release_file = dist_tmp_dir / "Release" release_file = dist_tmp_dir / "Release"
@ -124,32 +120,36 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
if len(fields) != 3 or len(fields[0]) != 64: # 64 is SHA-256 checksum length if len(fields) != 3 or len(fields[0]) != 64: # 64 is SHA-256 checksum length
break break
checksum, filesize, filename = tuple(fields) checksum, filesize, filename = tuple(fields)
if filename.startswith(f"{repo}/binary-{arch}"): if not (filename.startswith(f"{repo}/binary-{arch}") or \
pkgidx_filename = Path(filename).name # base name filename.startswith(f"{repo}/Contents")):
pkgidx_file = pkgidx_tmp_dir / pkgidx_filename print(f"Ignore the file {filename}")
pkglist_url = f"{base_url}/dists/{dist}/{filename}" continue
if check_and_download(pkglist_url, pkgidx_file) != 0: pkgidx_file = dist_tmp_dir / filename
print("Failed to download:", pkglist_url) pkglist_url = f"{base_url}/dists/{dist}/{filename}"
continue if check_and_download(pkglist_url, pkgidx_file) != 0:
print("Failed to download:", pkglist_url)
with pkgidx_file.open('rb') as t: content = t.read() continue
if len(content) != int(filesize):
print(f"Invalid size of {pkgidx_file}, expected {filesize}") with pkgidx_file.open('rb') as t: content = t.read()
return 1 if len(content) != int(filesize):
if hashlib.sha256(content).hexdigest() != checksum: print(f"Invalid size of {pkgidx_file}, expected {filesize}")
print(f"Invalid checksum of {pkgidx_file}, expected {checksum}") return 1
return 1 if hashlib.sha256(content).hexdigest() != checksum:
if pkgidx_content is None: print(f"Invalid checksum of {pkgidx_file}, expected {checksum}")
print("getting packages index content") return 1
suffix = pkgidx_file.suffix if pkgidx_content is None and pkgidx_file.stem == 'Packages':
if suffix == '.xz': print(f"getting packages index content from {pkgidx_file.name}")
pkgidx_content = lzma.decompress(content).decode('utf-8') suffix = pkgidx_file.suffix
elif suffix == '.bz2': if suffix == '.xz':
pkgidx_content = bz2.decompress(content).decode('utf-8') pkgidx_content = lzma.decompress(content).decode('utf-8')
elif suffix == '.gz': elif suffix == '.bz2':
pkgidx_content = gzip.decompress(content).decode('utf-8') pkgidx_content = bz2.decompress(content).decode('utf-8')
elif suffix == '': elif suffix == '.gz':
pkgidx_content = content.decode('utf-8') pkgidx_content = gzip.decompress(content).decode('utf-8')
elif suffix == '':
pkgidx_content = content.decode('utf-8')
else:
print("unsupported format")
# Currently only support SHA-256 checksum, because # Currently only support SHA-256 checksum, because
# "Clients may not use the MD5Sum and SHA1 fields for security purposes, and must require a SHA256 or a SHA512 field." # "Clients may not use the MD5Sum and SHA1 fields for security purposes, and must require a SHA256 or a SHA512 field."