mirror of
https://github.com/tuna/tunasync-scripts.git
synced 2025-07-01 15:35:45 +00:00
fix some errors
This commit is contained in:
parent
18a78ebf83
commit
a8b0c783c9
31
apt-sync.py
31
apt-sync.py
@ -13,7 +13,7 @@ import gzip
|
|||||||
import time
|
import time
|
||||||
from email.utils import parsedate_to_datetime
|
from email.utils import parsedate_to_datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple, IO
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
@ -67,8 +67,8 @@ def check_and_download(url: str, dst_file: Path)->int:
|
|||||||
if remote_ts is not None:
|
if remote_ts is not None:
|
||||||
os.utime(dst_file, (remote_ts, remote_ts))
|
os.utime(dst_file, (remote_ts, remote_ts))
|
||||||
return 0
|
return 0
|
||||||
except:
|
except BaseException as e:
|
||||||
traceback.print_exc()
|
print(e)
|
||||||
if dst_file.is_file():
|
if dst_file.is_file():
|
||||||
dst_file.unlink()
|
dst_file.unlink()
|
||||||
return 1
|
return 1
|
||||||
@ -76,7 +76,7 @@ def check_and_download(url: str, dst_file: Path)->int:
|
|||||||
def mkdir_with_dot_tmp(folder: Path)->Tuple[Path, Path]:
|
def mkdir_with_dot_tmp(folder: Path)->Tuple[Path, Path]:
|
||||||
tmpdir = folder / ".tmp"
|
tmpdir = folder / ".tmp"
|
||||||
if tmpdir.is_dir():
|
if tmpdir.is_dir():
|
||||||
shutil.rmtree(tmpdir)
|
shutil.rmtree(str(tmpdir))
|
||||||
tmpdir.mkdir(parents=True, exist_ok=True)
|
tmpdir.mkdir(parents=True, exist_ok=True)
|
||||||
return (folder, tmpdir)
|
return (folder, tmpdir)
|
||||||
|
|
||||||
@ -84,12 +84,12 @@ def move_files_in(src: Path, dst: Path):
|
|||||||
empty = True
|
empty = True
|
||||||
for file in src.glob('*'):
|
for file in src.glob('*'):
|
||||||
empty = False
|
empty = False
|
||||||
print(f"move {file} to {dst}")
|
print(f"moving {file} to {dst}")
|
||||||
shutil.move(file, dst)
|
shutil.move(str(file), str(dst))
|
||||||
if empty:
|
if empty:
|
||||||
raise ValueError(f"{src} is empty")
|
print(f"{src} is empty")
|
||||||
|
|
||||||
def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Path, filelist: str = '/dev/null')->int:
|
def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Path, filelist: IO[str])->int:
|
||||||
if not dest_base_dir.is_dir():
|
if not dest_base_dir.is_dir():
|
||||||
print("Destination directory is empty, cannot continue")
|
print("Destination directory is empty, cannot continue")
|
||||||
return 1
|
return 1
|
||||||
@ -128,7 +128,7 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
|
|||||||
pkglist_url = f"{base_url}/dists/{dist}/{filename}"
|
pkglist_url = f"{base_url}/dists/{dist}/{filename}"
|
||||||
if check_and_download(pkglist_url, pkgidx_file) != 0:
|
if check_and_download(pkglist_url, pkgidx_file) != 0:
|
||||||
print("Failed to download:", pkglist_url)
|
print("Failed to download:", pkglist_url)
|
||||||
return 1
|
continue
|
||||||
|
|
||||||
with pkgidx_file.open('rb') as t: content = t.read()
|
with pkgidx_file.open('rb') as t: content = t.read()
|
||||||
if len(content) != int(filesize):
|
if len(content) != int(filesize):
|
||||||
@ -161,6 +161,7 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
|
|||||||
|
|
||||||
# Download packages
|
# Download packages
|
||||||
err = 0
|
err = 0
|
||||||
|
deb_count = 0
|
||||||
for pkg in pkgidx_content.split('\n\n'):
|
for pkg in pkgidx_content.split('\n\n'):
|
||||||
try:
|
try:
|
||||||
pkg_filename = pattern_package_name.search(pkg).group(1)
|
pkg_filename = pattern_package_name.search(pkg).group(1)
|
||||||
@ -172,6 +173,7 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
|
|||||||
err = 1
|
err = 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
filelist.write(pkg_filename + '\n')
|
||||||
dest_filename = dest_base_dir / pkg_filename
|
dest_filename = dest_base_dir / pkg_filename
|
||||||
dest_dir = dest_filename.parent
|
dest_dir = dest_filename.parent
|
||||||
if not dest_dir.is_dir():
|
if not dest_dir.is_dir():
|
||||||
@ -198,11 +200,17 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
|
|||||||
else:
|
else:
|
||||||
print(f"Failed to download {dest_filename}")
|
print(f"Failed to download {dest_filename}")
|
||||||
err = 1
|
err = 1
|
||||||
|
deb_count += 1
|
||||||
|
# if deb_count == 2:
|
||||||
|
# break
|
||||||
try:
|
try:
|
||||||
move_files_in(pkgidx_tmp_dir, pkgidx_dir)
|
move_files_in(pkgidx_tmp_dir, pkgidx_dir)
|
||||||
move_files_in(comp_tmp_dir, comp_dir)
|
move_files_in(comp_tmp_dir, comp_dir)
|
||||||
move_files_in(dist_tmp_dir, dist_dir)
|
move_files_in(dist_tmp_dir, dist_dir)
|
||||||
|
|
||||||
|
shutil.rmtree(str(pkgidx_tmp_dir))
|
||||||
|
shutil.rmtree(str(comp_tmp_dir))
|
||||||
|
shutil.rmtree(str(dist_tmp_dir))
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return 1
|
return 1
|
||||||
@ -252,7 +260,8 @@ def main():
|
|||||||
# ret = sp.run(shell_args)
|
# ret = sp.run(shell_args)
|
||||||
# if ret.returncode != 0:
|
# if ret.returncode != 0:
|
||||||
# failed.append((os, comp, arch))
|
# failed.append((os, comp, arch))
|
||||||
if apt_mirror(args.base_url, os, comp, arch, args.working_dir, filelist[1]) != 0:
|
with open(filelist[1], "w") as pkg_file_list:
|
||||||
|
if apt_mirror(args.base_url, os, comp, arch, args.working_dir, pkg_file_list) != 0:
|
||||||
failed.append((os, comp, arch))
|
failed.append((os, comp, arch))
|
||||||
if len(failed) > 0:
|
if len(failed) > 0:
|
||||||
print(f"Failed APT repos of {args.base_url}: ", failed)
|
print(f"Failed APT repos of {args.base_url}: ", failed)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user