51 lines
3.1 KiB
Python
51 lines
3.1 KiB
Python
"""Provision the bundled map service on Linux; run before enabling its systemd unit.
|
|
|
|
Public downloads only. No location, family or credential data is sent upstream.
|
|
Python 3.11+; install python3-cryptography for the app's encrypted track archive.
|
|
"""
|
|
from pathlib import Path
|
|
import hashlib,os,shutil,subprocess,tarfile,urllib.request,zipfile
|
|
|
|
root=Path('/var/lib/zhaovision/maps');root.mkdir(parents=True,exist_ok=True)
|
|
tools=Path('/opt/zhaovision/tools');tools.mkdir(parents=True,exist_ok=True)
|
|
if shutil.disk_usage(root).free<12*1024**3:raise SystemExit('At least 12 GiB free space is required')
|
|
|
|
def download(url,dest,sha=None,accept=None):
|
|
if dest.exists() and (not sha or hashlib.file_digest(dest.open('rb'),'sha256').hexdigest()==sha):return
|
|
temporary=dest.with_suffix(dest.suffix+'.part')
|
|
headers={'User-Agent':'ZhaoVision-map-provisioner'}
|
|
if accept:headers['Accept']=accept
|
|
try:
|
|
with urllib.request.urlopen(urllib.request.Request(url,headers=headers),timeout=90) as response,temporary.open('wb') as out:
|
|
while chunk:=response.read(262144):
|
|
if shutil.disk_usage(root).free<10*1024**3:raise RuntimeError('Disk reserve reached')
|
|
out.write(chunk)
|
|
if sha and hashlib.file_digest(temporary.open('rb'),'sha256').hexdigest()!=sha:raise RuntimeError('Checksum mismatch')
|
|
os.replace(temporary,dest)
|
|
finally:temporary.unlink(missing_ok=True)
|
|
|
|
archive=root/'pmtiles-tool.tar.gz'
|
|
download('https://api.github.com/repos/protomaps/go-pmtiles/releases/assets/486313332',archive,
|
|
'3ed7dbf4ec2e6dfe5e25b6f70d1ffc932729f93c86db353bf514dd71010a312f','application/octet-stream')
|
|
with tarfile.open(archive) as tar:(tools/'pmtiles').write_bytes(tar.extractfile('pmtiles').read())
|
|
(tools/'pmtiles').chmod(0o755);archive.unlink()
|
|
for name,opts in [('world',['--maxzoom=6']),('china',['--maxzoom=9','--bbox=73,3,135.2,53.7'])]:
|
|
dest=root/(name+'.pmtiles');partial=root/(name+'.pmtiles.part')
|
|
if not dest.exists():
|
|
partial.unlink(missing_ok=True)
|
|
subprocess.run([str(tools/'pmtiles'),'extract','https://build.protomaps.com/20261017.pmtiles',str(partial),'--download-threads=2',*opts],check=True,timeout=5400,env=dict(os.environ,GOMEMLIMIT='192MiB',GOMAXPROCS='1'))
|
|
subprocess.run([str(tools/'pmtiles'),'verify',str(partial)],check=True,timeout=180);os.replace(partial,dest)
|
|
subprocess.run([str(tools/'pmtiles'),'verify',str(dest)],check=True,timeout=180)
|
|
assets=root/'assets.zip'
|
|
download('https://codeload.github.com/protomaps/basemaps-assets/zip/refs/heads/main',assets)
|
|
with zipfile.ZipFile(assets) as bundle:
|
|
for member in bundle.infolist():
|
|
parts=Path(member.filename).parts[1:]
|
|
if not parts or member.is_dir() or parts[0] not in ('fonts','sprites') or '..' in parts:continue
|
|
dest=root/'assets'/Path(*parts);dest.parent.mkdir(parents=True,exist_ok=True);dest.write_bytes(bundle.read(member))
|
|
assets.unlink()
|
|
import pwd
|
|
identity=pwd.getpwnam('zhaovision')
|
|
for file in [root,*root.rglob('*')]:os.chown(file,identity.pw_uid,identity.pw_gid)
|
|
print('Prepared world + China overview. Provinces download on authorized arrival.')
|