host_manager

This commit is contained in:
Zsolt Ero
2024-01-02 23:01:17 +01:00
parent b8a3ae7552
commit ac9803ee69
10 changed files with 235 additions and 233 deletions

View File

@@ -0,0 +1,43 @@
import shutil
import subprocess
from pathlib import Path
from http_host_lib.utils import download_if_size_differs
def download_fonts(assets_dir: Path):
"""
Download and extract font assets if their file size differ.
Making updates atomic, with extraction to a temp dest + rename
"""
fonts_dir = assets_dir / 'fonts'
fonts_dir.mkdir(exist_ok=True, parents=True)
fonts_temp = assets_dir / 'fonts_temp'
for font in ['ml', 'omt', 'pm']:
url = f'https://assets.openfreemap.com/fonts/{font}.tgz'
local_file = fonts_dir / f'{font}.tgz'
if not download_if_size_differs(url, local_file):
continue
shutil.rmtree(fonts_temp, ignore_errors=True)
fonts_temp.mkdir()
subprocess.run(
['tar', '-xzf', local_file, '-C', fonts_temp],
check=True,
)
target_dir = fonts_dir / font
target_dir_renamed = fonts_dir / f'{font}.bak'
temp_dir = fonts_temp / font
if target_dir.exists():
target_dir.rename(target_dir_renamed)
temp_dir.rename(target_dir)
shutil.rmtree(target_dir_renamed, ignore_errors=True)
shutil.rmtree(fonts_temp, ignore_errors=True)

View File

@@ -0,0 +1,42 @@
import shutil
import subprocess
import sys
from pathlib import Path
import click
from http_host_lib.utils import download_file_aria2
def download_and_extract_tileset(area: str, version: str, runs_dir: Path) -> bool:
"""
returns True if downloaded something
"""
click.echo(f'Downloading: area: {area}, version: {version}')
version_dir = runs_dir / area / version
btrfs_file = version_dir / 'tiles.btrfs'
if btrfs_file.exists():
print('File exists, skipping download')
return False
temp_dir = runs_dir / '_tmp'
if temp_dir.exists():
sys.exit(f'{temp_dir} dir exists, avoiding parallel run')
temp_dir.mkdir(parents=True)
url = f'https://{area}.openfreemap.com/{version}/tiles.btrfs.gz'
target_file = temp_dir / 'tiles.btrfs.gz'
download_file_aria2(url, target_file)
subprocess.run(['unpigz', temp_dir / 'tiles.btrfs.gz'], check=True)
btrfs_src = temp_dir / 'tiles.btrfs'
shutil.rmtree(version_dir, ignore_errors=True)
version_dir.mkdir(parents=True)
btrfs_src.rename(btrfs_file)
shutil.rmtree(temp_dir)
return True

View File

@@ -0,0 +1,50 @@
import os
import subprocess
import sys
from pathlib import Path
import requests
def assert_sudo():
if os.geteuid() != 0:
sys.exit('Needs sudo')
def assert_linux():
if not Path('/etc/fstab').exists():
sys.exit('Needs to be run on Linux')
def download_if_size_differs(url: str, local_file: Path) -> bool:
if not local_file.exists() or local_file.stat().st_size != get_remote_file_size(url):
download_file_aria2(url, local_file)
return True
return False
def get_remote_file_size(url: str) -> int | None:
r = requests.head(url)
size = r.headers.get('Content-Length')
return int(size) if size else None
def download_file_aria2(url: str, local_file: Path):
print(f'Downloading: {url} into {local_file}')
subprocess.run(
[
'aria2c',
'--split=8',
'--max-connection-per-server=8',
'--file-allocation=none',
'--min-split-size=1M',
'-d',
local_file.parent,
'-o',
local_file.name,
url,
],
check=True,
)